more code cleanup

This commit is contained in:
ArabCoders 2023-12-31 20:34:12 +03:00
parent 8f9d3e27f3
commit a30d671960
6 changed files with 44 additions and 39 deletions

View file

@ -4,6 +4,7 @@ import os
import re
import sys
import coloredlogs
from version import APP_VERSION
class Config:
@ -33,7 +34,10 @@ class Config:
logging_level: str = 'info'
_boolean_vars: tuple = ('keep_archive', 'ytdl_debug')
version: str = APP_VERSION
_boolean_vars: tuple = ('keep_archive', 'ytdl_debug',)
_immutable: tuple = ('version',)
def __init__(self):
baseDefualtPath: str = os.path.dirname(os.path.dirname(__file__))
@ -46,6 +50,11 @@ class Config:
if k.startswith('_'):
continue
# If the variable is not updateable, set it to the default value.
if k in self._immutable:
setattr(self, k, v)
continue
lookUpKey: str = f'YTP_{k}'.upper()
setattr(
self, k,
@ -53,7 +62,7 @@ class Config:
)
for k, v in self.__dict__.items():
if k.startswith('_'):
if k.startswith('_') or k in self._immutable:
continue
if isinstance(v, str) and '{' in v and '}' in v:

View file

@ -7,9 +7,8 @@ import os
import re
import shutil
import yt_dlp
from Utils import get_format, get_opts, jsonCookie, mergeConfig
from Utils import Notifier, get_format, get_opts, jsonCookie, mergeConfig
from ItemDTO import ItemDTO
from Notifier import Notifier
log = logging.getLogger('download')

View file

@ -6,11 +6,10 @@ import time
import yt_dlp
from sqlite3 import Connection
from Config import Config
from Notifier import Notifier
from Download import Download
from ItemDTO import ItemDTO
from DataStore import DataStore
from Utils import ObjectSerializer, calcDownloadPath, ExtractInfo, mergeConfig
from Utils import Notifier, ObjectSerializer, calcDownloadPath, ExtractInfo, mergeConfig
from datetime import datetime, timezone
log = logging.getLogger('DownloadQueue')

View file

@ -1,26 +0,0 @@
from socketio import AsyncServer
from Utils import ObjectSerializer
class Notifier:
def __init__(self, sio: AsyncServer, serializer: ObjectSerializer):
self.sio = sio
self.serializer = serializer
async def added(self, dl):
await self.emit('added', dl)
async def updated(self, dl):
await self.emit('updated', dl)
async def completed(self, dl):
await self.emit('completed', dl)
async def canceled(self, id):
await self.emit('canceled', id)
async def cleared(self, id):
await self.emit('cleared', id)
async def emit(self, event: str, data):
await self.sio.emit(event, self.serializer.encode(data))

View file

@ -5,6 +5,7 @@ import logging
import os
from typing import Any
import yt_dlp
from socketio import AsyncServer
log = logging.getLogger('Utils')
@ -274,3 +275,31 @@ class ObjectSerializer(json.JSONEncoder):
def default(self, obj):
return obj.__dict__ if isinstance(obj, object) else json.JSONEncoder.default(self, obj)
class Notifier:
'''
This class is used to send events to the frontend.
'''
def __init__(self, sio: AsyncServer, serializer: ObjectSerializer):
self.sio = sio
self.serializer = serializer
async def added(self, dl):
await self.emit('added', dl)
async def updated(self, dl):
await self.emit('updated', dl)
async def completed(self, dl):
await self.emit('completed', dl)
async def canceled(self, id):
await self.emit('canceled', id)
async def cleared(self, id):
await self.emit('cleared', id)
async def emit(self, event: str, data):
await self.sio.emit(event, self.serializer.encode(data))

View file

@ -6,10 +6,8 @@ import json
import os
import random
from Config import Config
from version import APP_VERSION
from Notifier import Notifier
from DownloadQueue import DownloadQueue
from Utils import ObjectSerializer
from Utils import ObjectSerializer, Notifier
from aiohttp import web
from aiohttp.web import Request, Response
from player.M3u8 import M3u8
@ -20,9 +18,6 @@ import caribou
import sqlite3
from aiocron import crontab
log: logging.Logger = None
class Main:
config: Config = None
serializer: ObjectSerializer = None
@ -36,7 +31,6 @@ class Main:
def __init__(self):
self.config = Config()
self.config.version = APP_VERSION
self.logger = logging.getLogger('main')
try:
@ -113,6 +107,7 @@ class Main:
)
async def connect(self, sid, _):
self.logger.info(f'Config [{self.config.__dict__}].')
data: dict = {
**self.dqueue.get(),
"config": self.config,