fix requests

This commit is contained in:
jxxghp
2024-04-10 22:16:10 +08:00
parent 294b4a6bf9
commit a3603f79c8
15 changed files with 567 additions and 556 deletions

View File

@ -20,7 +20,7 @@ class BangumiModule(_ModuleBase):
"""
测试模块连接性
"""
ret = RequestUtils().get_res("https://api.bgm.tv/")
with RequestUtils().get_res("https://api.bgm.tv/") as ret:
if ret and ret.status_code == 200:
return True, ""
elif ret:

View File

@ -30,13 +30,13 @@ class DoubanModule(_ModuleBase):
self.cache = DoubanCache()
def stop(self):
pass
self.doubanapi.close()
def test(self) -> Tuple[bool, str]:
"""
测试模块连接性
"""
ret = RequestUtils().get_res("https://movie.douban.com/")
with RequestUtils().get_res("https://movie.douban.com/") as ret:
if ret and ret.status_code == 200:
return True, ""
elif ret:

View File

@ -210,7 +210,7 @@ class DoubanApi(metaclass=Singleton):
},
data={
"apikey": "0ab215a8b1977939201640fa14c66bab",
},
}
)
"""
req_url = self._api_url + url
@ -481,6 +481,6 @@ class DoubanApi(metaclass=Singleton):
"""
self.__invoke.cache_clear()
def __del__(self):
def close(self):
if self._session:
self._session.close()

View File

@ -193,7 +193,7 @@ class DoubanScraper:
url = url.replace("/format/webp", "/format/jpg")
file_path.with_suffix(".jpg")
logger.info(f"正在下载{file_path.stem}图片:{url} ...")
r = RequestUtils().get_res(url=url)
with RequestUtils().get_res(url=url) as r:
if r:
if self._transfer_type in ['rclone_move', 'rclone_copy']:
self.__save_remove_file(file_path, r.content)

View File

