import hashlib import json import os from utils.HtmlUtils import htmlUtils from utils.FileUtils import imageUtils from utils.comic.ComicInfo import comicInfo from utils.Ntfy import ntfy class comicCommon: @classmethod def comicChapterDownload(cls,chapter_url): chapter_url = chapter_url+"?shunt=2" img_list =htmlUtils.xpathData("//div[@class='panel-body']/div/div[contains(@class,'center scramble-page')]/img/@data-original",url=chapter_url,update=True) pages_imgs =htmlUtils.xpathData("//div[@class='center scramble-page']/@id",url=chapter_url) comicInfo.setPages(pages_imgs) comicInfo.writeComicInfoXML(comicInfo.str_chapter) #print("img_list:",len(img_list)) list_img = [] list_file_name = [] for i in img_list: img_url= i img_name = os.path.basename(img_url).split('.')[0] aid = int(comicInfo.getValue1()) if aid > 220980: #if is_scramble: img_name = "scramble="+str(cls.get_scramble_num(aid,img_name))+"_"+img_name #path_img = "%s\\%s.jpg" % (cls.aid, img_name) path_img = "%s.jpg" % (img_name) list_img.append(img_url) list_file_name.append(path_img) comicInfo.comicChapterDownload(list_img,list_file_name) @classmethod def get_md5(cls,num): result1 = hashlib.md5(num.encode()).hexdigest() print('get_md5-', result1) return result1 @classmethod def get_scramble_num(cls,e, t): #print(type(e),e, type(t),t) a = 10 try: num_dict = {} for i in range(10): num_dict[i] = i * 2 + 2 if (int(e) >= 268850): n = str(e) + t; # switch(n=(n = (n = md5(n)).substr(-1)), n %= 10) { #print("n=",n) tmp = ord(cls.get_md5(n)[-1]) result = num_dict[tmp % 10] a = result return a except Exception as e: print(e.__traceback__.tb_lineno,e) return False @classmethod def encode_scramble_image(cls,imgpath): image = Image.open(imgpath) w, h = image.size #image.show() file_str = str(imgpath).split("=") #10_29.jpg base_dir = file_str[0].replace("scramble","") base_name = file_str[-1] base_fn = base_name.split("_") save_name = base_fn[1] save_name_delesu = save_name.split(".")[0] blocks = int(base_fn[0]) img_type = os.path.basename(imgpath).split('.')[-1] save_path = os.path.join(os.path.dirname(imgpath),save_name_delesu+"."+img_type) # print(type(aid),type(img_name)) if blocks: s = blocks # 随机值 # print(s) l = h % s # 切割最后多余的值 box_list = [] hz = 0 for i in range(s): c = math.floor(h / s) g = i * c hz += c h2 = h - c * (i + 1) - l if i == 0: c += l;hz += l else: g += l box_list.append((0, h2, w, h - g)) # print(box_list,len(box_list)) item_width = w # box_list.reverse() #还原切图可以倒序列表 # print(box_list, len(box_list)) newh = 0 image_list = [image.crop(box) for box in box_list] # print(box_list) newimage = Image.new("RGB", (w, h)) for image in image_list: # image.show() b_w, b_h = image.size newimage.paste(image, (0, newh)) newh += b_h newimage.save(save_path) if os.path.exists(imgpath): os.remove(imgpath)