This commit is contained in:
caiwx86 2022-12-13 06:36:57 +08:00
commit 9c9ce77e71
6 changed files with 29 additions and 14 deletions

View File

@ -2,6 +2,7 @@ import os
from pathlib import Path from pathlib import Path
from zipfile import ZipFile from zipfile import ZipFile
from utils.comic.ComicInfo import comicInfo from utils.comic.ComicInfo import comicInfo
from utils.Ntfy import ntfy
class CBZUtils: class CBZUtils:
@ -35,7 +36,7 @@ class CBZUtils:
print("打包中:" + str(y) + "/" + str(len(filenames)), os.path.join(source_dir, filename)) print("打包中:" + str(y) + "/" + str(len(filenames)), os.path.join(source_dir, filename))
zf.write(path.joinpath(filename), arc_dir.joinpath(filename)) zf.write(path.joinpath(filename), arc_dir.joinpath(filename))
zf.close() zf.close()
print("打包完成:", target_file) ntfy.sendMsg(f"打包完成:{target_file}")
#md5_file = md5(target_file) #md5_file = md5(target_file)
#print("md5:", md5_file) #print("md5:", md5_file)
#msg[target_file] = md5_file #msg[target_file] = md5_file

View File

@ -5,6 +5,7 @@ import traceback
import time import time
from urllib3.util.retry import Retry from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from utils.Ntfy import ntfy
class htmlUtils: class htmlUtils:
headers = {'User-Agent': UserAgent().random} headers = {'User-Agent': UserAgent().random}
@ -27,20 +28,20 @@ class htmlUtils:
while url_text == None: while url_text == None:
if count <= 10: if count <= 10:
try: try:
print("请求地址:",curl) print(f"请求地址:{curl}")
res = s.get(curl,stream=True, headers=cls.headers, timeout=50) res = s.get(curl,stream=True, headers=cls.headers, timeout=50)
if type == "bytes": if type == "bytes":
url_text = res url_text = res
else: else:
url_text = html.fromstring(res.text) url_text = html.fromstring(res.text)
except: except:
print(f'Retry! 第{count}') ntfy.sendMsg(f'Retry! 第{count}')
time.sleep(3) time.sleep(3)
traceback.print_exc() traceback.print_exc()
count += 1 count += 1
continue continue
else: else:
print(f"fail 请求失败:{curl}") ntfy.sendMsg(f"fail 请求失败:{curl}")
break break
if type == "None": if type == "None":
data = { curl : url_text} data = { curl : url_text}
@ -60,7 +61,7 @@ class htmlUtils:
data_json = res.json() data_json = res.json()
return data_json return data_json
except: except:
print(f"请求失败,重试中...") ntfy.sendMsg(f"请求失败,重试中... {curl}")
count += 1 count += 1
time.sleep(1) time.sleep(1)
cls.getJSON(curl) cls.getJSON(curl)

View File

@ -91,4 +91,4 @@ class netUtils:
save_path = os.path.join(pathCBZComic,comicInfo.getChapter()+icon_su) save_path = os.path.join(pathCBZComic,comicInfo.getChapter()+icon_su)
shutil.copy(pathComicIcon, save_path) shutil.copy(pathComicIcon, save_path)
print(f"{pathComicIcon} 已复制至: {save_path}") print(f"{pathComicIcon} 已复制至: {save_path}")
comicInfo.nextDownloadToCBZChapter() comicInfo.nextDownloadToCBZChapter()

8
utils/Ntfy.py Normal file
View File

@ -0,0 +1,8 @@
import requests
class ntfy:
@classmethod
def sendMsg(cls, msg):
print(f"#ntfy: {msg}")
requests.post("https://ntfy.caiwenxiu.cn/PyComic",
data=msg.encode(encoding='utf-8'))

View File

