Add delay parameter to avoid hitting rate limits

This commit is contained in:
mrsobakin 2024-08-13 22:27:58 +05:00
parent 9c01b5733b
commit ae9d49426a
No known key found for this signature in database
GPG key ID: 325CBF665E4FFD6E
4 changed files with 23 additions and 5 deletions

View file

@ -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"

6
config.toml Normal file
View file

@ -0,0 +1,6 @@
[providers]
order = ["musixmatch", "kugou"]
delay = 10
[providers.musixmatch]
token = "123456"

View file

@ -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)

View file

@ -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