From e41450ddd8740b154ccf90c7b557f23518a3d1a7 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 24 Jun 2023 21:00:24 +0800 Subject: [PATCH] add wallpaper api --- app/api/endpoints/login.py | 24 ++++++++++++++++++++++++ app/modules/qbittorrent/__init__.py | 6 +++--- app/modules/transmission/__init__.py | 6 +++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/api/endpoints/login.py b/app/api/endpoints/login.py index a75eddea..e34da0de 100644 --- a/app/api/endpoints/login.py +++ b/app/api/endpoints/login.py @@ -12,6 +12,7 @@ from app.core.config import settings from app.db import get_db from app.db.models.user import User from app.log import logger +from app.utils.http import RequestUtils router = APIRouter() @@ -47,3 +48,26 @@ async def login_access_token( ), token_type="bearer", ) + + +@router.get("/login/wallpaper", summary="Bing每日壁纸", response_model=schemas.Response) +async 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)) + return schemas.Response(success=False) diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index a70bade4..598a73d4 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -97,7 +97,7 @@ class QbittorrentModule(_ModuleBase): if hashs: # 按Hash获取 torrents, _ = self.qbittorrent.get_torrents(ids=hashs, tags=settings.TORRENT_TAG) - for torrent in torrents: + for torrent in torrents or []: content_path = torrent.get("content_path") if content_path: torrent_path = Path(content_path) @@ -112,7 +112,7 @@ class QbittorrentModule(_ModuleBase): elif status == TorrentStatus.TRANSFER: # 获取已完成且未整理的 torrents = self.qbittorrent.get_completed_torrents(tags=settings.TORRENT_TAG) - for torrent in torrents: + for torrent in torrents or []: tags = torrent.get("tags") or [] if "已整理" in tags: continue @@ -131,7 +131,7 @@ class QbittorrentModule(_ModuleBase): elif status == TorrentStatus.DOWNLOADING: # 获取正在下载的任务 torrents = self.qbittorrent.get_downloading_torrents(tags=settings.TORRENT_TAG) - for torrent in torrents: + for torrent in torrents or []: meta = MetaInfo(torrent.get('name')) ret_torrents.append(DownloadingTorrent( title=torrent.get('name'), diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index 4c15de98..272d7592 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -84,7 +84,7 @@ class TransmissionModule(_ModuleBase): if hashs: # 按Hash获取 torrents, _ = self.transmission.get_torrents(ids=hashs, tags=settings.TORRENT_TAG) - for torrent in torrents: + for torrent in torrents or []: ret_torrents.append(TransferTorrent( title=torrent.name, path=Path(torrent.download_dir) / torrent.name, @@ -94,7 +94,7 @@ class TransmissionModule(_ModuleBase): elif status == TorrentStatus.TRANSFER: # 获取已完成且未整理的 torrents = self.transmission.get_completed_torrents(tags=settings.TORRENT_TAG) - for torrent in torrents: + for torrent in torrents or []: # 含"已整理"tag的不处理 if "已整理" in torrent.labels or []: continue @@ -113,7 +113,7 @@ class TransmissionModule(_ModuleBase): elif status == TorrentStatus.DOWNLOADING: # 获取正在下载的任务 torrents = self.transmission.get_downloading_torrents(tags=settings.TORRENT_TAG) - for torrent in torrents: + for torrent in torrents or []: meta = MetaInfo(torrent.name) ret_torrents.append(DownloadingTorrent( title=torrent.name,