fix requests

This commit is contained in:
jxxghp
2024-04-10 22:16:10 +08:00
parent 294b4a6bf9
commit a3603f79c8
15 changed files with 567 additions and 556 deletions

View File

@ -30,17 +30,17 @@ class DoubanModule(_ModuleBase):
self.cache = DoubanCache()
def stop(self):
pass
self.doubanapi.close()
def test(self) -> Tuple[bool, str]:
"""
测试模块连接性
"""
ret = RequestUtils().get_res("https://movie.douban.com/")
if ret and ret.status_code == 200:
return True, ""
elif ret:
return False, f"无法连接豆瓣,错误码:{ret.status_code}"
with RequestUtils().get_res("https://movie.douban.com/") as ret:
if ret and ret.status_code == 200:
return True, ""
elif ret:
return False, f"无法连接豆瓣,错误码:{ret.status_code}"
return False, "豆瓣网络连接失败"
def init_setting(self) -> Tuple[str, Union[str, bool]]:

View File

@ -210,7 +210,7 @@ class DoubanApi(metaclass=Singleton):
},
data={
"apikey": "0ab215a8b1977939201640fa14c66bab",
},
}
)
"""
req_url = self._api_url + url
@ -481,6 +481,6 @@ class DoubanApi(metaclass=Singleton):
"""
self.__invoke.cache_clear()
def __del__(self):
def close(self):
if self._session:
self._session.close()

View File

@ -193,15 +193,15 @@ class DoubanScraper:
url = url.replace("/format/webp", "/format/jpg")
file_path.with_suffix(".jpg")
logger.info(f"正在下载{file_path.stem}图片:{url} ...")
r = RequestUtils().get_res(url=url)
if r:
if self._transfer_type in ['rclone_move', 'rclone_copy']:
self.__save_remove_file(file_path, r.content)
with RequestUtils().get_res(url=url) as r:
if r:
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:
file_path.write_bytes(r.content)
logger.info(f"图片已保存:{file_path}")
else:
logger.info(f"{file_path.stem}图片下载失败,请检查网络连通性")
logger.info(f"{file_path.stem}图片下载失败,请检查网络连通性")
except Exception as err:
logger.error(f"{file_path.stem}图片下载失败:{str(err)}")