From ae9d49426a09310c89dac6da8462d575a2590588 Mon Sep 17 00:00:00 2001 From: mrsobakin <68982655+mrsobakin@users.noreply.github.com> Date: Tue, 13 Aug 2024 22:27:58 +0500 Subject: [PATCH] Add `delay` parameter to avoid hitting rate limits --- README.md | 1 + config.toml | 6 ++++++ lyrics_dl/__init__.py | 20 +++++++++++++++----- lyrics_dl/config.py | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 config.toml diff --git a/README.md b/README.md index 6913a92..5361154 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ As an example, to enable the `musixmatch` provider, you'll need to [acquire a Mu ```toml [providers] order = ["musixmatch", "kugou"] +delay = 10 # Seconds to wait between searches [providers.musixmatch] token = "YOUR_TOKEN" diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..31de269 --- /dev/null +++ b/config.toml @@ -0,0 +1,6 @@ +[providers] +order = ["musixmatch", "kugou"] +delay = 10 + +[providers.musixmatch] +token = "123456" diff --git a/lyrics_dl/__init__.py b/lyrics_dl/__init__.py index 09eb751..44a7042 100644 --- a/lyrics_dl/__init__.py +++ b/lyrics_dl/__init__.py @@ -1,3 +1,4 @@ +import time from typing import Optional from pathlib import Path import traceback @@ -14,6 +15,7 @@ class LyricsDl: logger: AbstractLogger def __init__(self, config: LyricsDlConfig = LyricsDlConfig(), logger: AbstractLogger = DefaultLogger()): + self.config = config self.logger = logger providers_classes = Registry.get_synced_providers() @@ -55,32 +57,40 @@ class LyricsDl: return None - def process_file(self, path: Path, force: bool = False) -> None: + def process_file(self, path: Path, force: bool = False) -> bool: lyrics_path = path.with_suffix(".lrc") if lyrics_path.exists() and not force: self.logger.error("[lyrics-dl] Lyrics file already exists!") - return + return False # TODO handle errors try: song = Song.from_file(path) except Exception as e: self.logger.error(f"[lyrics-dl] {path}: {e}") - return + return False lyrics = self.fetch_lyrics(song) if not lyrics: self.logger.error("[lyrics-dl] No lyrics was found!") - return + return True with open(lyrics_path, "w") as f: f.write(lyrics) + return True + def process_directory(self, path: Path, extensions: list[str], force: bool = False) -> None: + delay_next = False + for file_path in path.rglob("*"): + if delay_next and self.config.delay is not None: + self.logger.info(f"[lyrics-dl] Sleeping for {self.config.delay:.2f}s...") + time.sleep(self.config.delay) + if file_path.suffix[1:] not in extensions: continue - self.process_file(file_path, force) + delay_next = self.process_file(file_path, force) diff --git a/lyrics_dl/config.py b/lyrics_dl/config.py index fbe4323..f04fbf9 100644 --- a/lyrics_dl/config.py +++ b/lyrics_dl/config.py @@ -22,6 +22,7 @@ CONFIG_PATH = _get_config_file() @dataclass class LyricsDlConfig: order: list[str] = field(default_factory=lambda: ["kugou", "youtube"]) + delay: float | None = 10 providers_configs: dict[str, dict] = field(default_factory=lambda: {}) @classmethod