Add JellyfinAPI implementation of DirGetter

This commit is contained in:
trueold89 2024-08-05 15:22:29 +03:00
parent b00c60f448
commit ebca2172f4
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
3 changed files with 58 additions and 1 deletions

View File

@ -8,5 +8,5 @@ setup(
author_email="root@orudo.ru",
description="A simple Telegram bot that will allow you to upload torrent files / magnet links to a remote Torrent server (qBitTorrent, Transmission, etc.)",
install_requires=["aiohttp>=3.10.0", "aiofiles>=24.1.0", "aiofiles>=24.1.0"],
packages=["tubot", "tubot.static", "tubot.torrent"],
packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter"],
)

View File

@ -14,6 +14,7 @@ class DirGetter(IValidatable, ABC):
"""
DirectoryGetter Abstract class
"""
_gtype: GetterTypes
def __init__(self) -> None:

View File

@ -9,12 +9,14 @@ from tubot.dirgetter.types import GetterTypes
from tubot.dirgetter.abc import DirGetter
from aiofiles.os import listdir
from aiofiles.ospath import isdir
from aiohttp import ClientResponse, ClientSession
class OSGetter(DirGetter):
"""
Python.os module implementation of DirectoryGetter
"""
_gtype = GetterTypes.OS
base_dir: str
@ -38,3 +40,57 @@ class OSGetter(DirGetter):
async def __validate__(self) -> bool:
return await isdir(self.base_dir)
class Jellyfin(DirGetter):
"""
Jellyfin API implementation of DirectoryGetter
"""
_gtype = GetterTypes.Jellyfin
host: str
token: str
def __init__(self, host: str, api_token: str) -> None:
"""
:param host: Adress of Jellyfin server
:param api_token: Jellyfin API Token for auth
"""
super().__init__()
self.host = host
self.token = api_token
async def _get(self, api: str) -> ClientResponse:
async with ClientSession() as session:
resp = await session.get(f"{self.host}/{api}?api_key={self.token}")
status = resp.status
match status:
case 200:
return resp
case 401:
raise ConnectionError("401: Auth error")
case 403:
raise ConnectionError("403: Forbidden")
case 404:
raise ConnectionError("403: Not found")
raise ConnectionError()
@property
async def idx(self) -> str | None:
resp = await self._get("System/Info")
json = await resp.json()
return json["Id"]
@property
async def folders(self) -> dict:
resp = await self._get("Library/VirtualFolders")
json = await resp.json()
dirs = {}
for folder in json:
dirs[folder["Name"]] = folder["Locations"][0]
return dirs
async def __validate__(self) -> bool:
if await self.idx is not None:
return True
return False