Rename module from lyrics-dl
to lrc-dl
This commit is contained in:
parent
4bfcbf17bd
commit
658e1bef77
16 changed files with 52 additions and 52 deletions
100
lrc_dl/__init__.py
Normal file
100
lrc_dl/__init__.py
Normal file
|
@ -0,0 +1,100 @@
|
|||
import time
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
import traceback
|
||||
|
||||
# Initialize classes from lrc_dl/providers
|
||||
import lrc_dl.providers
|
||||
from lrc_dl.core import Song
|
||||
from lrc_dl.registry import Registry
|
||||
from lrc_dl.config import LyricsDlConfig
|
||||
from lrc_dl.logger import DefaultLogger, AbstractLogger
|
||||
|
||||
|
||||
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()
|
||||
|
||||
self.providers = []
|
||||
|
||||
for name in config.order:
|
||||
Provider = providers_classes[name]
|
||||
provider_config = config.providers_configs.get(name)
|
||||
|
||||
if not provider_config:
|
||||
provider_config = {}
|
||||
|
||||
try:
|
||||
provider = Provider(**provider_config)
|
||||
except TypeError as e:
|
||||
self.logger.error(f"[lrc-dl] {e}")
|
||||
continue
|
||||
|
||||
self.providers.append(provider)
|
||||
|
||||
def fetch_lyrics(self, song: Song) -> Optional[str]:
|
||||
self.logger.info(f"[lrc-dl] Fetching lyrics for \"{song.artist} - {song.title}\"")
|
||||
for provider in self.providers:
|
||||
self.logger.info(f"[{provider.name}] Fetching lyrics...")
|
||||
|
||||
try:
|
||||
lyrics = provider.fetch_lyrics(song)
|
||||
except Exception as e:
|
||||
lyrics = None
|
||||
self.logger.error(f"[{provider.name}] Got exception while fetching lyrics! ({type(e).__name__}: {e})")
|
||||
self.logger.debug(f"[{provider.name}] {traceback.format_exc()}")
|
||||
|
||||
if lyrics:
|
||||
self.logger.info(f"[{provider.name}] Found lyrics!")
|
||||
|
||||
if self.config.prepend_header:
|
||||
lyrics = f"[re:lrc-dl:{provider.name}]\n\n{lyrics}"
|
||||
|
||||
return lyrics
|
||||
|
||||
self.logger.info(f"[{provider.name}] No lyrics was found!")
|
||||
|
||||
return 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("[lrc-dl] Lyrics file already exists!")
|
||||
return False
|
||||
|
||||
# TODO handle errors
|
||||
try:
|
||||
song = Song.from_file(path)
|
||||
except Exception as e:
|
||||
self.logger.error(f"[lrc-dl] {path}: {e}")
|
||||
return False
|
||||
|
||||
lyrics = self.fetch_lyrics(song)
|
||||
|
||||
if not lyrics:
|
||||
self.logger.error("[lrc-dl] No lyrics was found!")
|
||||
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"[lrc-dl] Sleeping for {self.config.delay:.2f}s...")
|
||||
time.sleep(self.config.delay)
|
||||
|
||||
if file_path.suffix[1:] not in extensions:
|
||||
continue
|
||||
|
||||
delay_next = self.process_file(file_path, force)
|
3
lrc_dl/__main__.py
Normal file
3
lrc_dl/__main__.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from lrc_dl.main import main
|
||||
|
||||
main()
|
50
lrc_dl/config.py
Normal file
50
lrc_dl/config.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Self
|
||||
import os
|
||||
import tomllib
|
||||
|
||||
from lrc_dl.logger import DefaultLogger
|
||||
|
||||
|
||||
def _get_config_file() -> Path | None:
|
||||
config_dir = os.environ.get("XDG_CONFIG_HOME")
|
||||
|
||||
if config_dir is None:
|
||||
return None
|
||||
|
||||
return Path(config_dir) / "lrc-dl" / "config.toml"
|
||||
|
||||
|
||||
CONFIG_PATH = _get_config_file()
|
||||
|
||||
|
||||
@dataclass
|
||||
class LyricsDlConfig:
|
||||
order: list[str] = field(default_factory=lambda: ["kugou", "youtube"])
|
||||
delay: float | None = 10
|
||||
prepend_header: bool = True
|
||||
providers_configs: dict[str, dict] = field(default_factory=lambda: {})
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, path: Path) -> Self:
|
||||
with open(path, "rb") as f:
|
||||
config = tomllib.load(f)
|
||||
|
||||
return cls(
|
||||
order=config["providers"].pop("order"),
|
||||
providers_configs=config["providers"],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def default(cls) -> Self:
|
||||
try:
|
||||
if CONFIG_PATH is not None:
|
||||
return cls.from_file(CONFIG_PATH)
|
||||
except FileNotFoundError:
|
||||
DefaultLogger().warning(
|
||||
f"Warning: Missing config file ({CONFIG_PATH})."
|
||||
" Falling back to default parameters."
|
||||
)
|
||||
|
||||
return cls()
|
38
lrc_dl/core.py
Normal file
38
lrc_dl/core.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from dataclasses import dataclass
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional, Self
|
||||
from pathlib import Path
|
||||
import mutagen
|
||||
|
||||
|
||||
@dataclass
|
||||
class Song:
|
||||
title: str
|
||||
artist: str
|
||||
album: Optional[str] = None
|
||||
duration: Optional[int] = None
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, path: Path) -> Self:
|
||||
metadata = mutagen.File(path)
|
||||
|
||||
if "title" not in metadata or "artist" not in metadata:
|
||||
raise RuntimeError("Song is missing title or artist name")
|
||||
|
||||
title = ", ".join(metadata.get("title"))
|
||||
artist = ", ".join(metadata.get("artist"))
|
||||
|
||||
album = metadata.get("album")
|
||||
if album:
|
||||
album = ", ".join(album)
|
||||
|
||||
duration = metadata.info.length
|
||||
|
||||
return cls(title=title, artist=artist, album=album, duration=duration)
|
||||
|
||||
|
||||
class AbstractProvider(ABC):
|
||||
name: str = ""
|
||||
@abstractmethod
|
||||
def fetch_lyrics(self, song: Song) -> Optional[str]:
|
||||
pass
|
55
lrc_dl/logger.py
Normal file
55
lrc_dl/logger.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
from typing import Self
|
||||
from abc import ABC, abstractmethod
|
||||
import logging
|
||||
|
||||
|
||||
class AbstractLogger(ABC):
|
||||
@abstractmethod
|
||||
def debug(self, message: str) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def info(self, message: str) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def warning(self, message: str) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def error(self, message: str) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class DefaultLogger(AbstractLogger):
|
||||
__instance = None
|
||||
__initialized = False
|
||||
|
||||
def __new__(cls) -> Self:
|
||||
if cls.__instance is None:
|
||||
cls.__instance = AbstractLogger.__new__(cls)
|
||||
return cls.__instance
|
||||
|
||||
def __init__(self) -> None:
|
||||
if self.__initialized:
|
||||
return
|
||||
self.__initialized = True
|
||||
|
||||
self.logger = logging.getLogger("lrc-dl")
|
||||
self.logger.setLevel(logging.DEBUG)
|
||||
|
||||
console_handler = logging.StreamHandler()
|
||||
console_handler.setLevel(logging.DEBUG)
|
||||
self.logger.addHandler(console_handler)
|
||||
|
||||
def debug(self, message: str) -> None:
|
||||
self.logger.debug(message)
|
||||
|
||||
def info(self, message: str) -> None:
|
||||
self.logger.info(message)
|
||||
|
||||
def warning(self, message: str) -> None:
|
||||
self.logger.warning(message)
|
||||
|
||||
def error(self, message: str) -> None:
|
||||
self.logger.error(message)
|
40
lrc_dl/main.py
Normal file
40
lrc_dl/main.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from lrc_dl.config import LyricsDlConfig
|
||||
from lrc_dl.logger import DefaultLogger
|
||||
from lrc_dl import LyricsDl
|
||||
|
||||
|
||||
DEFAULT_EXTENSIONS = ["flac", "alac", "mp3", "m4a", "mp4", "aac", "wav", "opus", "ogg"]
|
||||
|
||||
|
||||
def parse_args():
|
||||
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 lrc-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")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
logger = DefaultLogger()
|
||||
|
||||
if args.config:
|
||||
config = LyricsDlConfig.from_file(args.config)
|
||||
else:
|
||||
config = LyricsDlConfig.default()
|
||||
|
||||
lrc_dl = LyricsDl(config=config, logger=logger)
|
||||
|
||||
if args.path.is_dir():
|
||||
if not args.extensions:
|
||||
extensions = DEFAULT_EXTENSIONS
|
||||
else:
|
||||
extensions = args.extensions.split(",")
|
||||
|
||||
lrc_dl.process_directory(args.path, extensions, args.force_override)
|
||||
else:
|
||||
lrc_dl.process_file(args.path, args.force_override)
|
3
lrc_dl/providers/__init__.py
Normal file
3
lrc_dl/providers/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from lrc_dl.providers import musixmatch
|
||||
from lrc_dl.providers import kugou
|
||||
from lrc_dl.providers import youtube
|
79
lrc_dl/providers/kugou.py
Normal file
79
lrc_dl/providers/kugou.py
Normal file
|
@ -0,0 +1,79 @@
|
|||
from typing import Optional, Iterable
|
||||
from base64 import b64decode
|
||||
import zlib
|
||||
import re
|
||||
from itertools import filterfalse, islice
|
||||
from datetime import datetime
|
||||
|
||||
import httpx
|
||||
|
||||
from lrc_dl.core import Song, AbstractProvider
|
||||
from lrc_dl.registry import lyrics_provider
|
||||
|
||||
|
||||
KRC_ENCODE_KEY = [64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105]
|
||||
|
||||
RE_KRC_JUNK = re.compile(r"^\[((id|ar|ti|by|hash|al|sign|qq|total|language):|offset:0\]|.*\](<.*>)?(Written by:|Lyrics by:|Composed by:|Producer:|作曲 :|作词 :)).*$")
|
||||
RE_WORD_TIMING = re.compile(r"<-?\d+,-?\d+,-?\d+>")
|
||||
|
||||
|
||||
def decode_krc(content: bytes) -> str:
|
||||
content = b64decode(content)
|
||||
|
||||
buf = bytearray(len(content) - 4)
|
||||
for i in range(4, len(content)):
|
||||
buf[i - 4] = content[i] ^ KRC_ENCODE_KEY[(i - 4) % 16]
|
||||
|
||||
return zlib.decompress(buf).decode('utf-8-sig')
|
||||
|
||||
|
||||
def reformat_timings(lines: Iterable[str]) -> Iterable[str]:
|
||||
for line in lines:
|
||||
if not line.startswith("["):
|
||||
yield line
|
||||
continue
|
||||
|
||||
raw_timings, text = line.split("]", 1)
|
||||
beginning, _ = map(int, raw_timings[1:].split(","))
|
||||
|
||||
timing = datetime.fromtimestamp(beginning / 1000).strftime("%M:%S.%f")[:8]
|
||||
|
||||
yield f"[{timing}]{text}"
|
||||
|
||||
|
||||
@lyrics_provider
|
||||
class Kugou(AbstractProvider):
|
||||
name = "kugou"
|
||||
|
||||
def fetch_lyrics(self: AbstractProvider, song: Song) -> Optional[str]:
|
||||
keyword = f"{song.artist} - {song.title}"
|
||||
|
||||
response = httpx.get("https://krcs.kugou.com/search", params={
|
||||
"ver": 1,
|
||||
"man": "yes",
|
||||
"client": "mobi",
|
||||
"keyword": keyword
|
||||
}).json()
|
||||
|
||||
candidates = response["candidates"]
|
||||
|
||||
if not candidates:
|
||||
return None
|
||||
|
||||
id_, accesskey = candidates[0]["id"], candidates[0]["accesskey"]
|
||||
|
||||
r = httpx.get("https://krcs.kugou.com/download", params={
|
||||
"ver": 1,
|
||||
"man": "yes",
|
||||
"client": "mobi",
|
||||
"format": "lrc",
|
||||
"id": id_,
|
||||
"accesskey": accesskey
|
||||
}).json()
|
||||
|
||||
krc = decode_krc(r["content"])
|
||||
|
||||
krc = RE_WORD_TIMING.sub("", krc)
|
||||
lines = reformat_timings(islice(filterfalse(RE_KRC_JUNK.match, krc.splitlines()), 1, None))
|
||||
|
||||
return "\n".join(lines)
|
36
lrc_dl/providers/musixmatch.py
Normal file
36
lrc_dl/providers/musixmatch.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
from typing import Optional
|
||||
import httpx
|
||||
|
||||
from lrc_dl.core import Song, AbstractProvider
|
||||
from lrc_dl.registry import lyrics_provider
|
||||
|
||||
|
||||
@lyrics_provider
|
||||
class Musixmatch(AbstractProvider):
|
||||
name = "musixmatch"
|
||||
|
||||
def __init__(self, token: str) -> None:
|
||||
self.token = token
|
||||
|
||||
def fetch_lyrics(self, song: Song) -> Optional[str]:
|
||||
response = httpx.get("https://apic-desktop.musixmatch.com/ws/1.1/macro.subtitles.get", params={
|
||||
"format": "json",
|
||||
"namespace": "lyrics_synched",
|
||||
"part": "lyrics_crowd,user,lyrics_verified_by",
|
||||
"user_language": "en",
|
||||
"f_subtitle_length_max_deviation": 1,
|
||||
"subtitle_format": "lrc",
|
||||
"app_id": "web-desktop-app-v1.0",
|
||||
"usertoken": self.token,
|
||||
|
||||
"q_artist": song.artist,
|
||||
"q_track": song.title,
|
||||
"q_album": song.album,
|
||||
}, follow_redirects=True).json()
|
||||
|
||||
response = response["message"]["body"]["macro_calls"]["track.subtitles.get"]["message"]["body"]
|
||||
|
||||
if not response:
|
||||
return None
|
||||
|
||||
return response["subtitle_list"][0]["subtitle"]["subtitle_body"]
|
79
lrc_dl/providers/youtube.py
Normal file
79
lrc_dl/providers/youtube.py
Normal file
|
@ -0,0 +1,79 @@
|
|||
from typing import Optional, Dict
|
||||
from contextlib import redirect_stdout
|
||||
import subprocess
|
||||
import io
|
||||
import urllib
|
||||
import unittest.mock
|
||||
|
||||
from yt_dlp import YoutubeDL
|
||||
|
||||
from lrc_dl.core import Song, AbstractProvider
|
||||
from lrc_dl.registry import lyrics_provider
|
||||
from lrc_dl import utils
|
||||
|
||||
|
||||
@lyrics_provider
|
||||
class Youtube(AbstractProvider):
|
||||
name = "youtube"
|
||||
|
||||
def _craft_search_link(self, song: Song) -> str:
|
||||
query = f"{song.artist} - {song.title}"
|
||||
query = urllib.parse.quote(query)
|
||||
# sp=... means search only videos with subtitles
|
||||
url = f"https://www.youtube.com/results?search_query={query}&sp=EgIoAQ%253D%253D"
|
||||
return url
|
||||
|
||||
def _download_subtitles(self, video_id: str) -> str:
|
||||
# buffer = io.BytesIO()
|
||||
buffer = io.StringIO()
|
||||
|
||||
# A dirty monkey patch; youtube-dl does not
|
||||
# support "-" filename for subtitles, so we
|
||||
# just force it to use it here.
|
||||
with unittest.mock.patch("yt_dlp.YoutubeDL.subtitles_filename", new=lambda *_: "-"):
|
||||
with redirect_stdout(buffer):
|
||||
with YoutubeDL({"writesubtitles": True, "skip_download": True, "subtitlesformat": "srt/vtt/best", 'logtostderr': True}) as ydl:
|
||||
ydl.download(video_id)
|
||||
|
||||
return buffer.getvalue()
|
||||
|
||||
def _subtitles_to_lyrics(self, subtitles: str) -> str:
|
||||
# "-fflags +bitexact" prevents ffmpeg from
|
||||
# writing metadata to .lrc file
|
||||
# TODO: use `with` statement
|
||||
process = subprocess.Popen(["ffmpeg", "-loglevel", "quiet", "-i", "-", "-f", "lrc", "-fflags", "+bitexact", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
|
||||
if not process.stdin or not process.stdout:
|
||||
return ""
|
||||
|
||||
process.stdin.write(subtitles.encode())
|
||||
process.stdin.close()
|
||||
process.wait()
|
||||
return process.stdout.read().decode()[1:]
|
||||
|
||||
def fetch_lyrics(self, song: Song) -> Optional[str]:
|
||||
search_link = self._craft_search_link(song)
|
||||
with YoutubeDL({"extract_flat": True, "playlistend": 10}) as ydl:
|
||||
videos = ydl.extract_info(search_link)["entries"]
|
||||
|
||||
if song.duration:
|
||||
def match_duration(video: Dict) -> bool:
|
||||
return utils.threshold_equal(video["duration"], song.duration, 2)
|
||||
videos = filter(match_duration, videos)
|
||||
|
||||
def match_title(video: Dict) -> bool:
|
||||
return True
|
||||
videos = filter(match_title, videos)
|
||||
|
||||
video = utils.next_or_none(videos)
|
||||
|
||||
if not video:
|
||||
return None
|
||||
|
||||
subtitles = self._download_subtitles(video["id"])
|
||||
lyrics = self._subtitles_to_lyrics(subtitles)
|
||||
|
||||
if lyrics != "":
|
||||
return lyrics
|
||||
|
||||
return None
|
20
lrc_dl/registry.py
Normal file
20
lrc_dl/registry.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from lrc_dl.core import AbstractProvider
|
||||
|
||||
|
||||
class Registry:
|
||||
providers: dict[str, type[AbstractProvider]] = {}
|
||||
|
||||
@staticmethod
|
||||
def get_synced_providers() -> dict[str, type[AbstractProvider]]:
|
||||
# TODO: stub
|
||||
return dict(Registry.providers)
|
||||
|
||||
@staticmethod
|
||||
def register_provider(provider_class: type[AbstractProvider]) -> None:
|
||||
Registry.providers[provider_class.name] = provider_class
|
||||
|
||||
|
||||
def lyrics_provider(cls: type[AbstractProvider]) -> type[AbstractProvider]:
|
||||
Registry.register_provider(cls)
|
||||
|
||||
return cls
|
14
lrc_dl/utils.py
Normal file
14
lrc_dl/utils.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from typing import Iterator, Optional, TypeVar
|
||||
|
||||
|
||||
def threshold_equal(a: float, b: float, epsilon: float) -> bool:
|
||||
return abs(a - b) <= epsilon
|
||||
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
def next_or_none(iterator: Iterator[T]) -> Optional[T]:
|
||||
try:
|
||||
return next(iterator)
|
||||
except StopIteration:
|
||||
return None
|
Loading…
Add table
Add a link
Reference in a new issue