diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5123b..e3d4e34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,18 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Add support for more sites +## [2.2.16] - 2022-12-30 + +### Fixed + +- Log level is now fixed and should not default to 0 +- Docker schedule should now work again + +### Changed + +- Integrate logging logs to loguru via custom sink +- Simplify docker shell scripts + ## [2.2.15] - 2022-12-29 ### Added diff --git a/docker/rootfs/app/schedules/daily.sh b/docker/rootfs/app/schedules/daily.sh index d107bde..d1d9d1f 100755 --- a/docker/rootfs/app/schedules/daily.sh +++ b/docker/rootfs/app/schedules/daily.sh @@ -4,5 +4,4 @@ python3 /app/manga-dlp.py \ --path /app/downloads \ --read /app/mangas.txt \ --chapters all \ - --wait 2 \ - --warn + --wait 2 diff --git a/docker/rootfs/etc/cont-init.d/20-setenv.sh b/docker/rootfs/etc/cont-init.d/20-setenv.sh index e7c1300..4ac29ee 100644 --- a/docker/rootfs/etc/cont-init.d/20-setenv.sh +++ b/docker/rootfs/etc/cont-init.d/20-setenv.sh @@ -12,4 +12,4 @@ : "${MDLP_FILE_FORMAT:=cbz}" : "${MDLP_WAIT:=0.5}" : "${MDLP_FORCEVOL:=false}" -: "${MDLP_LOG_LEVEL:=warn}" +: "${MDLP_LOG_LEVEL:=}" diff --git a/docker/rootfs/etc/cont-init.d/51-fix-schedule.sh b/docker/rootfs/etc/cont-init.d/51-fix-schedule.sh deleted file mode 100644 index 4a8aad4..0000000 --- a/docker/rootfs/etc/cont-init.d/51-fix-schedule.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/with-contenv bash -# shellcheck shell=bash - -# source env variables -source /etc/cont-init.d/20-setenv.sh - -# check schedule -[[ -f "/app/schedules/daily.sh" ]] && DAILY_SH_FOUND=true -[[ -f "/app/schedules/daily" ]] && DAILY_FOUND=true -# check crontab -if grep -q -e "/app/schedules/daily.sh\s" /etc/cron.d/mangadlp; then - CRON_SH_FOUND=true -elif grep -q -e "/app/schedules/daily\s" /etc/cron.d/mangadlp; then - CRON_FOUND=true -fi - -# fix new .sh schedule if it's not synced with the crontab -if [[ "${CRON_SH_FOUND}" == "true" ]] && [[ "${DAILY_SH_FOUND}" != "true" ]]; then - echo "Fixing new .sh schedule" - echo "Adding symlink to daily.sh" - if ! ln -s /app/schedule/daily /app/schedule/daily.sh; then - echo "Can't fix schedule. Maybe the file is missing." - fi -elif [[ "${CRON_FOUND}" == "true" ]] && [[ "${DAILY_FOUND}" != "true" ]]; then - echo "Fixing new .sh schedule" - echo "Adding symlink to daily" - if ! ln -s /app/schedule/daily.sh /app/schedule/daily; then - echo "Can't fix schedule. Maybe the file is missing." - fi -fi diff --git a/docker/rootfs/etc/cont-init.d/52-set-schedule.sh b/docker/rootfs/etc/cont-init.d/52-set-schedule.sh index 5a9ce3c..e832020 100644 --- a/docker/rootfs/etc/cont-init.d/52-set-schedule.sh +++ b/docker/rootfs/etc/cont-init.d/52-set-schedule.sh @@ -4,52 +4,45 @@ # source env variables source /etc/cont-init.d/20-setenv.sh +custom_args=( + --path "${MDLP_PATH}" + --read "${MDLP_READ}" + --language "${MDLP_LANGUAGE}" + --chapters "${MDLP_CHAPTERS}" + --format "${MDLP_FILE_FORMAT}" + --wait "${MDLP_WAIT}" +) + function prepare_vars() { # set log level case "${MDLP_LOG_LEVEL}" in "warn") - MDLP_LOG_LEVEL_FLAG=" --warn" + custom_args+=("--warn") ;; "debug") - MDLP_LOG_LEVEL_FLAG=" --debug" + custom_args+=("--debug") ;; *) - MDLP_LOG_LEVEL_FLAG=" --loglevel ${MDLP_LOG_LEVEL}" + if [[ -n "${MDLP_LOG_LEVEL}" ]]; then + custom_args+=("--loglevel" "${MDLP_LOG_LEVEL}") + fi ;; esac # check if forcevol should be used if [[ "${MDLP_FORCEVOL,,}" == "true" ]]; then - # add backslash if log level is also specified - if [[ -n "${MDLP_LOG_LEVEL_FLAG}" ]]; then - MDLP_FORCEVOL_FLAG="\n --forcevol \\" - else - MDLP_FORCEVOL_FLAG="\n --forcevol" - fi + custom_args+=("--forcevol") fi } # set schedule with env variables function set_vars() { - echo -ne "#!/bin/bash\n -python3 /app/manga-dlp.py \\ - --path ${MDLP_PATH} \\ - --read ${MDLP_READ} \\ - --language ${MDLP_LANGUAGE} \\ - --chapters ${MDLP_CHAPTERS} \\ - --format ${MDLP_FILE_FORMAT} \\ - --wait ${MDLP_WAIT}" \ - > /app/schedules/daily.sh + cat << EOF > "/app/schedules/daily.sh" +#!/bin/bash - # set forcevol or log level if specified - if [[ -n "${MDLP_FORCEVOL_FLAG}" ]] || [[ -n "${MDLP_LOG_LEVEL_FLAG}" ]]; then - sed -i 's/--wait '"${MDLP_WAIT}"'/--wait '"${MDLP_WAIT}"' \\/g' /app/schedules/daily.sh - echo -e "${MDLP_FORCEVOL_FLAG:-}" >> /app/schedules/daily.sh - echo -e "${MDLP_LOG_LEVEL_FLAG:-}" >> /app/schedules/daily.sh - else - # add final newline of not added before - echo -ne "\n" >> /app/schedules/daily.sh - fi +python3 /app/manga-dlp.py ${custom_args[@]} + +EOF } # check if schedule should be generated diff --git a/docs/pages/docker.md b/docs/pages/docker.md index bf1b24c..3fe82f9 100644 --- a/docs/pages/docker.md +++ b/docs/pages/docker.md @@ -39,17 +39,17 @@ it will not generate it (it will just use the default one). For more info's about the options, you can look in the main scripts [README.md](../) -| ENV Variable | Default | manga-dlp option | Info | -|:-----------------------|:----------------|:------------------------------|--------------------------------------------------------------------------| -| MDLP_GENERATE_SCHEDULE | false | none | Has to be set to "true" to generate the config via environment variables | -| MDLP_PATH | /app/downloads | --path | | -| MDLP_READ | /app/mangas.txt | --read | | -| MDLP_LANGUAGE | en | --language | | -| MDLP_CHAPTERS | all | --chapter | | -| MDLP_FILE_FORMAT | cbz | --format | | -| MDLP_WAIT | 0.5 | --wait | | -| MDLP_FORCEVOL | false | --forcevol | | -| MDLP_LOG_LEVEL | lean | --warn / --debug / --loglevel | Can either be set to: warn, debug or a custom loglevel integer | +| ENV Variable | Default | manga-dlp option | Info | +|:-----------------------|:----------------|:------------------------------------|--------------------------------------------------------------------------| +| MDLP_GENERATE_SCHEDULE | false | none | Has to be set to "true" to generate the config via environment variables | +| MDLP_PATH | /app/downloads | --path | | +| MDLP_READ | /app/mangas.txt | --read | | +| MDLP_LANGUAGE | en | --language | | +| MDLP_CHAPTERS | all | --chapter | | +| MDLP_FILE_FORMAT | cbz | --format | | +| MDLP_WAIT | 0.5 | --wait | | +| MDLP_FORCEVOL | false | --forcevol | | +| MDLP_LOG_LEVEL | | --warn / --debug / --loglevel | Can either be set to: warn, debug or a custom loglevel integer | ## Run commands in container diff --git a/mangadlp/__about__.py b/mangadlp/__about__.py index 1fde5cd..a2586c2 100644 --- a/mangadlp/__about__.py +++ b/mangadlp/__about__.py @@ -1 +1 @@ -__version__ = "2.1.15" +__version__ = "2.1.16" diff --git a/mangadlp/cli.py b/mangadlp/cli.py index 8bf2669..b6b5b23 100644 --- a/mangadlp/cli.py +++ b/mangadlp/cli.py @@ -28,7 +28,7 @@ def readin_list(_, __, value) -> list: # filter empty lines and remove them filtered_list = list(filter(len, url_list)) - log.info(f"Mangas from list: {filtered_list}") + click.echo(f"Mangas from list: {filtered_list}") return filtered_list @@ -64,24 +64,24 @@ def readin_list(_, __, value) -> list: "--loglevel", "verbosity", type=int, - default=20, + default=None, show_default=True, help="Custom log level", ) @optgroup.option( "--warn", "verbosity", - flag_value=25, - default=20, - show_default=False, + flag_value=30, + default=None, + show_default=True, help="Only log warnings and higher", ) @optgroup.option( "--debug", "verbosity", flag_value=10, - default=20, - show_default=False, + default=None, + show_default=True, help="Debug logging. Log EVERYTHING", ) # other options @@ -212,6 +212,10 @@ def main( """ + # set log level to INFO if not set + if not verbosity: + verbosity = 20 + # set loglevel and log format prepare_logger(verbosity) diff --git a/mangadlp/logger.py b/mangadlp/logger.py index 590248e..f14e2da 100644 --- a/mangadlp/logger.py +++ b/mangadlp/logger.py @@ -3,33 +3,45 @@ import sys from loguru import logger -LOGGING_FMT: str = ( - "%(asctime)s | (D) [%(levelname)-7s] [%(name)-10s] [%(funcName)-20s]: %(message)s" -) -LOGURU_FMT: str = "{time:%Y-%m-%dT%H:%M:%S%z} | (C) [{level: <7}] [{name: <10}] [{function: <20}]: {message}" +LOGURU_FMT = "{time:%Y-%m-%dT%H:%M:%S%z} | [{level: <7}] [{name: <10}] [{function: <20}]: {message}" -def enable_default_logger(loglevel: int) -> None: - logging.root.handlers = [] +# from loguru docs +class InterceptHandler(logging.Handler): + """ + Intercept python logging messages and log them via loguru.logger + """ - logging.basicConfig( - format=LOGGING_FMT, - datefmt="%Y-%m-%dT%H:%M:%S%z", - level=loglevel, - handlers=[logging.StreamHandler()], - ) + def emit(self, record): + # Get corresponding Loguru level if it exists + try: + level = logger.level(record.levelname).name + except ValueError: + level = record.levelno + + # Find caller from where originated the logged message + frame, depth = logging.currentframe(), 2 + while frame.f_code.co_filename == logging.__file__: + frame = frame.f_back + depth += 1 + + logger.opt(depth=depth, exception=record.exc_info).log( + level, record.getMessage() + ) -# create config for a normal stderr logger -def prepare_logger(loglevel: int) -> None: +# init logger with format and log level +def prepare_logger(loglevel: int = 20) -> None: + config: dict = { "handlers": [ { "sink": sys.stdout, "level": loglevel, "format": LOGURU_FMT, - }, + } ], } + + logging.basicConfig(handlers=[InterceptHandler()], level=loglevel) logger.configure(**config) - enable_default_logger(loglevel) diff --git a/mangadlp/utils.py b/mangadlp/utils.py index fd3f8ab..0800fef 100644 --- a/mangadlp/utils.py +++ b/mangadlp/utils.py @@ -121,7 +121,7 @@ def get_filename( def progress_bar(progress: float, total: float) -> None: - time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") percent = int(progress / (int(total) / 100)) bar_length = 50 bar_progress = int(progress / (int(total) / bar_length)) @@ -129,6 +129,9 @@ def progress_bar(progress: float, total: float) -> None: whitespace_texture = " " * (bar_length - bar_progress) if progress == total: full_bar = "■" * bar_length - print(f"\r{time} | ❙{full_bar}❙ 100%", end="\n") + print(f"\r{time}{' '*6}| [BAR ] ❙{full_bar}❙ 100%", end="\n") else: - print(f"\r{time} | ❙{bar_texture}{whitespace_texture}❙ {percent}%", end="\r") + print( + f"\r{time}{' '*6}| [BAR ] ❙{bar_texture}{whitespace_texture}❙ {percent}%", + end="\r", + )