fix wallpaper
This commit is contained in:
parent
9df8d3d360
commit
e3707f39bb
@ -1,4 +1,3 @@
|
|||||||
import random
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
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 import get_db
|
||||||
from app.db.models.user import User
|
from app.db.models.user import User
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
from app.utils.http import RequestUtils
|
from app.utils.web import WebUtils
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@ -67,21 +66,10 @@ def bing_wallpaper() -> Any:
|
|||||||
"""
|
"""
|
||||||
获取Bing每日壁纸
|
获取Bing每日壁纸
|
||||||
"""
|
"""
|
||||||
url = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"
|
url = WebUtils.get_bing_wallpaper()
|
||||||
try:
|
if url:
|
||||||
resp = RequestUtils(timeout=5).get_res(url)
|
return schemas.Response(success=False,
|
||||||
except Exception as err:
|
message=url)
|
||||||
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))
|
|
||||||
return schemas.Response(success=False)
|
return schemas.Response(success=False)
|
||||||
|
|
||||||
|
|
||||||
@ -90,14 +78,10 @@ def tmdb_wallpaper(db: Session = Depends(get_db)) -> Any:
|
|||||||
"""
|
"""
|
||||||
获取TMDB电影海报
|
获取TMDB电影海报
|
||||||
"""
|
"""
|
||||||
infos = TmdbChain(db).tmdb_trending()
|
wallpager = TmdbChain(db).get_random_wallpager()
|
||||||
if infos:
|
if wallpager:
|
||||||
# 随机一个电影
|
return schemas.Response(
|
||||||
while True:
|
success=True,
|
||||||
info = random.choice(infos)
|
message=wallpager
|
||||||
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')}"
|
|
||||||
)
|
|
||||||
return schemas.Response(success=False)
|
return schemas.Response(success=False)
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
|
import random
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
|
from cachetools import cached, TTLCache
|
||||||
|
|
||||||
from app import schemas
|
from app import schemas
|
||||||
from app.chain import ChainBase
|
from app.chain import ChainBase
|
||||||
from app.schemas import MediaType
|
from app.schemas import MediaType
|
||||||
@ -106,3 +109,17 @@ class TmdbChain(ChainBase):
|
|||||||
:param page: 页码
|
:param page: 页码
|
||||||
"""
|
"""
|
||||||
return self.run_module("person_credits", person_id=person_id, page=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
|
||||||
|
@ -4,7 +4,7 @@ from typing import Any
|
|||||||
from app.chain import ChainBase
|
from app.chain import ChainBase
|
||||||
from app.schemas import Notification
|
from app.schemas import Notification
|
||||||
from app.schemas.types import EventType, MediaImageType, MediaType, NotificationType
|
from app.schemas.types import EventType, MediaImageType, MediaType, NotificationType
|
||||||
from app.utils.http import WebUtils
|
from app.utils.web import WebUtils
|
||||||
|
|
||||||
|
|
||||||
class WebhookChain(ChainBase):
|
class WebhookChain(ChainBase):
|
||||||
|
@ -172,39 +172,3 @@ class RequestUtils:
|
|||||||
cookiesList.append(cookies)
|
cookiesList.append(cookies)
|
||||||
return cookiesList
|
return cookiesList
|
||||||
return cookie_dict
|
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
56
app/utils/web.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user