Files
rss-server/handlers/pixiv/Webdav.py
mol 5157f4f8d2
All checks were successful
continuous-integration/drone/push Build is passing
feat
2023-09-05 18:06:07 +08:00

57 lines
1.9 KiB
Python

import os
from webdav3.client import Client
from webdav3.exceptions import LocalResourceNotFound, RemoteResourceNotFound
class Webdav(object):
def __init__(self, option):
webdav_option = {
'webdav_hostname': option['webdav_hostname'],
'webdav_login': option['webdav_login'],
'webdav_password': option['webdav_password'],
'disable_check': option['disable_check'],
}
self.client = self.connect_webdav(webdav_option)
self.default_upload_path = option['default_upload_path']
# 链接webdav
def connect_webdav(self, options):
return Client(options)
# 批量同步
def batch_sync(self, file_list, upload_path=None):
# 记录失败与成功的id
success_ids = []
failed_ids = []
# 获取临时文件路径
temp_path = os.path.join(os.path.dirname(__file__), 'temp')
for file_item in file_list:
path = os.path.join(temp_path, file_item['file_name'])
result_flag = self.sync_file(file_item['file_name'], path, upload_path)
if result_flag:
success_ids.append(file_item['id'])
else:
failed_ids.append(file_item['id'])
return (success_ids, failed_ids)
def sync_file(self, file_name, path, upload_path=None):
result_flag = True
try:
# self.client.upload('Pictures/ACGN/Pixiv/' + file_name, path)
if not(upload_path):
upload_path = self.default_upload_path
self.client.upload(upload_path + file_name, path)
print('Info: ' + file_name + ' upload success!!')
except LocalResourceNotFound as exception:
result_flag = False
print('Error: ' + file_name + ' upload failed!!')
return result_flag