From c98d89bde17d16c426a963dfcf74ec81c319deb8 Mon Sep 17 00:00:00 2001 From: shingyu Date: Sat, 23 Mar 2024 02:19:58 +0800 Subject: [PATCH] feat: 3.0.0b MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复epub更新两个元信息问题 为请求增加超时处理 --- src/function.py | 22 +++++++++++----------- src/main.py | 2 +- src/public.py | 16 +++++++++++++--- src/qimao_batch.py | 4 ++++ src/qimao_chapter.py | 4 ++++ src/qimao_epub.py | 8 ++++++-- src/qimao_normal.py | 5 ++++- src/qimao_update.py | 20 ++++++++------------ 8 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/function.py b/src/function.py index b97d8fa..82a4a4a 100644 --- a/src/function.py +++ b/src/function.py @@ -8,6 +8,7 @@ import re import time import requests +from requests.exceptions import Timeout import platform from sys import exit from packaging import version @@ -31,10 +32,6 @@ os.makedirs(data_path, exist_ok=True) book_id = None start_chapter_id = "0" -proxies = { - "http": None, - "https": None -} # 用户须知 @@ -146,7 +143,7 @@ def start(): clear_screen() contributors_url = 'https://gitee.com/xingyv1024/7mao-novel-downloader/raw/main/CONTRIBUTORS.md' try: - contributors = requests.get(contributors_url, timeout=5, proxies=proxies) + contributors = requests.get(contributors_url, timeout=5, proxies=p.proxies) # 检查响应状态码 if contributors.status_code == 200: @@ -449,7 +446,7 @@ def check_update(now_version): # noinspection PyBroadException try: # 发送GET请求以获取最新的发行版信息 - response = requests.get(api_url, timeout=5, proxies=proxies) + response = requests.get(api_url, timeout=5, proxies=p.proxies) if response.status_code != 200: print(f"请求失败,状态码:{response.status_code}") @@ -551,7 +548,7 @@ def check_eula(): eula_date_old = eula_txt.splitlines()[5] # noinspection PyBroadException try: - eula_text = requests.get(eula_url, timeout=10, proxies=proxies).text + eula_text = requests.get(eula_url, timeout=10, proxies=p.proxies).text except Exception: print("获取最终用户许可协议失败,请检查网络连接") input("按Enter键继续...\n") @@ -597,9 +594,9 @@ def check_eula(): def agree_eula(): # noinspection PyBroadException try: - eula_text = requests.get(eula_url, timeout=10, proxies=proxies).text - license_text = requests.get(license_url, timeout=10, proxies=proxies).text - license_text_zh = requests.get(license_url_zh, timeout=10, proxies=proxies).text + eula_text = requests.get(eula_url, timeout=10, proxies=p.proxies).text + license_text = requests.get(license_url, timeout=10, proxies=p.proxies).text + license_text_zh = requests.get(license_url_zh, timeout=10, proxies=p.proxies).text except Exception: print("获取最终用户许可协议失败,请检查网络连接") input("按Enter键继续...\n") @@ -665,7 +662,7 @@ def search(): 'is_short_story_user': '0' } response = requests.get("https://api-bc.wtzw.com/search/v1/words", params=p.sign_url_params(params_), - headers=p.get_headers("00000000")).json() + headers=p.get_headers("00000000"), timeout=10).json() books = response['data']['books'] for i, book in enumerate(books): @@ -692,6 +689,9 @@ def search(): continue except KeyboardInterrupt: return + except Timeout: + print("请求超时,请检查网络连接") + return # except Exception as e: # print(f"发生错误: {e}") # return diff --git a/src/main.py b/src/main.py index 874bec0..d0113ad 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,7 @@ import function as f from sys import exit -version = "v3.0.0-alpha" +version = "v3.0.0-beta" # 检查另一个实例是否正在运行 f.check_instance() diff --git a/src/public.py b/src/public.py index 1752bdf..ab5175d 100644 --- a/src/public.py +++ b/src/public.py @@ -38,6 +38,12 @@ init(autoreset=True) +proxies = { + "http": None, + "https": None +} + + def get_headers(book_id): version_list = [ @@ -256,7 +262,8 @@ def send_request(book_id, chapter_id): params = sign_url_params(params) - response = requests.get(f"https://api-ks.wtzw.com/api/v1/chapter/content", headers=headers, params=params) + response = requests.get(f"https://api-ks.wtzw.com/api/v1/chapter/content", headers=headers, params=params, + proxies=proxies, timeout=10) return response.json() @@ -274,7 +281,8 @@ def get_book_info(url, mode="normal"): # 提取ID book_id = re.search(r"/(\d+)/", url).group(1) # 请求API - info = requests.get(f"https://api-bc.wtzw.com/api/v1/reader/detail?id={book_id}").json() + info = requests.get(f"https://api-bc.wtzw.com/api/v1/reader/detail?id={book_id}", proxies=proxies, + timeout=12).json() # 提取信息 title = info["data"]["title"] @@ -304,7 +312,9 @@ def get_book_info(url, mode="normal"): } response = requests.get("https://api-ks.wtzw.com/api/v1/chapter/chapter-list", params=sign_url_params(params), - headers=get_headers(book_id)).json() + headers=get_headers(book_id), + proxies=proxies, + timeout=12).json() # { # "data": { diff --git a/src/qimao_batch.py b/src/qimao_batch.py index b263089..2b8783a 100644 --- a/src/qimao_batch.py +++ b/src/qimao_batch.py @@ -10,6 +10,7 @@ import hashlib import public as p from colorama import Fore, Style, init +from requests.exceptions import Timeout init(autoreset=True) @@ -132,6 +133,9 @@ def download_novels(url, encoding, folder_path, data_folder): # 调用异步函数获取7猫信息(模拟浏览器) try: title, content, chapters = p.get_book_info(url) + except Timeout: + print(Fore.RED + Style.BRIGHT + "连接超时,请检查网络连接是否正常。") + return except Exception as e: print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}") return diff --git a/src/qimao_chapter.py b/src/qimao_chapter.py index c96d425..bc3d0b2 100644 --- a/src/qimao_chapter.py +++ b/src/qimao_chapter.py @@ -6,6 +6,7 @@ from tqdm import tqdm import public as p from colorama import Fore, Style, init +from requests.exceptions import Timeout init(autoreset=True) @@ -18,6 +19,9 @@ def qimao_c(url, encoding, path_choice, start_chapter_id, # 调用异步函数获取7猫信息(模拟浏览器) try: title, introduction, chapters = p.get_book_info(url) + except Timeout: + print(Fore.RED + Style.BRIGHT + "连接超时,请检查网络连接是否正常。") + return except Exception as e: print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}") return diff --git a/src/qimao_epub.py b/src/qimao_epub.py index c972a07..08ab25f 100644 --- a/src/qimao_epub.py +++ b/src/qimao_epub.py @@ -4,6 +4,7 @@ # 导入必要的模块 import requests +from requests.exceptions import Timeout from ebooklib import epub import re import time @@ -20,6 +21,11 @@ def qimao_epub(url, path_choice, config_path): try: title, intro, author, img_url, chapters = p.get_book_info(url, mode='epub') + # 下载封面 + response = requests.get(img_url, timeout=10, proxies=p.proxies) + except Timeout: + print(Fore.RED + Style.BRIGHT + "连接超时,请检查网络连接是否正常。") + return except Exception as e: print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}") return @@ -27,8 +33,6 @@ def qimao_epub(url, path_choice, config_path): # 创建epub电子书 book = epub.EpubBook() - # 下载封面 - response = requests.get(img_url) # 获取图像的内容 img_data = response.content diff --git a/src/qimao_normal.py b/src/qimao_normal.py index cd1f312..d91a13d 100644 --- a/src/qimao_normal.py +++ b/src/qimao_normal.py @@ -8,7 +8,7 @@ # 导入必要的模块 from colorama import Fore, Style, init from tqdm import tqdm - +from requests.exceptions import Timeout import public as p init(autoreset=True) @@ -23,6 +23,9 @@ def qimao_n(url, encoding, path_choice, data_folder, start_chapter_id, # 调用异步函数获取7猫信息(模拟浏览器) try: title, content, chapters = p.get_book_info(url) + except Timeout: + print(Fore.RED + Style.BRIGHT + "连接超时,请检查网络连接是否正常。") + return except Exception as e: print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}") return diff --git a/src/qimao_update.py b/src/qimao_update.py index 08c3202..616ab2d 100644 --- a/src/qimao_update.py +++ b/src/qimao_update.py @@ -148,6 +148,9 @@ def download_novel(url, encoding, start_chapter_id, txt_file_path): # 调用异步函数获取7猫信息(模拟浏览器) try: _, _, chapters = p.get_book_info(url) + except Timeout: + print(Fore.RED + Style.BRIGHT + "连接超时,请检查网络连接是否正常。") + return except Exception as e: print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}") return @@ -362,12 +365,15 @@ def qimao_epub_update(book_path): try: title, intro, author, img_url, chapters = p.get_book_info(url, mode='epub') + # 下载封面 + response = requests.get(img_url, timeout=10, proxies=p.proxies) + except Timeout: + print(Fore.RED + Style.BRIGHT + "连接超时,请检查网络连接是否正常。") + return except Exception as e: print(Fore.RED + Style.BRIGHT + f"发生异常: \n{e}") return - # 下载封面 - response = requests.get(img_url) # 获取图像的内容 img_data = response.content @@ -387,16 +393,6 @@ def qimao_epub_update(book_path): book.add_author(author) book.add_metadata('DC', 'description', intro) - yaml_data = { - 'qmid': book_id - } - yaml_content = yaml.dump(yaml_data) - - # 设置 qmid 元数据 - yaml_item = epub.EpubItem(uid='yaml', file_name='metadata.yaml', media_type='application/octet-stream', - content=yaml_content) - book.add_item(yaml_item) - # intro chapter intro_e = epub.EpubHtml(title='Introduction', file_name='intro.xhtml', lang='hr') intro_e.content = (f'Cover Image'