jellyfin/src/connect.py

108 lines
3.1 KiB
Python
Raw Normal View History

2023-04-05 16:31:14 +00:00
"""handle connections"""
import base64
import os
import requests
from src.config import get_config
2023-04-06 04:58:04 +00:00
from src.static_types import ConfigType, TAVideo
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
CONFIG: ConfigType = get_config()
2023-04-05 16:31:14 +00:00
class Jellyfin:
"""connect to jellyfin"""
2023-04-06 04:58:04 +00:00
headers: dict = {
"Authorization": "MediaBrowser Token=" + CONFIG["jf_token"]
}
base: str = CONFIG["jf_url"]
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def get(self, path: str) -> dict:
2023-04-05 16:31:14 +00:00
"""make a get request"""
2023-04-06 04:58:04 +00:00
url: str = f"{self.base}/{path}"
2023-04-05 16:31:14 +00:00
response = requests.get(url, headers=self.headers, timeout=10)
if response.ok:
return response.json()
print(response.text)
2023-04-06 04:58:04 +00:00
return {}
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def post(self, path: str, data: dict | bool) -> None:
2023-04-05 16:31:14 +00:00
"""make a post request"""
2023-04-06 04:58:04 +00:00
url: str = f"{self.base}/{path}"
2023-04-05 16:31:14 +00:00
response = requests.post(
url, headers=self.headers, json=data, timeout=10
)
if not response.ok:
print(response.text)
2023-04-06 04:58:04 +00:00
def post_img(self, path: str, thumb_base64: bytes) -> None:
2023-04-05 16:31:14 +00:00
"""set image"""
2023-04-06 04:58:04 +00:00
url: str = f"{self.base}/{path}"
new_headers: dict = self.headers.copy()
2023-04-05 16:31:14 +00:00
new_headers.update({"Content-Type": "image/jpeg"})
response = requests.post(
url, headers=new_headers, data=thumb_base64, timeout=10
)
if not response.ok:
print(response.text)
2023-04-06 04:58:04 +00:00
def ping(self) -> None:
2023-04-05 16:31:14 +00:00
"""ping the server"""
2023-04-06 04:58:04 +00:00
response = self.get("Users")
2023-04-05 16:31:14 +00:00
if not response:
raise ConnectionError("failed to connect to jellyfin")
print("[connection] verified jellyfin connection")
class TubeArchivist:
"""connect to Tube Archivist"""
2023-04-06 04:58:04 +00:00
ta_token: str = CONFIG["ta_token"]
headers: dict = {"Authorization": f"Token {ta_token}"}
base: str = CONFIG["ta_url"]
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def get(self, path: str) -> TAVideo:
2023-04-05 16:31:14 +00:00
"""get document from ta"""
2023-04-06 04:58:04 +00:00
url: str = f"{self.base}/api/{path}"
2023-04-05 16:31:14 +00:00
response = requests.get(url, headers=self.headers, timeout=10)
if response.ok:
response_json = response.json()
if "data" in response_json:
return response.json().get("data")
return response.json()
2023-04-06 04:58:04 +00:00
raise ValueError(f"video not found in TA: {path}")
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def get_thumb(self, path: str) -> bytes:
2023-04-05 16:31:14 +00:00
"""get encoded thumbnail from ta"""
2023-04-06 04:58:04 +00:00
url: str = CONFIG["ta_url"] + path
2023-04-05 16:31:14 +00:00
response = requests.get(
url, headers=self.headers, stream=True, timeout=10
)
2023-04-06 04:58:04 +00:00
base64_thumb: bytes = base64.b64encode(response.content)
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
return base64_thumb
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def ping(self) -> None:
2023-04-05 16:31:14 +00:00
"""ping tubearchivist server"""
response = self.get("ping/")
if not response:
raise ConnectionError("failed to connect to tube archivist")
print("[connection] verified tube archivist connection")
2023-04-06 04:58:04 +00:00
def env_check() -> None:
2023-04-05 16:31:14 +00:00
"""check if ta_video_path is accessible"""
if not os.path.exists("config.json"):
raise FileNotFoundError("config.json file not found")
if not os.path.exists(CONFIG["ta_video_path"]):
raise FileNotFoundError("failed to access ta_video_path")