fix wallpaper

This commit is contained in:
jxxghp 2023-09-22 11:33:25 +08:00
parent 9df8d3d360
commit e3707f39bb
5 changed files with 85 additions and 64 deletions

View File

@ -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)

View File

@ -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

View File

@ -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):

View File

@ -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)

56
app/utils/web.py Normal file
View File

@ -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