feat:特定默认UA

This commit is contained in:
jxxghp
2025-07-14 12:35:08 +08:00
parent f260990b86
commit 7a1cbf70e3
2 changed files with 52 additions and 2 deletions
+7 -2
View File
@@ -233,8 +233,6 @@ class ConfigModel(BaseModel):
COOKIECLOUD_INTERVAL: Optional[int] = 60 * 24 COOKIECLOUD_INTERVAL: Optional[int] = 60 * 24
# CookieCloud同步黑名单,多个域名,分割 # CookieCloud同步黑名单,多个域名,分割
COOKIECLOUD_BLACKLIST: Optional[str] = None COOKIECLOUD_BLACKLIST: Optional[str] = None
# CookieCloud对应的浏览器UA
USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57"
# 电影重命名格式 # 电影重命名格式
MOVIE_RENAME_FORMAT: str = "{{title}}{% if year %} ({{year}}){% endif %}" \ MOVIE_RENAME_FORMAT: str = "{{title}}{% if year %} ({{year}}){% endif %}" \
"/{{title}}{% if year %} ({{year}}){% endif %}{% if part %}-{{part}}{% endif %}{% if videoFormat %} - {{videoFormat}}{% endif %}" \ "/{{title}}{% if year %} ({{year}}){% endif %}{% if part %}-{{part}}{% endif %}{% if videoFormat %} - {{videoFormat}}{% endif %}" \
@@ -510,6 +508,13 @@ class Settings(BaseSettings, ConfigModel, LogConfigModel):
""" """
return "v2" return "v2"
@property
def USER_AGENT(self) -> str:
"""
全局用户代理字符串
"""
return f"{self.PROJECT_NAME}/{self.VERSION_FLAG}"
@property @property
def INNER_CONFIG_PATH(self): def INNER_CONFIG_PATH(self):
return self.ROOT_PATH / "config" return self.ROOT_PATH / "config"
+45
View File
@@ -1,5 +1,7 @@
import sys
import re import re
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path
from typing import Any, Optional, Union from typing import Any, Optional, Union
import chardet import chardet
@@ -8,6 +10,7 @@ import urllib3
from requests import Response, Session from requests import Response, Session
from urllib3.exceptions import InsecureRequestWarning from urllib3.exceptions import InsecureRequestWarning
from app.core.config import settings
from app.log import logger from app.log import logger
urllib3.disable_warnings(InsecureRequestWarning) urllib3.disable_warnings(InsecureRequestWarning)
@@ -86,6 +89,7 @@ class AutoCloseResponse:
def __exit__(self, *args): def __exit__(self, *args):
self.close() self.close()
class RequestUtils: class RequestUtils:
def __init__(self, def __init__(self,
@@ -106,6 +110,10 @@ class RequestUtils:
if headers: if headers:
self._headers = headers self._headers = headers
else: else:
if ua and ua == settings.USER_AGENT:
caller_name = self.__get_caller()
if caller_name:
ua = f"{settings.USER_AGENT} Plugin/{caller_name}"
self._headers = { self._headers = {
"User-Agent": ua, "User-Agent": ua,
"Content-Type": content_type, "Content-Type": content_type,
@@ -120,6 +128,43 @@ class RequestUtils:
else: else:
self._cookies = None self._cookies = None
@staticmethod
def __get_caller():
"""
获取调用者的名称,识别是否为插件调用
"""
# 调用者名称
caller_name = None
try:
frame = sys._getframe(3) # noqa
except (AttributeError, ValueError):
return None
while frame:
filepath = Path(frame.f_code.co_filename)
parts = filepath.parts
if "app" in parts:
if not caller_name and "plugins" in parts:
try:
plugins_index = parts.index("plugins")
if plugins_index + 1 < len(parts):
plugin_candidate = parts[plugins_index + 1]
if plugin_candidate != "__init__.py":
caller_name = plugin_candidate
break
except ValueError:
pass
if "main.py" in parts:
break
elif len(parts) != 1:
break
try:
frame = frame.f_back
except AttributeError:
break
return caller_name
def request(self, method: str, url: str, raise_exception: bool = False, **kwargs) -> Optional[Response]: def request(self, method: str, url: str, raise_exception: bool = False, **kwargs) -> Optional[Response]:
""" """
发起HTTP请求 发起HTTP请求