From 401d8de03feabc9a3964dfcb2fc56f2edc5ed6aa Mon Sep 17 00:00:00 2001 From: mrsobakin <68982655+mrsobakin@users.noreply.github.com> Date: Wed, 13 Sep 2023 09:30:27 +0300 Subject: [PATCH] Added config file support --- lyrics_dl/__init__.py | 31 ++++++++++++++++++++++++ lyrics_dl/__main__.py | 55 +++++++++++-------------------------------- 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/lyrics_dl/__init__.py b/lyrics_dl/__init__.py index 2c6eaae..09eb751 100644 --- a/lyrics_dl/__init__.py +++ b/lyrics_dl/__init__.py @@ -1,4 +1,5 @@ from typing import Optional +from pathlib import Path import traceback # Initialize classes from lyrics_dl/providers @@ -53,3 +54,33 @@ class LyricsDl: self.logger.info(f"[{provider.name}] No lyrics was found!") return None + + def process_file(self, path: Path, force: bool = False) -> None: + lyrics_path = path.with_suffix(".lrc") + + if lyrics_path.exists() and not force: + self.logger.error("[lyrics-dl] Lyrics file already exists!") + return + + # TODO handle errors + try: + song = Song.from_file(path) + except Exception as e: + self.logger.error(f"[lyrics-dl] {path}: {e}") + return + + lyrics = self.fetch_lyrics(song) + + if not lyrics: + self.logger.error("[lyrics-dl] No lyrics was found!") + return + + with open(lyrics_path, "w") as f: + f.write(lyrics) + + def process_directory(self, path: Path, extensions: list[str], force: bool = False) -> None: + for file_path in path.rglob("*"): + if file_path.suffix[1:] not in extensions: + continue + + self.process_file(file_path, force) diff --git a/lyrics_dl/__main__.py b/lyrics_dl/__main__.py index 9466adb..65df76e 100644 --- a/lyrics_dl/__main__.py +++ b/lyrics_dl/__main__.py @@ -1,62 +1,35 @@ import argparse from pathlib import Path -from lyrics_dl.core import Song from lyrics_dl.config import LyricsDlConfig from lyrics_dl.logger import DefaultLogger from lyrics_dl import LyricsDl -logger = DefaultLogger() -config = LyricsDlConfig() -lyrics_dl = LyricsDl(config=config, logger=logger) - - -def process_file(path, force=False): - lyrics_path = path.with_suffix(".lrc") - - if lyrics_path.exists() and not force: - logger.error("[lyrics-dl] Lyrics file already exists!") - return - - # TODO handle errors - try: - song = Song.from_file(path) - except Exception as e: - logger.error(f"[lyrics-dl] {path}: {e}") - return - - lyrics = lyrics_dl.fetch_lyrics(song) - - if not lyrics: - logger.error("[lyrics-dl] No lyrics was found!") - return - - with open(lyrics_path, "w") as f: - f.write(lyrics) - - -def process_directory(path, extensions): - for file_path in path.rglob("*"): - if file_path.suffix[1:] not in extensions: - continue - - process_file(file_path) - - parser = argparse.ArgumentParser() parser.add_argument("path", type=Path, help="Path to the song file or directory") +parser.add_argument("-c", "--config", type=Path, help="Config file for lyrics-dl") parser.add_argument("-e", "--extensions", type=str, help="Music files extensions, separated by a comma. For example: wav,flac,mp3") parser.add_argument("-f", "--force-override", action="store_true", help="Force override .lrc file, if it already exists") - args = parser.parse_args() + +logger = DefaultLogger() + +if args.config: + config = LyricsDlConfig.from_file(args.config) +else: + config = LyricsDlConfig() + +lyrics_dl = LyricsDl(config=config, logger=logger) + + if args.path.is_dir(): if not args.extensions: extensions = ["flac", "alac", "mp3", "m4a", "mp4", "aac", "wav", "opus", "ogg"] else: extensions = args.extensions.split(",") - process_directory(args.path, extensions) + lyrics_dl.process_directory(args.path, extensions, args.force_override) else: - process_file(args.path) + lyrics_dl.process_file(args.path, args.force_override)