initialˆ

This commit is contained in:
2025-09-30 12:54:29 +08:00
commit acdf544b08
117 changed files with 20260 additions and 0 deletions

View File

@@ -0,0 +1,289 @@
from fastapi import FastAPI, HTTPException
from jmcomic import *
from typing import List, Union
import redis
from datetime import timedelta
from functools import wraps
import json
def cache_result(expire_time: timedelta):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
# 生成缓存键
cache_key = f"{func.__name__}:{json.dumps(kwargs)}"
# 尝试从缓存中获取数据
cached_data = redis_client.get(cache_key)
if cached_data:
return json.loads(cached_data)
# 如果缓存中没有数据,则调用函数获取数据
result = func(*args, **kwargs)
# 将结果存入缓存
redis_client.setex(cache_key, int(expire_time.total_seconds()), json.dumps(result))
return result
return wrapper
return decorator
# 配置Redis连接
redis_client = redis.StrictRedis(host='localhost', port=6379, db=0, decode_responses=True)
app = FastAPI()
# 初始化客户端
option = JmOption.default()
client = option.new_jm_client()
@app.post("/login/")
def login(username: str, password: str):
try:
client.login(username, password)
return {"message": "Login successful"}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
from datetime import timedelta
# 设置缓存时间为3天
cache_time = timedelta(days=3)
@app.get("/search/")
@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]
return results
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/album/{album_id}/")
@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
# 存储所有图片的URL
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}/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)
def get_album_details(album_id: int):
try:
page = client.search_site(search_query=str(album_id))
album = page.single_album
# 存储所有图片的URL
image_urls = []
nums = []
# 遍历每个章节
cut = 0
cut2 = 0
for photo in album:
if cut == 1:
break
cut = cut + 1
# 章节实体类
photo_detail = client.get_photo_detail(photo.photo_id, False)
# 遍历每个图片
for image in photo_detail:
# 图片实体类
if cut2 == 1:
break
cut2 = cut2 + 1
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("/favorites/")
@cache_result(cache_time)
def get_favorites(username: str):
try:
favorites = []
for page in client.favorite_folder_gen(username=username):
for aid, atitle in page:
favorites.append({"album_id": aid, "title": atitle})
return favorites
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/categories/")
@cache_result(cache_time)
def get_categories(page: int = 1, time: str = JmMagicConstants.TIME_ALL, category: str = JmMagicConstants.CATEGORY_ALL,
order_by: str = JmMagicConstants.ORDER_BY_LATEST):
try:
page = client.categories_filter(page=page, time=time, category=category, order_by=order_by)
results = [{"album_id": aid, "title": atitle} for aid, atitle in page]
return results
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/rankings/year")
@cache_result(cache_time)
def get_rankings_year(page: int):
try:
op = JmOption.default()
cl = op.new_jm_client()
page: JmCategoryPage = cl.categories_filter(
page=page,
time=JmMagicConstants.TIME_ALL,
category=JmMagicConstants.CATEGORY_ALL,
order_by=JmMagicConstants.ORDER_BY_LATEST,
)
results = [{"album_id": aid, "title": atitle} for aid, atitle in page]
return results
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/rankings/mouth")
@cache_result(cache_time)
def get_rankings_mouth(page: int):
try:
op = JmOption.default()
cl = op.new_jm_client()
page2: JmCategoryPage = cl.categories_filter(
page=page,
time=JmMagicConstants.TIME_MONTH,
category=JmMagicConstants.CATEGORY_ALL,
order_by=JmMagicConstants.ORDER_BY_LATEST,
)
results = [{"album_id": aid, "title": atitle} for aid, atitle in page2]
return results
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/rankings/week")
@cache_result(cache_time)
def get_rankings_week(page: int):
try:
op = JmOption.default()
cl = op.new_jm_client()
page2: JmCategoryPage = cl.categories_filter(
page=page,
time=JmMagicConstants.TIME_WEEK,
category=JmMagicConstants.CATEGORY_ALL,
order_by=JmMagicConstants.ORDER_BY_LATEST,
)
results = [{"album_id": aid, "title": atitle} for aid, atitle in page2]
return results
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# 启动API
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8982)