diff --git a/utils/ImageUtils.py b/utils/FileUtils.py similarity index 93% rename from utils/ImageUtils.py rename to utils/FileUtils.py index 94dba42..ddfe1b7 100644 --- a/utils/ImageUtils.py +++ b/utils/FileUtils.py @@ -197,4 +197,18 @@ class imageUtils: print("解密成功=",save_path) if os.path.exists(imgpath): os.remove(imgpath) - print("remove=",imgpath) \ No newline at end of file + print("remove=",imgpath) + +class fileUtils: + @classmethod + def ver_file(cls,file_path,type): + if type == "image": + #验证是否是图像 + try: + img = Image.open(file_path) + img.verify() + return True + except: + os.remove(file_path) + print(f"{file_path}已损坏 type:{type},删除重试中") + return False \ No newline at end of file diff --git a/utils/downloader.py b/utils/downloader.py index 0e08b17..b5999a7 100644 --- a/utils/downloader.py +++ b/utils/downloader.py @@ -11,10 +11,10 @@ import os import concurrent.futures import requests import time -from PIL import Image from utils.Ntfy import ntfy from utils.comic.ComicInfo import comicInfo from utils.HtmlUtils import htmlUtils +from utils.FileUtils import fileUtils headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", @@ -26,7 +26,7 @@ headers = { } -def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, proxy=None): +def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, proxy=None,type="image"): proxies = None if proxy_type is not None: proxies = { @@ -53,15 +53,9 @@ def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, p f.write(response.content) response.close() #验证是否是图像 - try: - img = Image.open(temp_path) - img.verify() - except: - os.remove(temp_path) - print(f"{temp_path}已损坏,删除重试中") - return None - shutil.move(temp_path, file_path) - print("## OK: {} {}".format(file_path, image_url)) + if fileUtils.ver_file(temp_path,type=type): + shutil.move(temp_path, file_path) + print("## OK: {} {}".format(file_path, image_url)) except Exception as e: if response: response.close() diff --git a/utils/entity/BaseComicEntity.py b/utils/entity/BaseComicEntity.py index 9f86d40..b4b1ad0 100644 --- a/utils/entity/BaseComicEntity.py +++ b/utils/entity/BaseComicEntity.py @@ -1,6 +1,6 @@ import json,os,time,random,shutil from utils.HtmlUtils import htmlUtils -from utils.ImageUtils import imageUtils +from utils.FileUtils import imageUtils from utils.comic.ComicInfo import comicInfo from utils.CBZUtils import CBZUtils from utils.downloader import download_images diff --git a/utils/entity/JMTI.py b/utils/entity/JMTI.py index fd97044..2365dc8 100644 --- a/utils/entity/JMTI.py +++ b/utils/entity/JMTI.py @@ -2,7 +2,7 @@ import hashlib import json,os,time,random,shutil import re,math from utils.HtmlUtils import htmlUtils -from utils.ImageUtils import imageUtils +from utils.FileUtils import imageUtils from utils.comic.ComicInfo import comicInfo from utils.CBZUtils import CBZUtils from utils.CBZUtils import verUtils diff --git a/utils/entity/down/JM.py b/utils/entity/down/JM.py index 3ebdb81..c53a900 100644 --- a/utils/entity/down/JM.py +++ b/utils/entity/down/JM.py @@ -2,7 +2,7 @@ import hashlib import json import os from utils.HtmlUtils import htmlUtils -from utils.ImageUtils import imageUtils +from utils.FileUtils import imageUtils from utils.comic.ComicInfo import comicInfo from utils.Ntfy import ntfy diff --git a/utils/entity/down/RouMan.py b/utils/entity/down/RouMan.py index a30c5e8..38a0878 100644 --- a/utils/entity/down/RouMan.py +++ b/utils/entity/down/RouMan.py @@ -1,6 +1,6 @@ import json from utils.HtmlUtils import htmlUtils -from utils.ImageUtils import imageUtils +from utils.FileUtils import imageUtils from utils.comic.ComicInfo import comicInfo from utils.Ntfy import ntfy