This commit is contained in:
2025-10-05 20:08:52 +08:00
parent acdf544b08
commit ca793b4de7
31 changed files with 4714 additions and 81 deletions

View File

@@ -58,17 +58,17 @@ from datetime import timedelta
cache_time = timedelta(days=3)
@app.get("/search/")
@cache_result(cache_time)
# @cache_result(cache_time)
def search_site(search_query: str, page: int = 1):
try:
page = client.search_site(search_query=search_query, page=page)
results = [{"album_id": album_id, "title": title} for album_id, title in page]
results = [{"album_id": album_id, "name": title} for album_id, title in page]
return results
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/album/{album_id}/")
@cache_result(cache_time)
# @cache_result(cache_time)
def get_album_details(album_id: int):
try:
page = client.search_site(search_query=str(album_id))
@@ -77,6 +77,92 @@ def get_album_details(album_id: int):
image_urls = []
nums = []
# 遍历每个章节
for photo in album:
# 章节实体类
photo_detail = client.get_photo_detail(photo.photo_id, False)
# 遍历每个图片
for image in photo_detail:
# 图片实体类
image_urls.append(image.img_url)
nums.append(JmImageTool.get_num_by_url(image.scramble_id, image.img_url))
return {
"album_id": album.album_id,
"scramble_id": album.scramble_id,
"name": album.name,
"page_count": album.page_count,
"pub_date": album.pub_date,
"update_date": album.update_date,
"likes": album.likes,
"views": album.views,
"comment_count": album.comment_count,
"works": album.works,
"actors": album.actors,
"authors": album.authors,
"tags": album.tags,
"related_list": album.related_list,
"episode_list": album.episode_list,
"image_urls": image_urls,
"nums": nums
}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/album/{album_id}/debug")
# @cache_result(cache_time)
def get_album_details(album_id: int):
try:
page = client.search_site(search_query=str(album_id))
album = page.single_album
# 输出调试信息到控制台
import json as json_module
# 创建简单的序列化函数,避免复杂对象导致的问题
def simple_serialize(obj):
if hasattr(obj, '__dict__'):
result = {}
for key, value in obj.__dict__.items():
try:
# 尝试直接序列化
json_module.dumps(value)
result[key] = value
except:
# 如果不能序列化,转换为字符串
result[key] = str(value)
return result
else:
return str(obj)
# 输出page和album的调试信息到控制台
print("=" * 50)
print("DEBUG: Page object")
print("=" * 50)
try:
print(json_module.dumps(simple_serialize(page), indent=2, ensure_ascii=False))
except Exception as e:
print(f"无法序列化page对象: {e}")
print(f"Page类型: {type(page)}")
print(f"Page属性: {[attr for attr in dir(page) if not attr.startswith('_')]}")
print("\n" + "=" * 50)
print("DEBUG: Album object")
print("=" * 50)
try:
print(json_module.dumps(simple_serialize(album), indent=2, ensure_ascii=False))
except Exception as e:
print(f"无法序列化album对象: {e}")
print(f"Album类型: {type(album)}")
print(f"Album属性: {[attr for attr in dir(album) if not attr.startswith('_')]}")
# 存储所有图片的URL
image_urls = []
nums = []
# 遍历每个章节
for photo in album:
# 章节实体类
photo_detail = client.get_photo_detail(photo.photo_id, False)
@@ -110,57 +196,8 @@ def get_album_details(album_id: int):
raise HTTPException(status_code=400, detail=str(e))
@app.get("/album/{album_id}/chapters/")
@cache_result(cache_time)
def get_album_chapters_paginated(album_id: int, page: int = 1, per_page: int = 5):
"""
分页获取专辑章节列表
:param album_id: 专辑ID
:param page: 页码从1开始
:param per_page: 每页章节数
"""
try:
page_result = client.search_site(search_query=str(album_id))
album = page_result.single_album
# 计算分页信息
total_chapters = len(album.photos)
total_pages = (total_chapters + per_page - 1) // per_page # 向上取整
if page < 1 or page > total_pages:
raise HTTPException(status_code=404, detail="Page out of range")
# 计算当前页的章节范围
start_index = (page - 1) * per_page
end_index = min(start_index + per_page, total_chapters)
# 获取当前页的章节信息
chapters = []
for i in range(start_index, end_index):
photo = album.photos[i]
chapters.append({
"chapter_index": i,
"chapter_id": photo.photo_id,
"title": photo.name,
"page_count": photo.page_count,
"pub_date": photo.pub_date
})
return {
"album_id": album.album_id,
"album_name": album.name,
"current_page": page,
"per_page": per_page,
"total_chapters": total_chapters,
"total_pages": total_pages,
"chapters": chapters
}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/fast/{album_id}/")
# @cache_result(cache_time)
@cache_result(cache_time)
def get_album_details(album_id: int):
try:
page = client.search_site(search_query=str(album_id))