# -*- coding: utf-8 -*- from requests import get as req_get from requests import Response from abc import ABCMeta, abstractmethod class DirectoryGetter(object): __metaclass__ = ABCMeta @abstractmethod def getDirs(self) -> dict: """ Returns a list key:value - Directory name : directory path :return: Dictionary - Directory name : directory path """ @abstractmethod def validation(self) -> bool: """ Checks if the directory parsing method is available :return: boolean check result """ class Jellyfin(DirectoryGetter): def __init__(self, url: str, api_key: str) -> None: self.url = url self.api_key = api_key if self.url[-1] == "/": self.url = self.url[:-1] if not (self.validation()): raise Exception("Error connecting to JellyfinAPI") def validation(self) -> bool: try: if self.__get("System/Info").json()["Id"] is not None: return True except: return False def __get(self, api_path: str) -> Response: request = req_get(f"{self.url}/{api_path}?api_key={self.api_key}") status = request.status_code match status: case 200: return request case 401: raise Exception("Error 401: Authorization error, check API key") case 403: raise Exception("Error 403: Forbidden") case 404: raise Exception("Error 404: Not found") def getDirs(self) -> dict: folders_list = self.__get("Library/VirtualFolders").json() out = {} for folder in folders_list: out[folder["Name"]] = folder["Locations"][0] return out