fix #426 刮削下载图片重试

This commit is contained in:
jxxghp 2023-09-04 10:14:05 +08:00
parent 7eb77875f1
commit 96f17e2bc2
2 changed files with 39 additions and 1 deletions

View File

@ -2,11 +2,14 @@ import time
from pathlib import Path from pathlib import Path
from xml.dom import minidom from xml.dom import minidom
from requests import RequestException
from app.core.config import settings from app.core.config import settings
from app.core.context import MediaInfo from app.core.context import MediaInfo
from app.core.metainfo import MetaInfo from app.core.metainfo import MetaInfo
from app.log import logger from app.log import logger
from app.schemas.types import MediaType from app.schemas.types import MediaType
from app.utils.common import retry
from app.utils.dom import DomUtils from app.utils.dom import DomUtils
from app.utils.http import RequestUtils from app.utils.http import RequestUtils
@ -312,6 +315,7 @@ class TmdbScraper:
self.__save_nfo(doc, file_path.with_suffix(".nfo")) self.__save_nfo(doc, file_path.with_suffix(".nfo"))
@staticmethod @staticmethod
@retry(RequestException, logger=logger)
def __save_image(url: str, file_path: Path): def __save_image(url: str, file_path: Path):
""" """
下载图片并保存 下载图片并保存
@ -320,7 +324,7 @@ class TmdbScraper:
return return
try: try:
logger.info(f"正在下载{file_path.stem}图片:{url} ...") logger.info(f"正在下载{file_path.stem}图片:{url} ...")
r = RequestUtils().get_res(url=url) r = RequestUtils().get_res(url=url, raise_exception=True)
if r: if r:
file_path.write_bytes(r.content) file_path.write_bytes(r.content)
logger.info(f"图片已保存:{file_path}") logger.info(f"图片已保存:{file_path}")

34
app/utils/common.py Normal file
View File

@ -0,0 +1,34 @@
import time
from typing import Any
def retry(ExceptionToCheck: Any,
tries: int = 3, delay: int = 3, backoff: int = 2, logger: Any = None):
"""
:param ExceptionToCheck: 需要捕获的异常
:param tries: 重试次数
:param delay: 延迟时间
:param backoff: 延迟倍数
:param logger: 日志对象
"""
def deco_retry(f):
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay
while mtries > 1:
try:
return f(*args, **kwargs)
except ExceptionToCheck as e:
msg = f"{str(e)}, {mdelay} 秒后重试 ..."
if logger:
logger.warn(msg)
else:
print(msg)
time.sleep(mdelay)
mtries -= 1
mdelay *= backoff
return f(*args, **kwargs)
return f_retry
return deco_retry