94 lines
3.7 KiB
Python
94 lines
3.7 KiB
Python
import os.path,shutil
|
|
import requests
|
|
from concurrent.futures import ThreadPoolExecutor,wait,ALL_COMPLETED
|
|
import time,random
|
|
import imghdr
|
|
from utils.ImageUtils import imageUtils
|
|
from utils.comic.ComicInfo import comicInfo
|
|
from utils.HtmlUtils import htmlUtils
|
|
from utils.downloader import download_image
|
|
|
|
class netUtils:
|
|
|
|
@classmethod
|
|
# 定义下载函数
|
|
def download(cls,url,path,fileType=None):
|
|
if os.path.exists(path):
|
|
if imghdr.what(path):
|
|
msg = "已存在同路径文件,已跳过:"+path
|
|
print(msg)
|
|
return msg
|
|
else:
|
|
print("文件已损坏,已重试:"+path)
|
|
tmp_file = path+".downloads"
|
|
if os.path.exists(tmp_file):
|
|
os.remove(tmp_file)
|
|
print("存在缓存文件,已删除:",tmp_file)
|
|
repair_count = 1
|
|
res = htmlUtils.getBytes(url)
|
|
while res.status_code != 200 and repair_count <= 5:
|
|
res = htmlUtils.getBytes(url)
|
|
print(f'重试:第{repair_count}次 {url}')
|
|
repair_count += 1
|
|
#判断是否为图片
|
|
if fileType == "image":
|
|
if 'image' not in res.headers.get("content-type",""):
|
|
print(f"url= {url} Error: URL doesnot appear to be an image")
|
|
basedir= os.path.dirname(path)
|
|
if not os.path.exists(basedir):
|
|
os.makedirs(basedir)
|
|
#expected_length = res.headers.get('Content-Length')
|
|
#actual_length = res.raw.tell()
|
|
with open(tmp_file, 'wb') as f:
|
|
for ch in res:
|
|
f.write(ch)
|
|
f.close()
|
|
shutil.move(tmp_file, path)
|
|
print(f"url={url} 保存至:{path}")
|
|
return path
|
|
|
|
@classmethod
|
|
def threadDownload(cls,url,path,fileType=None):
|
|
executor = ThreadPoolExecutor(max_workers=3)
|
|
tasks = executor.submit(cls.download, url,path,fileType)
|
|
time.sleep(random.uniform(0,1))
|
|
#wait(tasks, return_when=ALL_COMPLETED)
|
|
|
|
@classmethod
|
|
def downloadComicChapterImages(cls, imgs,scrambles=None):
|
|
file_path = comicInfo.getDirComicChapter()
|
|
print("files=",file_path)
|
|
|
|
count_img = 1
|
|
for img in imgs:
|
|
count = ("{:0>3d}".format(count_img))
|
|
file_name = count + os.path.splitext(img)[-1]
|
|
save_file_path = os.path.join(file_path, file_name)
|
|
if scrambles[count_img -1]:
|
|
su = "."+str(img).split(".")[-1]
|
|
de_str = str(img).split("/")[-1].replace(su,"==")
|
|
blockInt = imageUtils.encodeImage(de_str)
|
|
save_file_path = os.path.join(file_path,"scramble="+str(blockInt)+"_"+file_name)
|
|
cls.threadDownload(img, save_file_path, fileType="image")
|
|
count_img += 1
|
|
return os.path.dirname(save_file_path)
|
|
|
|
@classmethod
|
|
def downloadComicIcon(cls):
|
|
icon_url = comicInfo.getIcon()
|
|
if icon_url == None:
|
|
print("icon 不存在,已跳过")
|
|
return None
|
|
save_name = "cover"
|
|
icon_su = "."+str(icon_url).split(".")[-1]
|
|
#判断漫画名路径是否已存在comicname/cover.jpg, 存在跳过
|
|
pathComicIcon = os.path.join(comicInfo.getDirConfComic(),save_name+icon_su)
|
|
if not os.path.exists(pathComicIcon):
|
|
cls.download(icon_url, pathComicIcon)
|
|
pathCBZComic = comicInfo.getDirCBZComic()
|
|
if not os.path.exists(pathCBZComic):
|
|
os.makedirs(pathCBZComic)
|
|
save_path = os.path.join(pathCBZComic,comicInfo.getChapter()+icon_su)
|
|
shutil.copy(pathComicIcon, save_path)
|
|
print(f"{pathComicIcon} 已复制至: {save_path}")
|
|
comicInfo.nextDownloadToCBZChapter() |