Merge branch 'rewrite' of https://github.com/vitiko98/qobuz-dl into rewrite
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
import mutagen.id3 as id3
|
||||
import appdirs
|
||||
|
||||
APPNAME = "qobuz-dl"
|
||||
|
||||
CACHE_DIR = appdirs.user_cache_dir(APPNAME)
|
||||
CONFIG_DIR = appdirs.user_config_dir(APPNAME)
|
||||
LOG_DIR = appdirs.user_config_dir(APPNAME)
|
||||
|
||||
EXT = {
|
||||
5: ".mp3",
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
from typing import Optional
|
||||
import logging
|
||||
import os
|
||||
|
||||
import logging.handlers as handlers
|
||||
|
||||
from typing import Optional, Union
|
||||
|
||||
import requests
|
||||
from tqdm import tqdm
|
||||
|
||||
from .constants import LOG_DIR
|
||||
|
||||
|
||||
def safe_get(d: dict, *keys, default=None):
|
||||
"""A replacement for chained `get()` statements on dicts:
|
||||
@@ -60,3 +67,38 @@ def tqdm_download(url: str, filepath: str):
|
||||
for data in r.iter_content(chunk_size=1024):
|
||||
size = file.write(data)
|
||||
bar.update(size)
|
||||
|
||||
|
||||
def init_log(path: Optional[str] = None, level: str = "DEBUG", rotate: str = "midnight"):
|
||||
"""
|
||||
Initialize a log instance with a stream handler and a rotating file handler.
|
||||
If a path is not set, fallback to the default app log directory.
|
||||
|
||||
:param path:
|
||||
:type path: Optional[str]
|
||||
:param level:
|
||||
:type level: str
|
||||
:param rotate:
|
||||
:type rotate: str
|
||||
"""
|
||||
if not path:
|
||||
os.makedirs(LOG_DIR, exist_ok=True)
|
||||
path = os.path.join(LOG_DIR, "qobuz_dl.log")
|
||||
|
||||
logger = logging.getLogger()
|
||||
level = logging.getLevelName(level)
|
||||
logger.setLevel(level)
|
||||
|
||||
formatter = logging.Formatter(
|
||||
fmt="%(asctime)s - %(module)s.%(funcName)s.%(levelname)s: %(message)s",
|
||||
datefmt="%H:%M:%S",
|
||||
)
|
||||
|
||||
rotable = handlers.TimedRotatingFileHandler(path, when=rotate)
|
||||
printable = logging.StreamHandler()
|
||||
|
||||
rotable.setFormatter(formatter)
|
||||
printable.setFormatter(formatter)
|
||||
|
||||
logger.addHandler(printable)
|
||||
logger.addHandler(rotable)
|
||||
|
||||
Reference in New Issue
Block a user