Skip to content

Commit

Permalink
Merge pull request #2365 from InfinityPacer/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp authored Jun 19, 2024
2 parents 79c57d8 + 6392ee6 commit 0da0839
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 141 deletions.
10 changes: 2 additions & 8 deletions app/modules/emby/emby.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ class Emby:
def __init__(self):
self._host = settings.EMBY_HOST
if self._host:
if not self._host.endswith("/"):
self._host += "/"
if not self._host.startswith("http"):
self._host = "http://" + self._host
self._host = RequestUtils.standardize_base_url(self._host)
self._playhost = settings.EMBY_PLAY_HOST
if self._playhost:
if not self._playhost.endswith("/"):
self._playhost += "/"
if not self._playhost.startswith("http"):
self._playhost = "http://" + self._playhost
self._playhost = RequestUtils.standardize_base_url(self._playhost)
self._apikey = settings.EMBY_API_KEY
self.user = self.get_user(settings.SUPERUSER)
self.folders = self.get_emby_folders()
Expand Down
10 changes: 2 additions & 8 deletions app/modules/jellyfin/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ class Jellyfin:
def __init__(self):
self._host = settings.JELLYFIN_HOST
if self._host:
if not self._host.endswith("/"):
self._host += "/"
if not self._host.startswith("http"):
self._host = "http://" + self._host
self._host = RequestUtils.standardize_base_url(self._host)
self._playhost = settings.JELLYFIN_PLAY_HOST
if self._playhost:
if not self._playhost.endswith("/"):
self._playhost += "/"
if not self._playhost.startswith("http"):
self._playhost = "http://" + self._playhost
self._playhost = RequestUtils.standardize_base_url(self._playhost)
self._apikey = settings.JELLYFIN_API_KEY
self.user = self.get_user(settings.SUPERUSER)
self.serverid = self.get_server_id()
Expand Down
82 changes: 74 additions & 8 deletions app/modules/plex/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@
from cachetools import TTLCache, cached
from plexapi import media
from plexapi.server import PlexServer
from requests import Response, Session

from app import schemas
from app.core.config import settings
from app.log import logger
from app.schemas import MediaType
from app.utils.http import RequestUtils


class Plex:
_plex = None
_session = None

def __init__(self):
self._host = settings.PLEX_HOST
if self._host:
if not self._host.endswith("/"):
self._host += "/"
if not self._host.startswith("http"):
self._host = "http://" + self._host
self._host = RequestUtils.standardize_base_url(self._host)
self._playhost = settings.PLEX_PLAY_HOST
if self._playhost:
if not self._playhost.endswith("/"):
self._playhost += "/"
if not self._playhost.startswith("http"):
self._playhost = "http://" + self._playhost
self._playhost = RequestUtils.standardize_base_url(self._playhost)
self._token = settings.PLEX_TOKEN
if self._host and self._token:
try:
Expand All @@ -37,6 +34,7 @@ def __init__(self):
except Exception as e:
self._plex = None
logger.error(f"Plex服务器连接失败:{str(e)}")
self._session = self.__adapt_plex_session()

def is_inactive(self) -> bool:
"""
Expand Down Expand Up @@ -727,3 +725,71 @@ def get_latest(self, num: int = 20) -> Optional[List[schemas.MediaServerPlayItem
))
offset += num
return ret_resume[:num]

def get_data(self, endpoint: str, **kwargs) -> Optional[Response]:
"""
自定义从媒体服务器获取数据
:param endpoint: 端点
:param kwargs: 其他请求参数,如headers, cookies, proxies等
"""
return self.__request(method="get", endpoint=endpoint, **kwargs)

def post_data(self, endpoint: str, **kwargs) -> Optional[Response]:
"""
自定义从媒体服务器获取数据
:param endpoint: 端点
:param kwargs: 其他请求参数,如headers, cookies, proxies等
"""
return self.__request(method="post", endpoint=endpoint, **kwargs)

def put_data(self, endpoint: str, **kwargs) -> Optional[Response]:
"""
自定义从媒体服务器获取数据
:param endpoint: 端点
:param kwargs: 其他请求参数,如headers, cookies, proxies等
"""
return self.__request(method="put", endpoint=endpoint, **kwargs)

def __request(self, method: str, endpoint: str, **kwargs) -> Optional[Response]:
"""
自定义从媒体服务器获取数据
:param method: HTTP方法,如 get, post, put 等
:param endpoint: 端点
:param kwargs: 其他请求参数,如headers, cookies, proxies等
"""
if not self._session:
return
try:
url = RequestUtils.adapt_request_url(host=self._host, endpoint=endpoint)
kwargs.setdefault("headers", self.__get_request_headers())
kwargs.setdefault("raise_exception", True)
request_method = getattr(RequestUtils(session=self._session), f"{method}_res", None)
if request_method:
return request_method(url=url, **kwargs)
else:
logger.error(f"方法 {method} 不存在")
return None
except Exception as e:
logger.error(f"连接Plex出错:" + str(e))
return None

@staticmethod
def __get_request_headers() -> dict:
"""获取请求头"""
return {
"X-Plex-Token": settings.PLEX_TOKEN,
"Accept": "application/json",
"Content-Type": "application/json"
}

@staticmethod
def __adapt_plex_session() -> Session:
"""
创建并配置一个针对Plex服务的requests.Session实例
这个会话包括特定的头部信息,用于处理所有的Plex请求
"""
# 设置请求头部,通常包括验证令牌和接受/内容类型头部
headers = Plex.__get_request_headers()
session = Session()
session.headers = headers
return session
Loading

0 comments on commit 0da0839

Please sign in to comment.