def download_interactive(): id = int(input('请输入B站漫画id(整数,不带mc前缀):')) path = input('请输入保存路径(默认当前目录):') pdf = input('下载后是否合并为一个pdf(y/n):') if not path: path = os.getcwd() path = path.replace('\\', '/') if configData: mag = MangaDownloader(id, configData["users"][0]["cookieDatas"]) else: mag = MangaDownloader(id) print(f'开始下载漫画 "{mag.getTitle()}"') mag.downloadAll(path) print('下载任务结束') if pdf.upper() == 'Y': import fitz, glob print("正在合并下载图片为pdf") title = mag.getTitle() if path[-1] == '/': path = f'{path}{title}' else: path = fr'{path}/{title}' doc = fitz.open() for name in glob.glob(f'{path}/*/*.jpg'): imgdoc = fitz.open(name) pdfbytes = imgdoc.convertToPDF() imgpdf = fitz.open("pdf", pdfbytes) doc.insertPDF(imgpdf) doc.save(f'{path}/{title}.pdf') print(f'文件保存至{path}/{title}.pdf')
def download_interactive(mag: MangaDownloader): id = int(input('请输入B站漫画id(整数,不带mc前缀):')) path = input('请输入保存路径(默认当前目录):') pdf = input('下载后是否合并为一个pdf(y/n):') if not path: path = os.getcwd() mag.setComicId(id) print(f'开始下载漫画 "{mag.getTitle()}"') for ret in mag.downloadAll(path): print_format(ret.name, '<', 30) if ret.code == MangaDownloader.DownloadCode.Ok: print(' 下载成功') elif ret.code == MangaDownloader.DownloadCode.Locked: print(' 没有解锁') elif ret.code == MangaDownloader.DownloadCode.Error: print(' 下载失败') print('下载任务结束') if pdf.upper() == 'Y': import fitz, glob print("正在合并下载图片为pdf") title = mag.getTitle() path = os.path.join(path, title) doc = fitz.open() for name in glob.glob(os.path.join(path, "*", "*.jpg")): imgdoc = fitz.open(name) pdfbytes = imgdoc.convertToPDF() imgpdf = fitz.open("pdf", pdfbytes) doc.insertPDF(imgpdf) path = os.path.join(path, title + ".pdf") doc.save(path) print(f'文件保存至{path}')
def download_task(mag: MangaDownloader, params): title = mag.getTitle() print(f'开始下载漫画 "{title}"') if params["episode"]: ep_list = mag.getIndex() ep_len = len(ep_list) ep_P = set() for P in params["episode"].split(','): if '-' in P: start, end = P.split('-') for i in range(int(start), int(end) + 1): if i <= ep_len: ep_P.add(i - 1) else: if int(P) <= ep_len: ep_P.add(int(P) - 1) download = mag.downloadIndexes(ep_P, params["path"]) else: download = mag.downloadAll(params["path"]) for ret in download: print_format(ret.name, '<', 30) if ret.code == MangaDownloader.DownloadCode.Ok: print(' 下载成功') elif ret.code == MangaDownloader.DownloadCode.Locked: print(' 没有解锁') elif ret.code == MangaDownloader.DownloadCode.Error: print(' 下载失败') print('下载任务结束')
def download_task(task, path: str): if configData: mag = MangaDownloader(task[0], configData["users"][0]["cookieDatas"]) else: mag = MangaDownloader(task[0]) title = mag.getTitle() print(f'开始下载漫画 "{title}"') if task[1] == 'a': mag.downloadAll(path) else: ep_list = mag.getIndex() ep_len = len(ep_list) ep_P = set() for P in task[1].split(','): if '-' in P: start, end = P.split('-') for i in range(int(start), int(end) + 1): if i <= ep_len: ep_P.add(i - 1) else: if int(P) <= ep_len: ep_P.add(int(P) - 1) if path.endswith('/'): dpath = path + title else: dpath = path + '/' + title os.makedirs(dpath) bq = len(str(mag.getNum())) for x in ep_P: name = ep_list[x]["title"] if name.replace(' ', '') == '': name = ep_list[x]["short_title"] if not ep_list[x]["is_locked"]: mag.download(ep_list[x]['id'], f'{dpath}/{ep_list[x]["ord"]:0>{bq}}-{name}') print(f'{ep_list[x]["ord"]:0>{bq}}-{name} 下载完成') else: print(f'{ep_list[x]["ord"]:0>{bq}}-{name} 目前需要解锁') print('下载任务结束') if task[2]: import fitz, glob print("正在合并下载图片为pdf") if path.endswith('/'): dpath = path + title else: dpath = path + '/' + title doc = fitz.open() for name in glob.glob(f'{dpath}/*/*.jpg'): imgdoc = fitz.open(name) pdfbytes = imgdoc.convertToPDF() imgpdf = fitz.open("pdf", pdfbytes) doc.insertPDF(imgpdf) doc.save(f'{dpath}/{title}.pdf') print(f'文件保存至{dpath}/{title}.pdf')
def main(*args, **kwargs): interactive_mode = not (kwargs["manga"] or kwargs["pdf"]) biliapi = bili() if interactive_mode or kwargs["manga"]: if path: with open(path, 'r', encoding='utf-8-sig') as fp: userData = load(fp) if userData["SESSDATA"] and \ biliapi.login_by_cookie({"SESSDATA": userData["SESSDATA"]}): ... elif userData["access_token"] and \ userData["refresh_token"] and \ biliapi.login_by_access_token(userData["access_token"], userData["refresh_token"], True): userData["SESSDATA"] = biliapi.SESSDATA userData["bili_jct"] = biliapi.bili_jct userData["access_token"] = biliapi.access_token userData["refresh_token"] = biliapi.refresh_token with open(path, 'w', encoding='utf-8') as fp: dump(userData, fp, ensure_ascii=False, indent=4) elif userData["username"] and \ userData["password"] and \ biliapi.login_by_password(userData["username"], userData["password"]): userData["SESSDATA"] = biliapi.SESSDATA userData["bili_jct"] = biliapi.bili_jct userData["access_token"] = biliapi.access_token userData["refresh_token"] = biliapi.refresh_token with open(path, 'w', encoding='utf-8') as fp: dump(userData, fp, ensure_ascii=False, indent=4) else: print("当前处于未登录状态") else: print("当前处于未登录状态") manga = MangaDownloader(biliapi) if interactive_mode: download_interactive(manga) else: if kwargs["manga"]: manga.setComicId(kwargs["manga"]) download_task(manga, kwargs) kwargs["path"] = os.path.join(kwargs["path"], manga.getTitle()) if kwargs["pdf"]: print("正在合并下载图片为pdf") manga_to_PDF(kwargs["path"], kwargs["split"], kwargs["width"], kwargs["height"]) print("合并完成")
def download_task(mag: MangaDownloader, task, path: str): mag.setComicId(task[0]) title = mag.getTitle() print(f'开始下载漫画 "{title}"') if task[1] == 'a': mag.downloadAll(path) else: ep_list = mag.getIndex() ep_len = len(ep_list) ep_P = set() for P in task[1].split(','): if '-' in P: start, end = P.split('-') for i in range(int(start), int(end) + 1): if i <= ep_len: ep_P.add(i - 1) else: if int(P) <= ep_len: ep_P.add(int(P) - 1) for ret in mag.downloadIndexes(ep_P, path): print_format(ret.name, '<', 30) if ret.code == MangaDownloader.DownloadCode.Ok: print(' 下载成功') elif ret.code == MangaDownloader.DownloadCode.Locked: print(' 没有解锁') elif ret.code == MangaDownloader.DownloadCode.Error: print(' 下载失败') print('下载任务结束') if task[2]: import fitz, glob print("正在合并下载图片为pdf") path = os.path.join(path, title) doc = fitz.open() for name in glob.glob(os.path.join(path, "*", "*.jpg")): imgdoc = fitz.open(name) pdfbytes = imgdoc.convertToPDF() imgpdf = fitz.open("pdf", pdfbytes) doc.insertPDF(imgpdf) path = os.path.join(path, title + ".pdf") doc.save(path) print(f'文件保存至{path}')
def download_task(task, path: str): if configData: mag = MangaDownloader(task[0], configData["users"][0]["cookieDatas"]) else: mag = MangaDownloader(task[0]) title = mag.getTitle() print(f'开始下载漫画 "{title}"') if task[1] == 'a': mag.downloadAll(path) else: ep_list = mag.getIndex() ep_len = len(ep_list) ep_P = set() for P in task[1].split(','): if '-' in P: start, end = P.split('-') for i in range(int(start), int(end) + 1): if i <= ep_len: ep_P.add(i - 1) else: if int(P) <= ep_len: ep_P.add(int(P) - 1) mag.downloadIndexes(ep_P, path) print('下载任务结束') if task[2]: import fitz, glob print("正在合并下载图片为pdf") path = os.path.join(path, title) doc = fitz.open() for name in glob.glob(f'{path}/*/*.jpg'): imgdoc = fitz.open(name) pdfbytes = imgdoc.convertToPDF() imgpdf = fitz.open("pdf", pdfbytes) doc.insertPDF(imgpdf) doc.save(f'{path}/{title}.pdf') print(f'文件保存至{path}/{title}.pdf')