From 4289c4d834f9782373cb6162518e9814bcb917b8 Mon Sep 17 00:00:00 2001 From: Akash R Chandran Date: Sun, 8 Jan 2023 23:18:11 +0530 Subject: [PATCH] added support for id tags in lrc --- setup.py | 2 +- syrics/core.py | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index d3cbf5d..abc7bef 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ def read_file(fname): setup( name=pkg_name, - version="0.0.1.3", + version="0.0.1.4", author="Akash R Chandran", author_email="chandranrakash@gmail.com", description="A command line tool to fetch lyrics from spotify and save it to lrc file. It can fetch both synced and unsynced lyrics from spotify. ", diff --git a/syrics/core.py b/syrics/core.py index 22f225d..55f1e05 100755 --- a/syrics/core.py +++ b/syrics/core.py @@ -61,13 +61,19 @@ def get_playlist_tracks(playlist_id: str): return client.playlist_tracks(playlist_id, play_data['tracks']['total']), play_folder -def format_lrc(lyrics_json): +def format_lrc(lyrics_json, track_data): lyrics = lyrics_json['lyrics']['lines'] - if lyrics_json['lyrics']['syncType'] == 'UNSYNCED' or not config['synced_lyrics']: - lrc = [lines['words'] for lines in lyrics] - else: - lrc = [] - for lines in lyrics: + minutes, seconds = divmod(int(track_data["duration_ms"]) / 1000, 60) + lrc = [ + f'[ti:{track_data["name"]}]', + f'[al:{track_data["album_name"]}]', + f'[ar:{track_data["artist"]}]', + f'[length: {minutes:0>2.0f}:{seconds:05.2f}]', + ] + for lines in lyrics: + if lyrics_json['lyrics']['syncType'] == 'UNSYNCED' or not config['synced_lyrics']: + lrc.append(lines['words']) + else: duration = int(lines['startTimeMs']) minutes, seconds = divmod(duration / 1000, 60) lrc.append(f'[{minutes:0>2.0f}:{seconds:05.2f}] {lines["words"]}') @@ -125,7 +131,7 @@ def download_lyrics(track_ids: list, folder: str = None): unable.append(track['name']) continue file_name = f"{folder}/{rename_using_format(config['file_name'], track)}.lrc" - save_lyrics(format_lrc(lyrics_json), path=file_name) + save_lyrics(format_lrc(lyrics_json, track), path=file_name) return unable def fetch_files(path: str): @@ -135,13 +141,14 @@ def fetch_files(path: str): tag = TinyTag.get(os.path.join(path, files)) query = client.search(q=f"track:{tag.title} album:{tag.album}", type="track", limit=1) if track := query['tracks']['items']: + sanitize_track_data(track[0]) file_name = f"{os.path.splitext(tag._filehandler.name)[0]}.lrc" lyrics_json = client.get_lyrics(track[0]['id']) if not lyrics_json: unable.append(tag.title) continue if not os.path.exists(file_name) and config.get('force_download'): - save_lyrics(format_lrc(lyrics_json), path=file_name) + save_lyrics(format_lrc(lyrics_json, track[0]), path=file_name) else: unable.append(tag.title) return unable