✨ Lyrics are downloaded to separate *.lrc file. Implements #19

This commit is contained in:
Robert Honz
2024-02-01 20:53:45 +01:00
parent 1137fe2659
commit f94cd0d51c
2 changed files with 29 additions and 1 deletions
+1
View File
@@ -2,6 +2,7 @@ from enum import Enum
CTX_TIDAL: str = "tidal" CTX_TIDAL: str = "tidal"
REQUESTS_TIMEOUT_SEC = 45 REQUESTS_TIMEOUT_SEC = 45
EXTENSION_LYRICS = ".lrc"
class QualityVideo(Enum): class QualityVideo(Enum):
+28 -1
View File
@@ -18,6 +18,7 @@ from tidalapi import Album, Mix, Playlist, Session, Track, UserPlaylist, Video
from tidal_dl_ng.config import Settings from tidal_dl_ng.config import Settings
from tidal_dl_ng.constants import ( from tidal_dl_ng.constants import (
EXTENSION_LYRICS,
REQUESTS_TIMEOUT_SEC, REQUESTS_TIMEOUT_SEC,
AudioExtensions, AudioExtensions,
CoverDimensions, CoverDimensions,
@@ -239,6 +240,10 @@ class Download:
# Move final file to the configured destination directory. # Move final file to the configured destination directory.
os.makedirs(os.path.dirname(path_file), exist_ok=True) os.makedirs(os.path.dirname(path_file), exist_ok=True)
shutil.move(tmp_path_file, path_file) shutil.move(tmp_path_file, path_file)
# Move lyrics file
if self.settings.data.lyrics_file:
self._move_lyrics(path_file, tmp_path_file)
else: else:
self.fn_logger.debug(f"Download skipped, since file exists: '{path_file}'") self.fn_logger.debug(f"Download skipped, since file exists: '{path_file}'")
@@ -254,12 +259,31 @@ class Download:
return not download_skip, path_file return not download_skip, path_file
def _move_lyrics(self, file_media_dst: str, file_media_src: str):
# Build tmp lyrics filename
tmp_lyrics_file_path: str = file_media_src + EXTENSION_LYRICS
# Check if the file was downloaded
if os.path.isfile(tmp_lyrics_file_path):
# Move it.
shutil.move(tmp_lyrics_file_path, os.path.splitext(file_media_dst)[0] + EXTENSION_LYRICS)
def cover_url(self, sid: str, dimension: CoverDimensions = CoverDimensions.Px320): def cover_url(self, sid: str, dimension: CoverDimensions = CoverDimensions.Px320):
if sid is None: if sid is None:
return "" return ""
return f"https://resources.tidal.com/images/{sid.replace('-', '/')}/{dimension.value}.jpg" return f"https://resources.tidal.com/images/{sid.replace('-', '/')}/{dimension.value}.jpg"
def lyrics_write_file(self, file_path: str, lyrics: str) -> str:
result: str = file_path
try:
with open(file_path, "x", encoding="utf-8") as f:
f.write(lyrics)
except:
result = ""
return result
def metadata_write(self, track: Track, path_file: str): def metadata_write(self, track: Track, path_file: str):
result: bool = False result: bool = False
release_date: str = ( release_date: str = (
@@ -269,7 +293,7 @@ class Download:
isrc: str = track.isrc if hasattr(track, "isrc") and track.isrc else "" isrc: str = track.isrc if hasattr(track, "isrc") and track.isrc else ""
lyrics: str = "" lyrics: str = ""
if self.settings.data.lyrics_embed: if self.settings.data.lyrics_embed or self.settings.data.lyrics_file:
# Try to retrieve lyrics. # Try to retrieve lyrics.
try: try:
lyrics: str = track.lyrics().subtitles if hasattr(track, "lyrics") else "" lyrics: str = track.lyrics().subtitles if hasattr(track, "lyrics") else ""
@@ -277,6 +301,9 @@ class Download:
# TODO: Implement proper logging. # TODO: Implement proper logging.
print(f"Could not retrieve lyrics for `{name_builder_item(track)}`.") print(f"Could not retrieve lyrics for `{name_builder_item(track)}`.")
if lyrics:
self.lyrics_write_file(path_file + EXTENSION_LYRICS, lyrics)
# `None` values are not allowed. # `None` values are not allowed.
m: Metadata = Metadata( m: Metadata = Metadata(
path_file=path_file, path_file=path_file,