Skip to content

Commit

Permalink
fix: 上传封面前先检测是否已有海报
Browse files Browse the repository at this point in the history
  • Loading branch information
chu-shen committed Jun 2, 2024
1 parent 5292861 commit ebc429c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Executing this program will result in the loss of old metadata for series and bo
- ~~意义不明的参数~~,建议设置为`False`,可缩短程序运行时间
- 如果刷新书时,bangumi 数据不完整,则可以在数据补充后使用此参数修正此书元数据

- `USE_BANGUMI_THUMBNAIL`: 设置为`True`时使用 Bangumi 封面替换 Komga 生成的缩略图
- `USE_BANGUMI_THUMBNAIL`: 设置为`True`时使用 Bangumi 封面替换 Komga 生成的缩略图(如果未曾上传过海报)

## 如何修正错误元数据

Expand Down
19 changes: 18 additions & 1 deletion api/komgaApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def __init__(self, base_url, username, password):
self.r.mount('https://', HTTPAdapter(max_retries=3))

url = f'{self.base_url}/login/set-cookie'
if self.r.get(url, auth=self.auth).status_code != 204:
response = self.r.get(url, auth=self.auth,headers = {
'User-Agent': 'chu-shen/BangumiKomga (https://github.com/chu-shen/BangumiKomga)'})
if response.status_code != 204:
logger.error("Komga: login failed!")
exit(1)

Expand Down Expand Up @@ -98,6 +100,21 @@ def get_series_books(self, series_id):
return []
# return the response as a JSON object
return response.json()

def get_series_thumbnails(self, series_id):
'''
Retrieves all thumbnails in a specified series in the komga comic.
'''
try:
# make a GET request to the URL to retrieve all thumbnails in a given series
response = self.r.get(
f'{self.base_url}/series/{series_id}/thumbnails')
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"An error occurred: {e}")
return []
# return the response as a JSON object
return response.json()

def update_series_metadata(self, series_id, metadata):
'''
Expand Down
3 changes: 2 additions & 1 deletion refreshMetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def refresh_metadata(force_refresh_list=[]):
success_count, success_comic = record_series_status(
conn, series_id, subject_id, 1, series_name, komga_metadata.title, success_count, success_comic)
# 使用 Bangumi 图片替换原封面
if USE_BANGUMI_THUMBNAIL:
# 确保没有上传过海报,避免重复上传
if USE_BANGUMI_THUMBNAIL and len(komga.get_series_thumbnails(series_id)) == 0:
thumbnail=bgm.get_subject_thumbnail(metadata)
replace_thumbnail_result=komga.update_series_thumbnail(series_id, thumbnail)
if replace_thumbnail_result:
Expand Down

0 comments on commit ebc429c

Please sign in to comment.