From e3707f39bb512f8d5b161d4e78912a1651ae1906 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 22 Sep 2023 11:33:25 +0800 Subject: [PATCH] fix wallpaper --- app/api/endpoints/login.py | 38 ++++++++------------------ app/chain/tmdb.py | 17 ++++++++++++ app/chain/webhook.py | 2 +- app/utils/http.py | 36 ------------------------ app/utils/web.py | 56 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 64 deletions(-) create mode 100644 app/utils/web.py diff --git a/app/api/endpoints/login.py b/app/api/endpoints/login.py index 4f484e86..729cc31e 100644 --- a/app/api/endpoints/login.py +++ b/app/api/endpoints/login.py @@ -1,4 +1,3 @@ -import random from datetime import timedelta from typing import Any @@ -15,7 +14,7 @@ from app.core.security import get_password_hash from app.db import get_db from app.db.models.user import User from app.log import logger -from app.utils.http import RequestUtils +from app.utils.web import WebUtils router = APIRouter() @@ -67,21 +66,10 @@ def bing_wallpaper() -> Any: """ 获取Bing每日壁纸 """ - url = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" - try: - resp = RequestUtils(timeout=5).get_res(url) - except Exception as err: - print(str(err)) - return schemas.Response(success=False) - if resp and resp.status_code == 200: - try: - result = resp.json() - if isinstance(result, dict): - for image in result.get('images') or []: - return schemas.Response(success=False, - message=f"https://cn.bing.com{image.get('url')}" if 'url' in image else '') - except Exception as err: - print(str(err)) + url = WebUtils.get_bing_wallpaper() + if url: + return schemas.Response(success=False, + message=url) return schemas.Response(success=False) @@ -90,14 +78,10 @@ def tmdb_wallpaper(db: Session = Depends(get_db)) -> Any: """ 获取TMDB电影海报 """ - infos = TmdbChain(db).tmdb_trending() - if infos: - # 随机一个电影 - while True: - info = random.choice(infos) - if info and info.get("backdrop_path"): - return schemas.Response( - success=True, - message=f"https://image.tmdb.org/t/p/original{info.get('backdrop_path')}" - ) + wallpager = TmdbChain(db).get_random_wallpager() + if wallpager: + return schemas.Response( + success=True, + message=wallpager + ) return schemas.Response(success=False) diff --git a/app/chain/tmdb.py b/app/chain/tmdb.py index 2a7ae951..b991eae9 100644 --- a/app/chain/tmdb.py +++ b/app/chain/tmdb.py @@ -1,5 +1,8 @@ +import random from typing import Optional, List +from cachetools import cached, TTLCache + from app import schemas from app.chain import ChainBase from app.schemas import MediaType @@ -106,3 +109,17 @@ class TmdbChain(ChainBase): :param page: 页码 """ return self.run_module("person_credits", person_id=person_id, page=page) + + @cached(cache=TTLCache(maxsize=1, ttl=3600)) + def get_random_wallpager(self): + """ + 获取随机壁纸,缓存1个小时 + """ + infos = self.tmdb_trending() + if infos: + # 随机一个电影 + while True: + info = random.choice(infos) + if info and info.get("backdrop_path"): + return f"https://image.tmdb.org/t/p/original{info.get('backdrop_path')}" + return None diff --git a/app/chain/webhook.py b/app/chain/webhook.py index dc37c1b2..5ee77d12 100644 --- a/app/chain/webhook.py +++ b/app/chain/webhook.py @@ -4,7 +4,7 @@ from typing import Any from app.chain import ChainBase from app.schemas import Notification from app.schemas.types import EventType, MediaImageType, MediaType, NotificationType -from app.utils.http import WebUtils +from app.utils.web import WebUtils class WebhookChain(ChainBase): diff --git a/app/utils/http.py b/app/utils/http.py index 7f032a17..4330da62 100644 --- a/app/utils/http.py +++ b/app/utils/http.py @@ -172,39 +172,3 @@ class RequestUtils: cookiesList.append(cookies) return cookiesList return cookie_dict - - -class WebUtils: - @staticmethod - def get_location(ip: str): - """ - https://api.mir6.com/api/ip - { - "code": 200, - "msg": "success", - "data": { - "ip": "240e:97c:2f:1::5c", - "dec": "47925092370311863177116789888333643868", - "country": "中国", - "countryCode": "CN", - "province": "广东省", - "city": "广州市", - "districts": "", - "idc": "", - "isp": "中国电信", - "net": "数据中心", - "zipcode": "510000", - "areacode": "020", - "protocol": "IPv6", - "location": "中国[CN] 广东省 广州市", - "myip": "125.89.7.89", - "time": "2023-09-01 17:28:23" - } - } - """ - try: - r = RequestUtils().get_res(f"https://api.mir6.com/api/ip?ip={ip}&type=json") - if r: - return r.json().get("data", {}).get("location") or '' - except Exception as err: - return str(err) diff --git a/app/utils/web.py b/app/utils/web.py new file mode 100644 index 00000000..4116cf96 --- /dev/null +++ b/app/utils/web.py @@ -0,0 +1,56 @@ +from typing import Optional + +from app.utils.http import RequestUtils + + +class WebUtils: + @staticmethod + def get_location(ip: str): + """ + https://api.mir6.com/api/ip + { + "code": 200, + "msg": "success", + "data": { + "ip": "240e:97c:2f:1::5c", + "dec": "47925092370311863177116789888333643868", + "country": "中国", + "countryCode": "CN", + "province": "广东省", + "city": "广州市", + "districts": "", + "idc": "", + "isp": "中国电信", + "net": "数据中心", + "zipcode": "510000", + "areacode": "020", + "protocol": "IPv6", + "location": "中国[CN] 广东省 广州市", + "myip": "125.89.7.89", + "time": "2023-09-01 17:28:23" + } + } + """ + try: + r = RequestUtils().get_res(f"https://api.mir6.com/api/ip?ip={ip}&type=json") + if r: + return r.json().get("data", {}).get("location") or '' + except Exception as err: + return str(err) + + @staticmethod + def get_bing_wallpaper() -> Optional[str]: + """ + 获取Bing每日壁纸 + """ + url = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" + resp = RequestUtils(timeout=5).get_res(url) + if resp and resp.status_code == 200: + try: + result = resp.json() + if isinstance(result, dict): + for image in result.get('images') or []: + return f"https://cn.bing.com{image.get('url')}" if 'url' in image else '' + except Exception as err: + print(str(err)) + return None