Compare commits
4 Commits
4f7b6de5a6
...
7e17c71610
Author | SHA1 | Date | |
---|---|---|---|
trueold89 | 7e17c71610 |
|
|
trueold89 | 0bb8ceb6c1 |
|
|
trueold89 | d4a46ca5ad |
|
|
trueold89 | aeafc2db41 |
|
|
@ -0,0 +1,33 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from json import load as jsonLoad
|
||||
from TYPES import DirParserTypes, LogTypes
|
||||
|
||||
CFG_PATH = "config.json"
|
||||
|
||||
|
||||
def loadCFG(config_path: str) -> dict:
|
||||
with open(config_path, "r") as cfg:
|
||||
return jsonLoad(cfg)
|
||||
|
||||
|
||||
def loadParser(options: dict) -> tuple:
|
||||
dirParser = None
|
||||
parser_value = options["Main"]["DirParser"]
|
||||
for av_parser in DirParserTypes:
|
||||
if parser_value == av_parser.value:
|
||||
dirParser = av_parser
|
||||
if dirParser is None:
|
||||
raise Exception(f"Directory parser '{parser_value}' not found")
|
||||
else:
|
||||
match dirParser:
|
||||
case DirParserTypes.Jellyfin:
|
||||
JellyfinConfig = {}
|
||||
for i in options["JellyfinConfig"].keys():
|
||||
JellyfinConfig[i] = options["JellyfinConfig"][i]
|
||||
return dirParser, JellyfinConfig
|
||||
|
||||
|
||||
configuration = loadCFG(CFG_PATH)
|
||||
|
||||
dirParser, JellyfinConfig = loadParser(configuration)
|
||||
DBPath = configuration["Main"]["DBPath"]
|
|
@ -0,0 +1,33 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from sqlite3 import connect as sqconnect
|
||||
|
||||
|
||||
class DataBase(object):
|
||||
|
||||
def __init__(self, path: str, table: str) -> None:
|
||||
self.path = path
|
||||
self.table = table
|
||||
|
||||
def __execute(self, request: str, additional=None):
|
||||
with sqconnect(self.path) as db:
|
||||
cursor = db.cursor()
|
||||
if additional is not None:
|
||||
cursor.execute(request, additional)
|
||||
else:
|
||||
cursor.execute(request)
|
||||
db.commit()
|
||||
|
||||
def createTable(self, table_content: str) -> None:
|
||||
self.__execute(f'''
|
||||
CREATE TABLE IF NOT EXISTS {self.table}(
|
||||
id INTEGER PRIMARY KEY,
|
||||
{table_content}
|
||||
)
|
||||
''')
|
||||
|
||||
def insert(self, data: dict) -> None:
|
||||
fields = ", ".join(list(data.keys()))
|
||||
values = list(data.values())
|
||||
self.__execute(f'''
|
||||
INSERT INTO {self.table} ({fields}) VALUES ({str("?," * len(data))[:-1]})
|
||||
''', tuple(values))
|
|
@ -15,7 +15,7 @@ class DirectoryGetter(object):
|
|||
"""
|
||||
|
||||
@abstractmethod
|
||||
def CheckAvailability(self) -> bool:
|
||||
def validation(self) -> bool:
|
||||
"""
|
||||
Checks if the directory parsing method is available
|
||||
:return: boolean check result
|
||||
|
@ -26,12 +26,14 @@ 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 CheckAvailability(self) -> bool:
|
||||
def validation(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}")
|
||||
|
@ -40,7 +42,7 @@ class Jellyfin(DirectoryGetter):
|
|||
case 200:
|
||||
return request
|
||||
case 401:
|
||||
raise Exception("Error 401: Authorization error, check api key")
|
||||
raise Exception("Error 401: Authorization error, check API key")
|
||||
case 403:
|
||||
raise Exception("Error 403: Forbidden")
|
||||
case 404:
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
from TYPES import LogTypes
|
||||
from DataBase import DataBase
|
||||
from Config import DBPath
|
||||
|
||||
|
||||
class Log(object):
|
||||
|
||||
def __init__(self, logtype: LogTypes, location: str, message: str) -> None:
|
||||
self.logtype = logtype
|
||||
self.location = location
|
||||
self.msg = message
|
||||
self.toDB()
|
||||
|
||||
date = datetime.now().date()
|
||||
time = datetime.now().time().strftime("%H:%M:%S")
|
||||
|
||||
def __str__(self):
|
||||
return f"[{self.date} | {self.time}] [{self.logtype.value}] [{self.location}] {self.msg}"
|
||||
|
||||
def toDB(self):
|
||||
db = DataBase(DBPath, "logs")
|
||||
db.createTable("date TEXT, time TEXT, type TEXT, location TEXT, message TEXT")
|
||||
db.insert({
|
||||
"date": self.date,
|
||||
"time": self.time,
|
||||
"type": self.logtype.value,
|
||||
"location": self.location,
|
||||
"message": self.msg
|
||||
})
|
11
TYPES.py
11
TYPES.py
|
@ -1 +1,12 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class DirParserTypes(Enum):
|
||||
Jellyfin = "Jellyfin"
|
||||
|
||||
|
||||
class LogTypes(Enum):
|
||||
LOG = "Log"
|
||||
ERROR = "Error"
|
||||
WARN = "Warning"
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"Main": {
|
||||
"DirParser": "Jellyfin",
|
||||
"DBPath": "/etc/qbitbot/botdb.db"
|
||||
},
|
||||
|
||||
"JellyfinConfig": {
|
||||
"ServerURL": "https://yourdomain.com",
|
||||
"APIKey": "youaractualjellyfintoken"
|
||||
}
|
||||
}
|
Reference in New Issue