This commit is contained in:
64
handlers/pixiv/pixiv_handler.py
Normal file
64
handlers/pixiv/pixiv_handler.py
Normal file
@ -0,0 +1,64 @@
|
||||
import re
|
||||
import time
|
||||
import os
|
||||
|
||||
from handlers.pixiv.comb import extract_pixiv_info, get_prefix_name_on_entry
|
||||
from handlers.pixiv.Webdav import Webdav
|
||||
from handlers.pixiv.request import downloadPic
|
||||
from handlers.pixiv.file import save_file, remove_file
|
||||
|
||||
|
||||
webdav = Webdav({
|
||||
'webdav_hostname': os.getenv('webdav_hostname'),
|
||||
'webdav_login': os.getenv('webdav_login'),
|
||||
'webdav_password': os.getenv('webdav_password'),
|
||||
'disable_check': True,
|
||||
'default_upload_path': os.getenv('default_upload_path')
|
||||
})
|
||||
|
||||
# 处理结果
|
||||
def pixiv_result_handler(entries):
|
||||
list = extract_pixiv_info(entries)
|
||||
|
||||
success_entries = []
|
||||
failed_entries = []
|
||||
|
||||
for item in list:
|
||||
prefix_name = get_prefix_name_on_entry(item)
|
||||
|
||||
result_flag = True
|
||||
|
||||
need_sync_files = []
|
||||
|
||||
for url in item['link']:
|
||||
file_name_pattern = re.compile(r'\/(\w*\.(?:jpg|png))$')
|
||||
file_name = ','.join(re.findall(file_name_pattern, url))
|
||||
|
||||
if file_name:
|
||||
full_name = f'{prefix_name} {file_name}'
|
||||
# 替换不符合文件名规范的字符
|
||||
full_name = re.sub(r'[\/\\\:\*\?\"\<\>\|]', "_", full_name)
|
||||
(status, data) = downloadPic(url)
|
||||
if status:
|
||||
save_file(full_name, data)
|
||||
need_sync_files.append({ 'id': item['id'], 'file_name': full_name })
|
||||
else:
|
||||
result_flag = False
|
||||
time.sleep(10)
|
||||
|
||||
if result_flag:
|
||||
(success_ids, failed_ids) = webdav.batch_sync(need_sync_files)
|
||||
|
||||
for entry in entries:
|
||||
if entry['id'] in success_ids:
|
||||
success_entries.append(entry)
|
||||
elif entry['id'] in failed_ids:
|
||||
failed_entries.append(entry)
|
||||
else:
|
||||
for entry in entries:
|
||||
if entry['id'] == item['id']:
|
||||
failed_entries.append(entry)
|
||||
|
||||
return (success_entries, failed_entries)
|
||||
|
||||
|
Reference in New Issue
Block a user