fix #788 Rclone远程刮削
This commit is contained in:
@ -1,25 +1,34 @@
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
from xml.dom import minidom
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.context import MediaInfo
|
||||
from app.core.meta import MetaBase
|
||||
from app.log import logger
|
||||
from app.schemas.types import MediaType
|
||||
from app.utils.dom import DomUtils
|
||||
from app.utils.http import RequestUtils
|
||||
from app.utils.system import SystemUtils
|
||||
|
||||
|
||||
class DoubanScraper:
|
||||
|
||||
def gen_scraper_files(self, meta: MetaBase, mediainfo: MediaInfo, file_path: Path):
|
||||
_transfer_type = settings.TRANSFER_TYPE
|
||||
|
||||
def gen_scraper_files(self, meta: MetaBase, mediainfo: MediaInfo,
|
||||
file_path: Path, transfer_type: str):
|
||||
"""
|
||||
生成刮削文件
|
||||
:param meta: 元数据
|
||||
:param mediainfo: 媒体信息
|
||||
:param file_path: 文件路径或者目录路径
|
||||
:param transfer_type: 转输类型
|
||||
"""
|
||||
|
||||
self._transfer_type = transfer_type
|
||||
|
||||
try:
|
||||
# 电影
|
||||
if mediainfo.type == MediaType.MOVIE:
|
||||
@ -154,8 +163,7 @@ class DoubanScraper:
|
||||
# 保存
|
||||
self.__save_nfo(doc, season_path.joinpath("season.nfo"))
|
||||
|
||||
@staticmethod
|
||||
def __save_image(url: str, file_path: Path):
|
||||
def __save_image(self, url: str, file_path: Path):
|
||||
"""
|
||||
下载图片并保存
|
||||
"""
|
||||
@ -171,20 +179,39 @@ class DoubanScraper:
|
||||
logger.info(f"正在下载{file_path.stem}图片:{url} ...")
|
||||
r = RequestUtils().get_res(url=url)
|
||||
if r:
|
||||
file_path.write_bytes(r.content)
|
||||
if self._transfer_type in ['rclone_move', 'rclone_copy']:
|
||||
self.__save_remove_file(file_path, r.content)
|
||||
else:
|
||||
file_path.write_bytes(r.content)
|
||||
logger.info(f"图片已保存:{file_path}")
|
||||
else:
|
||||
logger.info(f"{file_path.stem}图片下载失败,请检查网络连通性")
|
||||
except Exception as err:
|
||||
logger.error(f"{file_path.stem}图片下载失败:{err}")
|
||||
|
||||
@staticmethod
|
||||
def __save_nfo(doc, file_path: Path):
|
||||
def __save_nfo(self, doc, file_path: Path):
|
||||
"""
|
||||
保存NFO
|
||||
"""
|
||||
if file_path.exists():
|
||||
return
|
||||
xml_str = doc.toprettyxml(indent=" ", encoding="utf-8")
|
||||
file_path.write_bytes(xml_str)
|
||||
if self._transfer_type in ['rclone_move', 'rclone_copy']:
|
||||
self.__save_remove_file(file_path, xml_str)
|
||||
else:
|
||||
file_path.write_bytes(xml_str)
|
||||
logger.info(f"NFO文件已保存:{file_path}")
|
||||
|
||||
def __save_remove_file(self, out_file: Path, content: Union[str, bytes]):
|
||||
"""
|
||||
保存文件到远端
|
||||
"""
|
||||
temp_file = settings.TEMP_PATH / str(out_file)[1:]
|
||||
temp_file_dir = temp_file.parent
|
||||
if not temp_file_dir.exists():
|
||||
temp_file_dir.mkdir(parents=True, exist_ok=True)
|
||||
temp_file.write_bytes(content)
|
||||
if self._transfer_type == 'rclone_move':
|
||||
SystemUtils.rclone_move(temp_file, out_file)
|
||||
elif self._transfer_type == 'rclone_copy':
|
||||
SystemUtils.rclone_copy(temp_file, out_file)
|
||||
|
Reference in New Issue
Block a user