add wallpapers api
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
from typing import Any, List
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Form
|
from fastapi import APIRouter, Depends, HTTPException, Form
|
||||||
from fastapi.security import OAuth2PasswordRequestForm
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
@ -21,9 +21,9 @@ router = APIRouter()
|
|||||||
|
|
||||||
@router.post("/access-token", summary="获取token", response_model=schemas.Token)
|
@router.post("/access-token", summary="获取token", response_model=schemas.Token)
|
||||||
async def login_access_token(
|
async def login_access_token(
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
form_data: OAuth2PasswordRequestForm = Depends(),
|
form_data: OAuth2PasswordRequestForm = Depends(),
|
||||||
otp_password: str = Form(None)
|
otp_password: str = Form(None)
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
获取认证Token
|
获取认证Token
|
||||||
@ -78,18 +78,9 @@ def wallpaper() -> Any:
|
|||||||
获取登录页面电影海报
|
获取登录页面电影海报
|
||||||
"""
|
"""
|
||||||
if settings.WALLPAPER == "tmdb":
|
if settings.WALLPAPER == "tmdb":
|
||||||
return tmdb_wallpaper()
|
url = WebUtils.get_bing_wallpaper()
|
||||||
elif settings.WALLPAPER == "bing":
|
else:
|
||||||
return bing_wallpaper()
|
url = TmdbChain().get_random_wallpager()
|
||||||
return schemas.Response(success=False)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/bing", summary="Bing每日壁纸", response_model=schemas.Response)
|
|
||||||
def bing_wallpaper() -> Any:
|
|
||||||
"""
|
|
||||||
获取Bing每日壁纸
|
|
||||||
"""
|
|
||||||
url = WebUtils.get_bing_wallpaper()
|
|
||||||
if url:
|
if url:
|
||||||
return schemas.Response(
|
return schemas.Response(
|
||||||
success=True,
|
success=True,
|
||||||
@ -98,15 +89,12 @@ def bing_wallpaper() -> Any:
|
|||||||
return schemas.Response(success=False)
|
return schemas.Response(success=False)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/tmdb", summary="TMDB电影海报", response_model=schemas.Response)
|
@router.get("/wallpapers", summary="登录页面电影海报列表", response_model=List[str])
|
||||||
def tmdb_wallpaper() -> Any:
|
def wallpapers() -> Any:
|
||||||
"""
|
"""
|
||||||
获取TMDB电影海报
|
获取登录页面电影海报
|
||||||
"""
|
"""
|
||||||
wallpager = TmdbChain().get_random_wallpager()
|
if settings.WALLPAPER == "tmdb":
|
||||||
if wallpager:
|
return TmdbChain().get_trending_wallpapers()
|
||||||
return schemas.Response(
|
else:
|
||||||
success=True,
|
return WebUtils.get_bing_wallpapers()
|
||||||
message=wallpager
|
|
||||||
)
|
|
||||||
return schemas.Response(success=False)
|
|
||||||
|
@ -126,3 +126,14 @@ class TmdbChain(ChainBase, metaclass=Singleton):
|
|||||||
if info and info.backdrop_path:
|
if info and info.backdrop_path:
|
||||||
return f"https://{settings.TMDB_IMAGE_DOMAIN}/t/p/original{info.backdrop_path}"
|
return f"https://{settings.TMDB_IMAGE_DOMAIN}/t/p/original{info.backdrop_path}"
|
||||||
return None
|
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
|
||||||
|
@ -88,3 +88,19 @@ class WebUtils:
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(str(err))
|
print(str(err))
|
||||||
return None
|
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
|
||||||
|
Reference in New Issue
Block a user