@ -11,6 +11,7 @@ import os
import concurrent.futures import concurrent.futures
import requests import requests
import time import time
from utils.Ntfy import ntfy
headers = { headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
@ -39,7 +40,7 @@ def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, p
image_url, headers=headers, timeout=timeout, proxies=proxies) image_url, headers=headers, timeout=timeout, proxies=proxies)
while response.status_code != 200 and repair_count <= 5: while response.status_code != 200 and repair_count <= 5:
download_image(image_url,dst_dir,file_name) download_image(image_url,dst_dir,file_name)
print(f'重试:第{repair_count}{image_url}') ntfy.sendMsg(f'重试:第{repair_count}{image_url}')
repair_count += 1 repair_count += 1
with open(temp_path, 'wb') as f: with open(temp_path, 'wb') as f:
f.write(response.content) f.write(response.content)
@ -51,7 +52,7 @@ def download_image(image_url, dst_dir, file_name, timeout=20, proxy_type=None, p
response.close() response.close()
if repair_count < 5: if repair_count < 5:
time.sleep(1) time.sleep(1)
print("## Repeat: {} {}".format(image_url)) ntfy.sendMsg("## Repeat: {} {}".format(image_url))
download_image(image_url,dst_dir,file_name) download_image(image_url,dst_dir,file_name)
repair_count += 1 repair_count += 1
else: else:

View File

@ -5,6 +5,7 @@ from utils.ImageUtils import imageUtils
from utils.comic.ComicInfo import comicInfo from utils.comic.ComicInfo import comicInfo
from utils.CBZUtils import CBZUtils from utils.CBZUtils import CBZUtils
from utils.downloader import download_images from utils.downloader import download_images
from utils.Ntfy import ntfy
class comicEntity: class comicEntity:
@ -29,13 +30,15 @@ class comicEntity:
book_name = book.get("name") book_name = book.get("name")
updatedAt = book.get("updatedAt") updatedAt = book.get("updatedAt")
comicHref = baseUrl+"/books/"+book_id comicHref = baseUrl+"/books/"+book_id
random_int = random.uniform(5,20) random_int = random.randint(5,20)
comicInfo.setComicName(book_name) comicInfo.setComicName(book_name)
dirConfComic = comicInfo.getDirConfComic() dirConfComic = comicInfo.getDirConfComic()
if not os.path.exists(dirConfComic): if not os.path.exists(dirConfComic):
print(f"{random_int}秒后开始下载 漫画:{book_name}") ntfy.sendMsg(f"{random_int}秒后开始下载 漫画:{book_name}")
time.sleep(random_int) time.sleep(random_int)
cls.oneComic(comicHref, random.uniform(0,3)) else:
ntfy.sendMsg(f"已存在 漫画:{book_name}")
cls.oneComic(comicHref, random.uniform(0,3))
print(books) print(books)
#for comicHref in comicsHref: #for comicHref in comicsHref:
@ -76,7 +79,7 @@ class comicEntity:
chapter = chapters[count_chapter] chapter = chapters[count_chapter]
comicInfo.setChapterName(chapter) comicInfo.setChapterName(chapter)
if not comicInfo.nextExistsGetPath("done_"): if not comicInfo.nextExistsGetPath("done_"):
comicEntity.comicChapter(href,scramble=True,sleep=random.uniform(15,25)) comicEntity.comicChapter(href,scramble=True,sleep=random.randint(15,25))
count_chapter += 1 count_chapter += 1
#一本漫画下载后等待 #一本漫画下载后等待
#清空文件夹 #清空文件夹
@ -113,7 +116,7 @@ class comicEntity:
shutil.rmtree(dirComicChapter) shutil.rmtree(dirComicChapter)
cls.comicChapter(chapter_url,scramble,sleep) cls.comicChapter(chapter_url,scramble,sleep)
if not sleep == None: if not sleep == None:
print(f"{sleep} 秒后开始下载下一个章节") ntfy.sendMsg(f"{sleep} 秒后开始下载下一个章节")
time.sleep(sleep) time.sleep(sleep)
@classmethod @classmethod
@ -149,6 +152,7 @@ class comicEntity:
description = x.get("description") description = x.get("description")
images = x.get("images") images = x.get("images")
chapterAPIPath = x.get("chapterAPIPath") chapterAPIPath = x.get("chapterAPIPath")
ntfy.sendMsg(f"{bookName} {chapterName} 下载中")
comicInfo.setComicName(bookName) comicInfo.setComicName(bookName)
comicInfo.setChapterName(chapterName) comicInfo.setChapterName(chapterName)
comicInfo.setDep(description) comicInfo.setDep(description)
@ -165,7 +169,7 @@ class comicEntity:
chapterName = data.get("name") chapterName = data.get("name")
images = data.get("images") images = data.get("images")
if images == None: if images == None:
print(f"未获取到章节图像 comic_name={bookName} chapter={chapterName}") ntfy.sendMsg(f"未获取到章节图像 comic_name={bookName} chapter={chapterName}")
totalChapter = x.get("totalChapter") totalChapter = x.get("totalChapter")
tags = x.get("tags") tags = x.get("tags")
x = tags x = tags