From 488a691f292fa2dbad4b760ab4fc576082c9e9a7 Mon Sep 17 00:00:00 2001 From: qiaoyun680 <549653222@qq.com> Date: Wed, 14 May 2025 16:50:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=A3=81=E7=BA=B8api=E5=9C=B0?= =?UTF-8?q?=E5=9D=80,=E8=BF=94=E5=9B=9E=E4=B8=AD=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=AD=E5=85=81=E8=AE=B8=E7=9A=84=E5=9B=BE=E7=89=87=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8E=E7=BC=80=E6=A0=BC=E5=BC=8F=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E9=83=BD=E4=BC=9A=E8=BF=94=E5=9B=9E=E4=BD=9C=E4=B8=BA=E5=A3=81?= =?UTF-8?q?=E7=BA=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/login.py | 4 +++ app/core/config.py | 3 ++- app/utils/web.py | 51 +++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/app/api/endpoints/login.py b/app/api/endpoints/login.py index cb12d39d..ea6d900b 100644 --- a/app/api/endpoints/login.py +++ b/app/api/endpoints/login.py @@ -58,6 +58,8 @@ def wallpaper() -> Any: url = WebUtils.get_bing_wallpaper() elif settings.WALLPAPER == "mediaserver": url = MediaServerChain().get_latest_wallpaper() + elif settings.WALLPAPER == "customize": + url = WebUtils.get_customize_wallpapers()[0] else: url = TmdbChain().get_random_wallpager() if url: @@ -79,5 +81,7 @@ def wallpapers() -> Any: return MediaServerChain().get_latest_wallpapers() elif settings.WALLPAPER == "tmdb": return TmdbChain().get_trending_wallpapers() + elif settings.WALLPAPER == "customize": + return WebUtils.get_customize_wallpapers() else: return [] diff --git a/app/core/config.py b/app/core/config.py index 5a649b9f..30f1a591 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -267,7 +267,8 @@ class ConfigModel(BaseModel): TOKENIZED_SEARCH: bool = False # 为指定默认字幕添加.default后缀 DEFAULT_SUB: Optional[str] = "zh-cn" - + # 自定义壁纸api地址 + CUSTOMIZE_WALLPAPER_API_URL: Optional[str] = None class Settings(BaseSettings, ConfigModel, LogConfigModel): """ diff --git a/app/utils/web.py b/app/utils/web.py index 9e83070c..55adce24 100644 --- a/app/utils/web.py +++ b/app/utils/web.py @@ -1,7 +1,7 @@ from typing import Optional, List from app.core.cache import cached - +from app.core.config import settings from app.utils.http import RequestUtils @@ -108,3 +108,52 @@ class WebUtils: except Exception as err: print(str(err)) return [] + + @staticmethod + @cached(maxsize=1, ttl=3600) + def get_customize_wallpapers() -> List[str]: + """ + 递归查找对象中所有包含特定后缀的文件或URL,返回匹配的字符串列表 + 支持输入:字典、列表、字符串(普通文件路径或URL) + """ + def find_files_with_suffixes(obj, suffixes: list[str]) -> list[str]: + """ + 递归查找对象中所有包含特定后缀的文件,返回匹配的字符串列表 + 支持输入:字典、列表、字符串 + """ + result = [] + + # 处理字符串 + if isinstance(obj, str): + if obj.endswith(tuple(suffixes)): + result.append(obj) + + # 处理字典 + elif isinstance(obj, dict): + for value in obj.values(): + result.extend(find_files_with_suffixes(value, suffixes)) + + # 处理列表 + elif isinstance(obj, list): + for item in obj: + result.extend(find_files_with_suffixes(item, suffixes)) + + return result + """ + 获取自定义壁纸api壁纸 + """ + # 判断是否存在自定义壁纸api + if settings.CUSTOMIZE_WALLPAPER_API_KEY is not None and len(settings.CUSTOMIZE_WALLPAPER_API_KEY) > 0: + url = settings.CUSTOMIZE_WALLPAPER_API_KEY + wallpaper_list = [] + resp = RequestUtils(timeout=5).get_res(url) + if resp and resp.status_code == 200: + try: + result = resp.json() + if isinstance(result, list) or isinstance(result, dict) or isinstance(result, str): + wallpaper_list = find_files_with_suffixes(result, settings.SECURITY_IMAGE_SUFFIXES) + except Exception as err: + print(str(err)) + return wallpaper_list + else: + return [] \ No newline at end of file From 16b6c0da33f43ea325ca94374c7c34a67b964243 Mon Sep 17 00:00:00 2001 From: qiaoyun680 <549653222@qq.com> Date: Wed, 14 May 2025 20:04:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=A3=81=E7=BA=B8api=E5=9C=B0?= =?UTF-8?q?=E5=9D=80,=E8=BF=94=E5=9B=9E=E4=B8=AD=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=AD=E5=85=81=E8=AE=B8=E7=9A=84=E5=9B=BE=E7=89=87=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8E=E7=BC=80=E6=A0=BC=E5=BC=8F=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E9=83=BD=E4=BC=9A=E8=BF=94=E5=9B=9E=E4=BD=9C=E4=B8=BA=E5=A3=81?= =?UTF-8?q?=E7=BA=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/web.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/utils/web.py b/app/utils/web.py index 55adce24..72a0fe86 100644 --- a/app/utils/web.py +++ b/app/utils/web.py @@ -143,8 +143,8 @@ class WebUtils: 获取自定义壁纸api壁纸 """ # 判断是否存在自定义壁纸api - if settings.CUSTOMIZE_WALLPAPER_API_KEY is not None and len(settings.CUSTOMIZE_WALLPAPER_API_KEY) > 0: - url = settings.CUSTOMIZE_WALLPAPER_API_KEY + if settings.CUSTOMIZE_WALLPAPER_API_URL is not None and len(settings.CUSTOMIZE_WALLPAPER_API_URL) > 0: + url = settings.CUSTOMIZE_WALLPAPER_API_URL wallpaper_list = [] resp = RequestUtils(timeout=5).get_res(url) if resp and resp.status_code == 200: