Skip to content

Commit

Permalink
add support for downloading plain lyrics
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Jan 22, 2025
1 parent 87e54c0 commit 5f38f1a
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 114 deletions.
50 changes: 24 additions & 26 deletions src/onthespot/api/apple_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,31 +251,28 @@ def apple_music_get_lyrics(session, item_id, item_type, metadata, filepath):
ttml_data = track_data.get('data', [])[0].get('relationships', {}).get('lyrics', {}).get('data', [])[0].get('attributes', {}).get('ttml')
lyrics_list = []

if config.get("embed_branding"):
lyrics_list.append('[re:OnTheSpot]')

for key in metadata.keys():
value = metadata[key]

if key in ['title', 'track_title', 'tracktitle'] and config.get("embed_name"):
lyrics_list.append(f'[ti:{value}]')

elif key == 'artists' and config.get("embed_artist"):
lyrics_list.append(f'[ar:{value}]')

elif key in ['album_name', 'album'] and config.get("embed_album"):
lyrics_list.append(f'[al:{value}]')

elif key in ['writers'] and config.get("embed_writers"):
lyrics_list.append(f'[au:{value}]')

if config.get("embed_length"):
l_ms = int(metadata['length'])
if round((l_ms/1000)/60) < 10:
digit="0"
else:
digit=""
lyrics_list.append(f'[length:{digit}{str((l_ms/1000)/60)[:1]}:{round((l_ms/1000)%60)}]\n')
if not config.get('only_download_plain_lyrics'):
if config.get("embed_branding"):
lyrics_list.append('[re:OnTheSpot]')

for key in metadata.keys():
value = metadata[key]
if key in ['title', 'track_title', 'tracktitle'] and config.get("embed_name"):
lyrics_list.append(f'[ti:{value}]')
elif key == 'artists' and config.get("embed_artist"):
lyrics_list.append(f'[ar:{value}]')
elif key in ['album_name', 'album'] and config.get("embed_album"):
lyrics_list.append(f'[al:{value}]')
elif key in ['writers'] and config.get("embed_writers"):
lyrics_list.append(f'[au:{value}]')

if config.get("embed_length"):
l_ms = int(metadata['length'])
if round((l_ms/1000)/60) < 10:
digit="0"
else:
digit=""
lyrics_list.append(f'[length:{digit}{str((l_ms/1000)/60)[:1]}:{round((l_ms/1000)%60)}]\n')

default_length = len(lyrics_list)

Expand All @@ -299,7 +296,8 @@ def apple_music_get_lyrics(session, item_id, item_type, metadata, filepath):
except (TypeError, ValueError):
milliseconds = '0'
formatted_time = f"{int(minutes):02}:{int(seconds):02}.{milliseconds.replace('s', '')[:2]}"
lyric = f'[{formatted_time}] {lyric}'
if not config.get('only_download_plain_lyrics'):
lyric = f'[{formatted_time}] {lyric}'

lyrics_list.append(lyric)

Expand Down
62 changes: 31 additions & 31 deletions src/onthespot/api/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,46 +295,46 @@ def spotify_get_lyrics(token, item_id, item_type, metadata, filepath):
logger.info(f"Failed to find lyrics for {item_type}: {item_id}")
return None

if config.get("embed_branding"):
lyrics.append('[re:OnTheSpot]')
if not config.get('only_download_plain_lyrics'):
if config.get("embed_branding"):
lyrics.append('[re:OnTheSpot]')

for key in metadata.keys():
value = metadata[key]
if key in ['title', 'track_title', 'tracktitle'] and config.get("embed_name"):
title = value
lyrics.append(f'[ti:{title}]')
elif key == 'artists' and config.get("embed_artist"):
artist = value
lyrics.append(f'[ar:{artist}]')
elif key in ['album_name', 'album'] and config.get("embed_album"):
album = value
lyrics.append(f'[al:{album}]')
elif key in ['writers'] and config.get("embed_writers"):
author = value
lyrics.append(f'[au:{author}]')

for key in metadata.keys():
value = metadata[key]

if key in ['title', 'track_title', 'tracktitle'] and config.get("embed_name"):
title = value
lyrics.append(f'[ti:{title}]')

elif key == 'artists' and config.get("embed_artist"):
artist = value
lyrics.append(f'[ar:{artist}]')

elif key in ['album_name', 'album'] and config.get("embed_album"):
album = value
lyrics.append(f'[al:{album}]')

elif key in ['writers'] and config.get("embed_writers"):
author = value
lyrics.append(f'[au:{author}]')

if item_type == "track":
lyrics.append(f'[by:{resp["lyrics"]["provider"]}]')
if item_type == "track":
lyrics.append(f'[by:{resp["lyrics"]["provider"]}]')

if config.get("embed_length"):
l_ms = int(metadata['length'])
if round((l_ms/1000)/60) < 10:
digit="0"
else:
digit=""
lyrics.append(f'[length:{digit}{round((l_ms/1000)/60)}:{round((l_ms/1000)%60)}]\n')
if config.get("embed_length"):
l_ms = int(metadata['length'])
if round((l_ms/1000)/60) < 10:
digit="0"
else:
digit=""
lyrics.append(f'[length:{digit}{round((l_ms/1000)/60)}:{round((l_ms/1000)%60)}]\n')

default_length = len(lyrics)

if item_type == "track":
if resp["lyrics"]["syncType"] == "LINE_SYNCED":
for line in resp["lyrics"]["lines"]:
minutes, seconds = divmod(int(line['startTimeMs']) / 1000, 60)
lyrics.append(f'[{minutes:0>2.0f}:{seconds:05.2f}] {line["words"]}')
if not config.get('only_download_plain_lyrics'):
lyrics.append(f'[{minutes:0>2.0f}:{seconds:05.2f}] {line["words"]}')
else:
lyrics.append(line["words"])
elif resp["lyrics"]["syncType"] == "UNSYNCED" and not config.get("only_download_synced_lyrics"):
lyrics = [line['words'] for line in resp['lyrics']['lines']]

Expand Down
53 changes: 26 additions & 27 deletions src/onthespot/api/tidal.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,33 +274,30 @@ def tidal_get_lyrics(token, item_id, item_type, metadata, filepath):
if not resp:
return False

if config.get("embed_branding"):
lyrics.append('[re:OnTheSpot]')

for key in metadata.keys():
value = metadata[key]

if key in ['title', 'track_title', 'tracktitle'] and config.get("embed_name"):
lyrics.append(f'[ti:{value}]')

elif key == 'artists' and config.get("embed_artist"):
lyrics.append(f'[ar:{value}]')

elif key in ['album_name', 'album'] and config.get("embed_album"):
lyrics.append(f'[al:{value}]')

elif key in ['writers'] and config.get("embed_writers"):
lyrics.append(f'[au:{value}]')

lyrics.append(f'[by:{resp.get("lyricsProvider").title()}]')

if config.get("embed_length"):
l_ms = int(metadata['length'])
if round((l_ms/1000)/60) < 10:
digit="0"
else:
digit=""
lyrics.append(f'[length:{digit}{round((l_ms/1000)/60)}:{round((l_ms/1000)%60)}]\n')
if not config.get('only_download_plain_lyrics'):
if config.get("embed_branding"):
lyrics.append('[re:OnTheSpot]')

for key in metadata.keys():
value = metadata[key]
if key in ['title', 'track_title', 'tracktitle'] and config.get("embed_name"):
lyrics.append(f'[ti:{value}]')
elif key == 'artists' and config.get("embed_artist"):
lyrics.append(f'[ar:{value}]')
elif key in ['album_name', 'album'] and config.get("embed_album"):
lyrics.append(f'[al:{value}]')
elif key in ['writers'] and config.get("embed_writers"):
lyrics.append(f'[au:{value}]')

lyrics.append(f'[by:{resp.get("lyricsProvider").title()}]')

if config.get("embed_length"):
l_ms = int(metadata['length'])
if round((l_ms/1000)/60) < 10:
digit="0"
else:
digit=""
lyrics.append(f'[length:{digit}{round((l_ms/1000)/60)}:{round((l_ms/1000)%60)}]\n')

default_length = len(lyrics)

Expand All @@ -312,6 +309,8 @@ def tidal_get_lyrics(token, item_id, item_type, metadata, filepath):
lyric_data = ''

for line in lyric_data.split('\n'):
if config.get('only_download_plain_lyrics'):
line = line.split('] ', 1)[-1]
lyrics.append(line)

merged_lyrics = '\n'.join(lyrics)
Expand Down
4 changes: 2 additions & 2 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def run(self):
logger.info('Overwriting Existing Metadata')

# Lyrics
if item_service in ("spotify", "tidal"):
if item_service in ("apple_music", "spotify", "tidal") and config.get('download_lyrics'):
item['item_status'] = 'Getting Lyrics'
if self.gui:
self.progress.emit(item, self.tr("Getting Lyrics"), 99)
Expand Down Expand Up @@ -574,7 +574,7 @@ def run(self):
# Audio Formatting
if item_type in ('track', 'podcast_episode'):
# Lyrics
if item_service in ("apple_music", "spotify", "tidal"):
if item_service in ("apple_music", "spotify", "tidal") and config.get('download_lyrics'):
item['item_status'] = 'Getting Lyrics'
if self.gui:
self.progress.emit(item, self.tr("Getting Lyrics"), 99)
Expand Down
64 changes: 43 additions & 21 deletions src/onthespot/gui/qtui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-3485</y>
<y>-1924</y>
<width>627</width>
<height>4132</height>
</rect>
Expand Down Expand Up @@ -2571,26 +2571,26 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="group_playlist_cfg_m3u_11">
<widget class="QGroupBox" name="group_lyrics_cfg_only_synced_5">
<property name="title">
<string/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_86">
<layout class="QHBoxLayout" name="horizontalLayout_55">
<item>
<widget class="QCheckBox" name="save_lrc_file">
<widget class="QCheckBox" name="only_download_plain_lyrics">
<property name="toolTip">
<string>This feature requires a premium account.</string>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Save LRC File*</string>
<string>Only Download Plain Lyrics*</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_26">
<spacer name="horizontalSpacer_28">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
Expand All @@ -2617,6 +2617,41 @@
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_84">
<item>
<widget class="QGroupBox" name="group_playlist_cfg_m3u_11">
<property name="title">
<string/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_86">
<item>
<widget class="QCheckBox" name="save_lrc_file">
<property name="toolTip">
<string>This feature requires a premium account.</string>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Save LRC File*</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_26">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="translate_file_path_2">
<property name="title">
Expand Down Expand Up @@ -2652,19 +2687,6 @@
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -4707,8 +4729,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>140</width>
<height>1309</height>
<width>641</width>
<height>405</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
Expand Down
6 changes: 4 additions & 2 deletions src/onthespot/gui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ def load_config(self):
self.file_hertz.setValue(config.get("file_hertz"))
self.use_custom_file_bitrate.setChecked(config.get("use_custom_file_bitrate"))
self.download_lyrics.setChecked(config.get("download_lyrics"))
self.save_lrc_file.setChecked(config.get("save_lrc_file"))
self.only_download_synced_lyrics.setChecked(config.get("only_download_synced_lyrics"))
self.only_download_plain_lyrics.setChecked(config.get("only_download_plain_lyrics"))
self.save_lrc_file.setChecked(config.get("save_lrc_file"))
self.translate_file_path.setChecked(config.get("translate_file_path"))

# Audio Metadata Settings
Expand Down Expand Up @@ -267,8 +268,9 @@ def save_config(self):
config.set('file_hertz', self.file_hertz.value())
config.set('use_custom_file_bitrate', self.use_custom_file_bitrate.isChecked())
config.set('download_lyrics', self.download_lyrics.isChecked())
config.set('save_lrc_file', self.save_lrc_file.isChecked())
config.set('only_download_synced_lyrics', self.only_download_synced_lyrics.isChecked())
config.set('only_download_plain_lyrics', self.only_download_plain_lyrics.isChecked())
config.set('save_lrc_file', self.save_lrc_file.isChecked())
config.set('translate_file_path', self.translate_file_path.isChecked())

# Audio Metadata Settings
Expand Down
3 changes: 2 additions & 1 deletion src/onthespot/otsconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ def __init__(self, cfg_path=None):
"file_hertz": 44100, # Converted file hertz
"use_custom_file_bitrate": False, # Use bitrate specified by file bitrate
"download_lyrics": False, # Enable lyrics download
"save_lrc_file": False, # Download .lrc file alongside track
"only_download_synced_lyrics": False, # Only download synced lyrics
"only_download_plain_lyrics": False, # Only download plain lyrics
"save_lrc_file": False, # Download .lrc file alongside track
"translate_file_path": False, # Translate downloaded file path to application language

# Audio Metadata Settings
Expand Down
Loading

0 comments on commit 5f38f1a

Please sign in to comment.