diff --git a/app/api/endpoints/login.py b/app/api/endpoints/login.py index d182d507..0179738c 100644 --- a/app/api/endpoints/login.py +++ b/app/api/endpoints/login.py @@ -1,5 +1,5 @@ from datetime import timedelta -from typing import Any +from typing import Any, List from fastapi import APIRouter, Depends, HTTPException, Form from fastapi.security import OAuth2PasswordRequestForm @@ -21,9 +21,9 @@ router = APIRouter() @router.post("/access-token", summary="获取token", response_model=schemas.Token) async def login_access_token( - db: Session = Depends(get_db), - form_data: OAuth2PasswordRequestForm = Depends(), - otp_password: str = Form(None) + db: Session = Depends(get_db), + form_data: OAuth2PasswordRequestForm = Depends(), + otp_password: str = Form(None) ) -> Any: """ 获取认证Token @@ -78,18 +78,9 @@ def wallpaper() -> Any: 获取登录页面电影海报 """ if settings.WALLPAPER == "tmdb": - return tmdb_wallpaper() - elif settings.WALLPAPER == "bing": - return bing_wallpaper() - return schemas.Response(success=False) - - -@router.get("/bing", summary="Bing每日壁纸", response_model=schemas.Response) -def bing_wallpaper() -> Any: - """ - 获取Bing每日壁纸 - """ - url = WebUtils.get_bing_wallpaper() + url = WebUtils.get_bing_wallpaper() + else: + url = TmdbChain().get_random_wallpager() if url: return schemas.Response( success=True, @@ -98,15 +89,12 @@ def bing_wallpaper() -> Any: return schemas.Response(success=False) -@router.get("/tmdb", summary="TMDB电影海报", response_model=schemas.Response) -def tmdb_wallpaper() -> Any: +@router.get("/wallpapers", summary="登录页面电影海报列表", response_model=List[str]) +def wallpapers() -> Any: """ - 获取TMDB电影海报 + 获取登录页面电影海报 """ - wallpager = TmdbChain().get_random_wallpager() - if wallpager: - return schemas.Response( - success=True, - message=wallpager - ) - return schemas.Response(success=False) + if settings.WALLPAPER == "tmdb": + return TmdbChain().get_trending_wallpapers() + else: + return WebUtils.get_bing_wallpapers() diff --git a/app/chain/tmdb.py b/app/chain/tmdb.py index 89ae2a39..16a99791 100644 --- a/app/chain/tmdb.py +++ b/app/chain/tmdb.py @@ -126,3 +126,14 @@ class TmdbChain(ChainBase, metaclass=Singleton): if info and info.backdrop_path: return f"https://{settings.TMDB_IMAGE_DOMAIN}/t/p/original{info.backdrop_path}" return None + + @cached(cache=TTLCache(maxsize=1, ttl=3600)) + def get_trending_wallpapers(self, num: int = 10) -> Optional[List[str]]: + """ + 获取所有流行壁纸 + """ + infos = self.tmdb_trending() + if infos: + return [f"https://{settings.TMDB_IMAGE_DOMAIN}/t/p/original{info.backdrop_path}" + for info in infos if info and info.backdrop_path][:num] + return None diff --git a/app/utils/web.py b/app/utils/web.py index 5fa20237..4ab0e877 100644 --- a/app/utils/web.py +++ b/app/utils/web.py @@ -88,3 +88,19 @@ class WebUtils: except Exception as err: print(str(err)) return None + + @staticmethod + def get_bing_wallpapers(num: int = 7) -> Optional[str]: + """ + 获取7天的Bing每日壁纸 + """ + url = f"https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n={num}" + resp = RequestUtils(timeout=5).get_res(url) + if resp and resp.status_code == 200: + try: + result = resp.json() + if isinstance(result, dict): + return [f"https://cn.bing.com{image.get('url')}" for image in result.get('images') or []] + except Exception as err: + print(str(err)) + return None