From ebca2172f4a795ecf9665f8a09fa6ffe6ed47956 Mon Sep 17 00:00:00 2001 From: trueold89 Date: Mon, 5 Aug 2024 15:22:29 +0300 Subject: [PATCH] Add JellyfinAPI implementation of DirGetter --- setup.py | 2 +- tubot/dirgetter/abc.py | 1 + tubot/dirgetter/getter.py | 56 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index cd46982..d86025e 100644 --- a/setup.py +++ b/setup.py @@ -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"], ) diff --git a/tubot/dirgetter/abc.py b/tubot/dirgetter/abc.py index 3a52fe6..adad075 100644 --- a/tubot/dirgetter/abc.py +++ b/tubot/dirgetter/abc.py @@ -14,6 +14,7 @@ class DirGetter(IValidatable, ABC): """ DirectoryGetter Abstract class """ + _gtype: GetterTypes def __init__(self) -> None: diff --git a/tubot/dirgetter/getter.py b/tubot/dirgetter/getter.py index 76cfc48..59338b9 100644 --- a/tubot/dirgetter/getter.py +++ b/tubot/dirgetter/getter.py @@ -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