4
1
Fork 1
This repository has been archived on 2024-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
qBitDownload-Bot/DirectoryGetter.py

55 lines
1.6 KiB
Python
Raw Normal View History

# -*- 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