Add delay
parameter to avoid hitting rate limits
This commit is contained in:
parent
9c01b5733b
commit
ae9d49426a
4 changed files with 23 additions and 5 deletions
|
@ -117,6 +117,7 @@ As an example, to enable the `musixmatch` provider, you'll need to [acquire a Mu
|
||||||
```toml
|
```toml
|
||||||
[providers]
|
[providers]
|
||||||
order = ["musixmatch", "kugou"]
|
order = ["musixmatch", "kugou"]
|
||||||
|
delay = 10 # Seconds to wait between searches
|
||||||
|
|
||||||
[providers.musixmatch]
|
[providers.musixmatch]
|
||||||
token = "YOUR_TOKEN"
|
token = "YOUR_TOKEN"
|
||||||
|
|
6
config.toml
Normal file
6
config.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[providers]
|
||||||
|
order = ["musixmatch", "kugou"]
|
||||||
|
delay = 10
|
||||||
|
|
||||||
|
[providers.musixmatch]
|
||||||
|
token = "123456"
|
|
@ -1,3 +1,4 @@
|
||||||
|
import time
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -14,6 +15,7 @@ class LyricsDl:
|
||||||
logger: AbstractLogger
|
logger: AbstractLogger
|
||||||
|
|
||||||
def __init__(self, config: LyricsDlConfig = LyricsDlConfig(), logger: AbstractLogger = DefaultLogger()):
|
def __init__(self, config: LyricsDlConfig = LyricsDlConfig(), logger: AbstractLogger = DefaultLogger()):
|
||||||
|
self.config = config
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
|
|
||||||
providers_classes = Registry.get_synced_providers()
|
providers_classes = Registry.get_synced_providers()
|
||||||
|
@ -55,32 +57,40 @@ class LyricsDl:
|
||||||
|
|
||||||
return None
|
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")
|
lyrics_path = path.with_suffix(".lrc")
|
||||||
|
|
||||||
if lyrics_path.exists() and not force:
|
if lyrics_path.exists() and not force:
|
||||||
self.logger.error("[lyrics-dl] Lyrics file already exists!")
|
self.logger.error("[lyrics-dl] Lyrics file already exists!")
|
||||||
return
|
return False
|
||||||
|
|
||||||
# TODO handle errors
|
# TODO handle errors
|
||||||
try:
|
try:
|
||||||
song = Song.from_file(path)
|
song = Song.from_file(path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"[lyrics-dl] {path}: {e}")
|
self.logger.error(f"[lyrics-dl] {path}: {e}")
|
||||||
return
|
return False
|
||||||
|
|
||||||
lyrics = self.fetch_lyrics(song)
|
lyrics = self.fetch_lyrics(song)
|
||||||
|
|
||||||
if not lyrics:
|
if not lyrics:
|
||||||
self.logger.error("[lyrics-dl] No lyrics was found!")
|
self.logger.error("[lyrics-dl] No lyrics was found!")
|
||||||
return
|
return True
|
||||||
|
|
||||||
with open(lyrics_path, "w") as f:
|
with open(lyrics_path, "w") as f:
|
||||||
f.write(lyrics)
|
f.write(lyrics)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def process_directory(self, path: Path, extensions: list[str], force: bool = False) -> None:
|
def process_directory(self, path: Path, extensions: list[str], force: bool = False) -> None:
|
||||||
|
delay_next = False
|
||||||
|
|
||||||
for file_path in path.rglob("*"):
|
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:
|
if file_path.suffix[1:] not in extensions:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.process_file(file_path, force)
|
delay_next = self.process_file(file_path, force)
|
||||||
|
|
|
@ -22,6 +22,7 @@ CONFIG_PATH = _get_config_file()
|
||||||
@dataclass
|
@dataclass
|
||||||
class LyricsDlConfig:
|
class LyricsDlConfig:
|
||||||
order: list[str] = field(default_factory=lambda: ["kugou", "youtube"])
|
order: list[str] = field(default_factory=lambda: ["kugou", "youtube"])
|
||||||
|
delay: float | None = 10
|
||||||
providers_configs: dict[str, dict] = field(default_factory=lambda: {})
|
providers_configs: dict[str, dict] = field(default_factory=lambda: {})
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue