4
1
Fork 1

Init DirectoryGetter.py

- Add the abstract class DirectoryGetter
- Made DirectoryGetter implementation for JellyfinAPI
This commit is contained in:
trueold89 2024-05-03 16:18:46 +03:00
parent b1e2fb98c4
commit 4f7b6de5a6
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
2 changed files with 54 additions and 1 deletions

54
DirectoryGetter.py Normal file
View File

@ -0,0 +1,54 @@
# -*- 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 CheckAvailability(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
def CheckAvailability(self) -> bool:
if self.__get("Library/VirtualFolders").status_code == 200:
return True
else:
raise Exception("Error connecting to JellyfinAPI")
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

View File

@ -1 +0,0 @@
# -*- coding: utf-8 -*-