@ -56,7 +56,7 @@ class Emby:
return []
req_url = "%semby/Library/SelectableMediaFolders?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json()
else:
@ -74,7 +74,7 @@ class Emby:
return []
req_url = "%semby/Library/VirtualFolders/Query?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
library_items = res.json().get("Items")
librarys = []
@ -113,7 +113,7 @@ class Emby:
user = self.user
req_url = f"{self._host}emby/Users/{user}/Views?api_key={self._apikey}"
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json().get("Items")
else:
@ -164,7 +164,7 @@ class Emby:
return None
req_url = "%sUsers?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
users = res.json()
# 先查询是否有与当前用户名称匹配的
@ -227,7 +227,7 @@ class Emby:
return None
req_url = "%sSystem/Info?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json().get("Id")
else:
@ -245,7 +245,7 @@ class Emby:
return 0
req_url = "%semby/Users/Query?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json().get("TotalRecordCount")
else:
@ -264,7 +264,7 @@ class Emby:
return schemas.Statistic()
req_url = "%semby/Items/Counts?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
result = res.json()
return schemas.Statistic(
@ -299,7 +299,7 @@ class Emby:
"&api_key=%s") % (
self._host, name, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
res_items = res.json().get("Items")
if res_items:
@ -329,7 +329,7 @@ class Emby:
"&Recursive=true&SearchTerm=%s&Limit=10&IncludeSearchTypes=false&api_key=%s" % (
self._host, title, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
res_items = res.json().get("Items")
if res_items:
@ -401,7 +401,7 @@ class Emby:
try:
req_url = "%semby/Shows/%s/Episodes?Season=%s&IsMissing=false&api_key=%s" % (
self._host, item_id, season, self._apikey)
res_json = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res_json:
if res_json:
tv_item = res_json.json()
res_items = tv_item.get("Items")
@ -464,7 +464,7 @@ class Emby:
req_url = "%sItems/%s/Images/%s" % (self._playhost, item_id, image_type)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res and res.status_code != 404:
logger.info("影片图片链接:{}".format(res.url))
return res.url
@ -483,7 +483,7 @@ class Emby:
return False
req_url = "%semby/Items/%s/Refresh?Recursive=true&api_key=%s" % (self._host, item_id, self._apikey)
try:
res = RequestUtils().post_res(req_url)
with RequestUtils().post_res(req_url) as res:
if res:
return True
else:
@ -501,7 +501,7 @@ class Emby:
return False
req_url = "%semby/Library/Refresh?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().post_res(req_url)
with RequestUtils().post_res(req_url) as res:
if res:
return True
else:
@ -580,7 +580,7 @@ class Emby:
return None
req_url = "%semby/Users/%s/Items/%s?api_key=%s" % (self._host, self.user, itemid, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res and res.status_code == 200:
item = res.json()
tmdbid = item.get("ProviderIds", {}).get("Tmdb")
@ -611,7 +611,7 @@ class Emby:
yield None
req_url = "%semby/Users/%s/Items?ParentId=%s&api_key=%s" % (self._host, self.user, parent, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res and res.status_code == 200:
results = res.json().get("Items") or []
for result in results:
@ -1033,7 +1033,7 @@ class Emby:
req_url = (f"{self._host}Users/{user}/Items/Resume?"
f"Limit=100&MediaTypes=Video&api_key={self._apikey}&Fields=ProductionYear,Path")
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
result = res.json().get("Items") or []
ret_resume = []
@ -1096,7 +1096,7 @@ class Emby:
req_url = (f"{self._host}Users/{user}/Items/Latest?"
f"Limit=100&MediaTypes=Video&api_key={self._apikey}&Fields=ProductionYear,Path")
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
result = res.json() or []
ret_latest = []

View File

@ -321,7 +321,7 @@ class FanartModule(_ModuleBase):
"""
测试模块连接性
"""
ret = RequestUtils().get_res("https://webservice.fanart.tv")
with RequestUtils().get_res("https://webservice.fanart.tv") as ret:
if ret and ret.status_code == 200:
return True, ""
elif ret:

View File

@ -52,7 +52,7 @@ class Jellyfin:
return []
req_url = "%sLibrary/SelectableMediaFolders?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json()
else:
@ -70,7 +70,7 @@ class Jellyfin:
return []
req_url = "%sLibrary/VirtualFolders?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
library_items = res.json()
librarys = []
@ -109,7 +109,7 @@ class Jellyfin:
user = self.user
req_url = f"{self._host}Users/{user}/Views?api_key={self._apikey}"
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json().get("Items")
else:
@ -163,7 +163,7 @@ class Jellyfin:
return 0
req_url = "%sUsers?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return len(res.json())
else:
@ -181,7 +181,7 @@ class Jellyfin:
return None
req_url = "%sUsers?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
users = res.json()
# 先查询是否有与当前用户名称匹配的
@ -244,7 +244,7 @@ class Jellyfin:
return None
req_url = "%sSystem/Info?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json().get("Id")
else:
@ -262,7 +262,7 @@ class Jellyfin:
return schemas.Statistic()
req_url = "%sItems/Counts?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
result = res.json()
return schemas.Statistic(
@ -287,7 +287,7 @@ class Jellyfin:
"api_key=%s&searchTerm=%s&IncludeItemTypes=Series&Limit=10&Recursive=true") % (
self._host, self.user, self._apikey, name)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
res_items = res.json().get("Items")
if res_items:
@ -317,7 +317,7 @@ class Jellyfin:
"api_key=%s&searchTerm=%s&IncludeItemTypes=Movie&Limit=10&Recursive=true") % (
self._host, self.user, self._apikey, title)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
res_items = res.json().get("Items")
if res_items:
@ -387,7 +387,7 @@ class Jellyfin:
try:
req_url = "%sShows/%s/Episodes?season=%s&&userId=%s&isMissing=false&api_key=%s" % (
self._host, item_id, season, self.user, self._apikey)
res_json = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res_json:
if res_json:
tv_info = res_json.json()
res_items = tv_info.get("Items")
@ -458,7 +458,7 @@ class Jellyfin:
_host = self._playhost
req_url = "%sItems/%s/Images/%s" % (_host, item_id, image_type)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res and res.status_code != 404:
logger.info("影片图片链接:{}".format(res.url))
return res.url
@ -479,7 +479,7 @@ class Jellyfin:
"""
req_url = "%sItems/%s/Ancestors?api_key=%s" % (self._host, item_id, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
return res.json()[index].get(key)
else:
@ -497,7 +497,7 @@ class Jellyfin:
return False
req_url = "%sLibrary/Refresh?api_key=%s" % (self._host, self._apikey)
try:
res = RequestUtils().post_res(req_url)
with RequestUtils().post_res(req_url) as res:
if res:
return True
else:
@ -632,7 +632,7 @@ class Jellyfin:
req_url = "%sUsers/%s/Items/%s?api_key=%s" % (
self._host, self.user, itemid, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res and res.status_code == 200:
item = res.json()
tmdbid = item.get("ProviderIds", {}).get("Tmdb")
@ -663,7 +663,7 @@ class Jellyfin:
yield None
req_url = "%sUsers/%s/Items?parentId=%s&api_key=%s" % (self._host, self.user, parent, self._apikey)
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res and res.status_code == 200:
results = res.json().get("Items") or []
for result in results:
@ -761,7 +761,7 @@ class Jellyfin:
req_url = (f"{self._host}Users/{user}/Items/Resume?"
f"Limit=100&MediaTypes=Video&api_key={self._apikey}&Fields=ProductionYear,Path")
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
result = res.json().get("Items") or []
ret_resume = []
@ -784,7 +784,7 @@ class Jellyfin:
else:
image = self.__get_local_image_by_id(item.get("Id"))
# 小部分剧集无[xxx-S01E01-thumb.jpg]图片
image_res = RequestUtils().get_res(image)
with RequestUtils().get_res(image) as image_res:
if not image_res or image_res.status_code == 404:
image = self.generate_image_link(item.get("Id"), "Backdrop", False)
if item_type == MediaType.MOVIE.value:
@ -822,7 +822,7 @@ class Jellyfin:
req_url = (f"{self._host}Users/{user}/Items/Latest?"
f"Limit=100&MediaTypes=Video&api_key={self._apikey}&Fields=ProductionYear,Path")
try:
res = RequestUtils().get_res(req_url)
with RequestUtils().get_res(req_url) as res:
if res:
result = res.json() or []
ret_latest = []

View File

@ -41,31 +41,31 @@ class Slack:
# 注册消息响应
@slack_app.event("message")
def slack_message(message):
local_res = requests.post(self._ds_url, json=message, timeout=10)
with requests.post(self._ds_url, json=message, timeout=10) as local_res:
logger.debug("message: %s processed, response is: %s" % (message, local_res.text))
@slack_app.action(re.compile(r"actionId-\d+"))
def slack_action(ack, body):
ack()
local_res = requests.post(self._ds_url, json=body, timeout=60)
with requests.post(self._ds_url, json=body, timeout=60) as local_res:
logger.debug("message: %s processed, response is: %s" % (body, local_res.text))
@slack_app.event("app_mention")
def slack_mention(say, body):
say(f"收到,请稍等... <@{body.get('event', {}).get('user')}>")
local_res = requests.post(self._ds_url, json=body, timeout=10)
with requests.post(self._ds_url, json=body, timeout=10) as local_res:
logger.debug("message: %s processed, response is: %s" % (body, local_res.text))
@slack_app.shortcut(re.compile(r"/*"))
def slack_shortcut(ack, body):
ack()
local_res = requests.post(self._ds_url, json=body, timeout=10)
with requests.post(self._ds_url, json=body, timeout=10) as local_res:
logger.debug("message: %s processed, response is: %s" % (body, local_res.text))
@slack_app.command(re.compile(r"/*"))
def slack_command(ack, body):
ack()
local_res = requests.post(self._ds_url, json=body, timeout=10)
with requests.post(self._ds_url, json=body, timeout=10) as local_res:
logger.debug("message: %s processed, response is: %s" % (body, local_res.text))
# 启动服务

View File

@ -12,7 +12,7 @@ from app.modules import _ModuleBase
from app.modules.themoviedb.category import CategoryHelper
from app.modules.themoviedb.scraper import TmdbScraper
from app.modules.themoviedb.tmdb_cache import TmdbCache
from app.modules.themoviedb.tmdbapi import TmdbHelper
from app.modules.themoviedb.tmdbapi import TmdbApi
from app.schemas.types import MediaType, MediaImageType
from app.utils.http import RequestUtils
from app.utils.system import SystemUtils
@ -26,7 +26,7 @@ class TheMovieDbModule(_ModuleBase):
# 元数据缓存
cache: TmdbCache = None
# TMDB
tmdb: TmdbHelper = None
tmdb: TmdbApi = None
# 二级分类
category: CategoryHelper = None
# 刮削器
@ -34,12 +34,13 @@ class TheMovieDbModule(_ModuleBase):
def init_module(self) -> None:
self.cache = TmdbCache()
self.tmdb = TmdbHelper()
self.tmdb = TmdbApi()
self.category = CategoryHelper()
self.scraper = TmdbScraper(self.tmdb)
def stop(self):
self.cache.save()
self.tmdb.close()
def test(self) -> Tuple[bool, str]:
"""

View File

@ -15,7 +15,7 @@ from .tmdbv3api import TMDb, Search, Movie, TV, Season, Episode, Discover, Trend
from .tmdbv3api.exceptions import TMDbException
class TmdbHelper:
class TmdbApi:
"""
TMDB识别匹配
"""
@ -1271,3 +1271,9 @@ class TmdbHelper:
except Exception as e:
print(str(e))
return {}
def close(self):
"""
关闭连接
"""
self.tmdb.close()

View File

@ -222,6 +222,6 @@ class TMDb(object):
return json.get(key)
return json
def __del__(self):
def close(self):
if self._session:
self._session.close()

View File

@ -17,7 +17,7 @@ class TheTvDbModule(_ModuleBase):
proxies=settings.PROXY)
def stop(self):
pass
self.tvdb.close()
def test(self) -> Tuple[bool, str]:
"""

View File

@ -733,6 +733,10 @@ class Tvdb:
}
self.proxies = proxies
def close(self):
if self.session:
self.session.close()
@staticmethod
def _getTempDir():
"""Returns the [system temp dir]/tvdb_api-u501 (or
@ -764,9 +768,9 @@ class Tvdb:
if not self.__authorized:
# only authorize of we haven't before and we
# don't have the url in the cache
fake_session_for_key = requests.Session()
fake_session_for_key.headers['Accept-Language'] = language
cache_key = None
with requests.Session() as fake_session_for_key:
fake_session_for_key.headers['Accept-Language'] = language
try:
# in case the session class has no cache object, fail gracefully
cache_key = self.session.cache.create_key(

View File

@ -71,7 +71,7 @@ class WeChat:
return None
try:
token_url = self._token_url % (self._corpid, self._appsecret)
res = RequestUtils().get_res(token_url)
with RequestUtils().get_res(token_url) as res:
if res:
ret_json = res.json()
if ret_json.get('errcode') == 0:

View File

@ -40,7 +40,7 @@ class WebUtils:
}
"""
try:
r = RequestUtils().get_res(f"https://api.mir6.com/api/ip?ip={ip}&type=json")
with RequestUtils().get_res(f"https://api.mir6.com/api/ip?ip={ip}&type=json") as r:
if r:
return r.json().get("data", {}).get("location") or ''
except Exception as err:
@ -65,7 +65,7 @@ class WebUtils:
}
"""
try:
r = RequestUtils().get_res(f"https://whois.pconline.com.cn/ipJson.jsp?json=true&ip={ip}")
with RequestUtils().get_res(f"https://whois.pconline.com.cn/ipJson.jsp?json=true&ip={ip}") as r:
if r:
return r.json().get("addr") or ''
except Exception as err: