Skip to content

Commit

Permalink
feat: 3.0.0b
Browse files Browse the repository at this point in the history
修复epub更新两个元信息问题

为请求增加超时处理
  • Loading branch information
shing-yu committed Mar 22, 2024
1 parent 5b70174 commit c98d89b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 30 deletions.
22 changes: 11 additions & 11 deletions src/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,10 +32,6 @@
os.makedirs(data_path, exist_ok=True)
book_id = None
start_chapter_id = "0"
proxies = {
"http": None,
"https": None
}


# 用户须知
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -692,6 +689,9 @@ def search():
continue
except KeyboardInterrupt:
return
except Timeout:
print("请求超时,请检查网络连接")
return
# except Exception as e:
# print(f"发生错误: {e}")
# return
Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
16 changes: 13 additions & 3 deletions src/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
init(autoreset=True)


proxies = {
"http": None,
"https": None
}


def get_headers(book_id):

version_list = [
Expand Down Expand Up @@ -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()


Expand All @@ -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"]
Expand Down Expand Up @@ -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": {
Expand Down
4 changes: 4 additions & 0 deletions src/qimao_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hashlib
import public as p
from colorama import Fore, Style, init
from requests.exceptions import Timeout

init(autoreset=True)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/qimao_chapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/qimao_epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# 导入必要的模块
import requests
from requests.exceptions import Timeout
from ebooklib import epub
import re
import time
Expand All @@ -20,15 +21,18 @@ 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

# 创建epub电子书
book = epub.EpubBook()

# 下载封面
response = requests.get(img_url)
# 获取图像的内容
img_data = response.content

Expand Down
5 changes: 4 additions & 1 deletion src/qimao_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
20 changes: 8 additions & 12 deletions src/qimao_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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'<img src="image.jpg" alt="Cover Image"/>'
Expand Down

0 comments on commit c98d89b

Please sign in to comment.