PyComicPackRouMan/utils/ComicUtils.py
2022-12-04 22:29:04 +08:00

88 lines
3.0 KiB
Python

from fake_useragent import UserAgent
import requests, os, shutil,re,json
from lxml import html
from utils.comic.ComicStr import comicStr
from utils.FileUtils import fileUtils
class comicUtils:
comic_title = None
temp_url = ""
@classmethod
def setKeyValue(cls,name, c_value,bool_list=None):
count = 0
len_value = len(c_value)
if bool_list:
count += 1
if cls.comics.get(count) == None:
cls.comics[count] = {name: c_value}
else:
cls.comics[count].update({name: c_value})
else:
for x in range(0, len_value):
value = c_value[x]
count += 1
if cls.comics.get(count) == None:
cls.comics[count] = {name: value}
else:
cls.comics[count].update({name: value})
@classmethod
def listImgDownload(cls,save_dir_name, links):
print("save_dir_name:", save_dir_name)
print("list_link:", links)
len_links = len(links)
count = 1
for link in links:
print("link:",count,":", link)
file_cout = ("{:0>3d}".format(count))
file_path = os.path.join("Comic", save_dir_name,str(file_cout)+".jpg")
netUtils.download(link, file_path)
count += 1
@classmethod
def downloadComic(cls,comic_name):
comic = fileUtils.read_comic(comic_name)
list_img = comic.get(comicStr.list_img)
len_list_img = len(list_img)
icon = comic.get(comicStr.icon)
for x in range(0, len_list_img):
chapter_img = list_img[x]
keys = chapter_img.keys()
for key in keys:
if not icon == None:
icon = netUtils.downloadComicIcon(comic_name,key,icon)
else:
print("icon已跳过")
netUtils.downloadComicChapterImages(comic_name, key, chapter_img[key])
'''
传入 comic_name读取配置文件中的各类信息
输出 "ComicInfo.xml" 并将图片打包成CBZ
'''
@classmethod
def packComicCBZ(cls,comic_name):
data = fileUtils.read_comic(comic_name)
title = data.get(comicStr.title)
author = data.get(comicStr.author)
author = str(author).replace("/",",").replace(" ","")
dep = data.get(comicStr.dep)
chapters = data.get(comicStr.chapters)
tags = "韩漫"
c_publisher = "韩漫"
print("title:", title,"author=",author,"dep=",dep,"chapters:",chapters)
for chapter in chapters:
CBZUtils.writeComicInfoXML(title, chapter, dep, author,
tags, c_publisher)
CBZUtils.packComicCBZ(title,chapter)
'''
获取网站主页
'''
@classmethod
def getBaseUrl(cls,url):
num = 3
index = 0
for x in range(0, num):
index = str(url).find("/",index)+1
return url[0:index-1]