From 8761c82afeb0393ae1a5f56efbc410da5112a4a0 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 29 May 2025 07:14:42 +0800 Subject: [PATCH] =?UTF-8?q?fix=20TVDB=E4=BB=A3=E7=90=86=E4=B8=8ESSL?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=20#4356?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/modules/thetvdb/__init__.py | 6 +++++- app/modules/thetvdb/tvdb_v4_official.py | 27 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/modules/thetvdb/__init__.py b/app/modules/thetvdb/__init__.py index 4ad07a16..04a6a3ff 100644 --- a/app/modules/thetvdb/__init__.py +++ b/app/modules/thetvdb/__init__.py @@ -18,7 +18,9 @@ class TheTvDbModule(_ModuleBase): 创建或刷新 TVDB 登录会话 """ try: - self.tvdb = tvdb_v4_official.TVDB(apikey=settings.TVDB_V4_API_KEY, pin=settings.TVDB_V4_API_PIN) + self.tvdb = tvdb_v4_official.TVDB(apikey=settings.TVDB_V4_API_KEY, + pin=settings.TVDB_V4_API_PIN, + proxy=settings.PROXY) except Exception as e: logger.error(f"TVDB 登录失败: {str(e)}") @@ -75,6 +77,8 @@ class TheTvDbModule(_ModuleBase): """ 测试模块连接性 """ + if not self.tvdb: + return False, "TheTVDB 连接失败" try: self._handle_tvdb_call(self.tvdb.get_series, 81189) return True, "" diff --git a/app/modules/thetvdb/tvdb_v4_official.py b/app/modules/thetvdb/tvdb_v4_official.py index be5ab4d8..7dccdfab 100644 --- a/app/modules/thetvdb/tvdb_v4_official.py +++ b/app/modules/thetvdb/tvdb_v4_official.py @@ -4,6 +4,7 @@ __author__ = "Weylin Wagnon" __version__ = "1.0.12" import json +import ssl import string import urllib import urllib.request @@ -12,16 +13,21 @@ from urllib.error import HTTPError class Auth: - def __init__(self, url, apikey, pin=""): + def __init__(self, url, apikey, pin="", proxy=None): loginInfo = {"apikey": apikey} if pin != "": loginInfo["pin"] = pin loginInfoBytes = json.dumps(loginInfo, indent=2).encode("utf-8") + if proxy: + proxy_handler = urllib.request.ProxyHandler(proxy) + opener = urllib.request.build_opener(proxy_handler) + urllib.request.install_opener(opener) req = urllib.request.Request(url, data=loginInfoBytes) req.add_header("Content-Type", "application/json") try: - with urllib.request.urlopen(req, data=loginInfoBytes) as response: + context = ssl._create_unverified_context() + with urllib.request.urlopen(req, context=context, data=loginInfoBytes) as response: res = json.load(response) self.token = res["data"]["token"] except HTTPError as e: @@ -33,17 +39,24 @@ class Auth: class Request: - def __init__(self, auth_token): + def __init__(self, auth_token, proxy=None): self.auth_token = auth_token self.links = None + self.proxy = proxy def make_request(self, url, if_modified_since=None): + """Makes a request to the given URL and returns the data""" + if self.proxy: + proxy_handler = urllib.request.ProxyHandler(self.proxy) + opener = urllib.request.build_opener(proxy_handler) + urllib.request.install_opener(opener) req = urllib.request.Request(url) req.add_header("Authorization", "Bearer {}".format(self.auth_token)) if if_modified_since: req.add_header("If-Modified-Since", "{}".format(if_modified_since)) try: - with urllib.request.urlopen(req) as response: + context = ssl._create_unverified_context() + with urllib.request.urlopen(req, context=context) as response: res = json.load(response) except HTTPError as e: try: @@ -87,12 +100,12 @@ class Url: class TVDB: - def __init__(self, apikey: str, pin=""): + def __init__(self, apikey: str, pin="", proxy=None): self.url = Url() login_url = self.url.construct("login") - self.auth = Auth(login_url, apikey, pin) + self.auth = Auth(login_url, apikey, pin, proxy) auth_token = self.auth.get_token() - self.request = Request(auth_token) + self.request = Request(auth_token, proxy) def get_req_links(self) -> dict: return self.request.links