Init DirectoryGetter.py
- Add the abstract class DirectoryGetter - Made DirectoryGetter implementation for JellyfinAPI
This commit is contained in:
parent
b1e2fb98c4
commit
4f7b6de5a6
|
@ -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
|
|
@ -1 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
Reference in New Issue