Skip to content

Commit

Permalink
fix: 繁转简后再执行bangumi查询
Browse files Browse the repository at this point in the history
  • Loading branch information
chu-shen committed Jul 5, 2023
1 parent e629490 commit 9af7438
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions bangumiApi.py → api/bangumiApi.py
Expand Up @@ -5,8 +5,9 @@


import requests
from requests.adapters import HTTPAdapter
from Levenshtein import distance
from log import logger
from tools.log import logger
from zhconv import convert
from urllib.parse import quote_plus

Expand All @@ -15,6 +16,9 @@ class BangumiApi:
BASE_URL = "https://api.bgm.tv"

def __init__(self, access_token=None):
self.r = requests.Session()
self.r.mount('http://', HTTPAdapter(max_retries=3))
self.r.mount('https://', HTTPAdapter(max_retries=3))
self.access_token = access_token
if self.access_token:
self.refresh_token()
Expand Down Expand Up @@ -55,11 +59,11 @@ def search_subjects(self, query):
'''
获取搜索结果,并移除非漫画系列。返回具有完整元数据的条目
'''
# query = convert(query, 'zh-cn'))
query = convert(query, 'zh-cn')
url = f"{self.BASE_URL}/search/subject/{quote_plus(query)}?responseGroup=small&type=1&max_results=25"
# TODO 处理'citrus+ ~柑橘味香气plus~'
try:
response = requests.get(url, headers=self._get_headers())
response = self.r.get(url, headers=self._get_headers())
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"An error occurred: {e}")
Expand All @@ -84,6 +88,8 @@ def search_subjects(self, query):
for result in results:
manga_id = result['id']
manga_metadata = self.get_subject_metadata(manga_id)
if not manga_metadata:
continue
# bangumi书籍类型包括:漫画、小说、画集、其他
# 由于komga不支持小说文字的读取,这里直接忽略`小说`类型,避免返回错误结果
if manga_metadata["platform"] != "小说":
Expand All @@ -109,7 +115,7 @@ def get_subject_metadata(self, subject_id):
'''
url = f"{self.BASE_URL}/v0/subjects/{subject_id}"
try:
response = requests.get(url, headers=self._get_headers())
response = self.r.get(url, headers=self._get_headers())
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"An error occurred: {e}")
Expand All @@ -122,7 +128,7 @@ def get_related_subjects(self, subject_id):
'''
url = f"{self.BASE_URL}/v0/subjects/{subject_id}/subjects"
try:
response = requests.get(url, headers=self._get_headers())
response = self.r.get(url, headers=self._get_headers())
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"An error occurred: {e}")
Expand Down

0 comments on commit 9af7438

Please sign in to comment.