Skip to content

Commit

Permalink
Fix nested quotation marks and make unsynced/synced lyrics distiction…
Browse files Browse the repository at this point in the history
… clear
  • Loading branch information
twodoorcoupe committed Nov 2, 2024
1 parent 18d2f7e commit 6ea9dc5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
55 changes: 26 additions & 29 deletions plugins/lrclib_lyrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
REQUESTS_DELAY = 100

# Options
ADD_LYRICS = "add_lyrics"
ADD_SYNCEDLYRICS = "add_syncedlyrics"
ADD_UNSYNCED_LYRICS = "add_unsynced_lyrics"
ADD_SYNCED_LYRICS = "add_synced_lyrics"
NEVER_REPLACE_LYRICS = "never_replace_lyrics"
LRC_FILENAME = "exported_lrc_filename"
EXPORT_LRC = "exported_lrc"
Expand All @@ -64,22 +64,21 @@
def get_lyrics(track, file):
album = track.album
metadata = file.metadata
if not (config.setting[ADD_LYRICS] or config.setting[ADD_SYNCEDLYRICS]):
if not (config.setting[ADD_UNSYNCED_LYRICS] or config.setting[ADD_SYNCED_LYRICS]):
return
if not (metadata.get("title") and metadata.get("artist")):
log.debug(f"Skipping fetching lyrics for track in {album} as both title and artist are required")
return
if config.setting[NEVER_REPLACE_LYRICS] and metadata.get("lyrics"):
log.debug(f"Skipping fetching lyrics for {metadata["title"]} as lyrics are already embedded")
log.debug(f"Skipping fetching lyrics for {metadata['title']} as lyrics are already embedded")
return
args = {
"track_name": metadata["title"],
"artist_name": metadata["artist"],
}
if metadata.get("album"):
args["album_name"] = metadata["album"]
handler = partial(response_handler, album, metadata)
album._requests += 1
handler = partial(response_handler, metadata)
album.tagger.webservice.get_url(
method="GET",
handler=handler,
Expand All @@ -89,31 +88,29 @@ def get_lyrics(track, file):
)


def response_handler(album, metadata, document, reply, error):
def response_handler(metadata, document, reply, error):
if document and not error:
lyrics = document.get("plainLyrics")
syncedlyrics = document.get("syncedLyrics")
if lyrics:
lyrics_cache[metadata["title"]] = lyrics
if ((not config.setting[ADD_LYRICS]) or
unsynced_lyrics = document.get("plainLyrics")
synced_lyrics = document.get("syncedLyrics")
if unsynced_lyrics:
lyrics_cache[metadata["title"]] = unsynced_lyrics
if ((not config.setting[ADD_UNSYNCED_LYRICS]) or
(config.setting[NEVER_REPLACE_LYRICS] and metadata.get("lyrics"))):
return
metadata["lyrics"] = lyrics
if syncedlyrics:
lyrics_cache[metadata["title"]] = syncedlyrics
metadata["lyrics"] = unsynced_lyrics
if synced_lyrics:
lyrics_cache[metadata["title"]] = synced_lyrics
# Support for the syncedlyrics tag is not available yet
# if (not config.setting[ADD_SYNCEDLYRICS] or
# if (not config.setting[ADD_SYNCED_LYRICS] or
# (config.setting[NEVER_REPLACE_LYRICS] and metadata.get("syncedlyrics"))):
# return
# metadata["syncedlyrics"] = syncedlyrics
else:
log.debug(f"Could not fetch lyrics for {metadata["title"]}")
album._requests -= 1
album._finalize_loading(None)
log.debug(f"Could not fetch lyrics for {metadata['title']}")


def get_lrc_file_name(file):
filename = f"{tags_pattern.sub("{}", config.setting[LRC_FILENAME])}"
filename = f"{tags_pattern.sub('{}', config.setting[LRC_FILENAME])}"
tags = tags_pattern.findall(config.setting[LRC_FILENAME])
values = []
for tag in tags:
Expand All @@ -138,9 +135,9 @@ def export_lrc_file(file):
file.write(lyrics)
log.debug(f"Created new lyrics file at {filename}")
except OSError:
log.debug(f"Could not create the lrc file for {metadata["title"]}")
log.debug(f"Could not create the lrc file for {metadata['title']}")
else:
log.debug(f"Could not export any lyrics for {metadata["title"]}")
log.debug(f"Could not export any lyrics for {metadata['title']}")


class ImportLrc(BaseAction):
Expand All @@ -161,7 +158,7 @@ def callback(self, objs):
else:
file.metadata["lyrics"] = lyrics
except FileNotFoundError:
log.debug(f"Could not find matching lrc file for {file.metadata["title"]}")
log.debug(f"Could not find matching lrc file for {file.metadata['title']}")


class LrclibLyricsOptions(OptionsPage):
Expand All @@ -173,8 +170,8 @@ class LrclibLyricsOptions(OptionsPage):
__default_naming = "%filename%.lrc"

options = [
config.BoolOption("setting", ADD_LYRICS, True),
config.BoolOption("setting", ADD_SYNCEDLYRICS, False),
config.BoolOption("setting", ADD_UNSYNCED_LYRICS, True),
config.BoolOption("setting", ADD_SYNCED_LYRICS, False),
config.BoolOption("setting", NEVER_REPLACE_LYRICS, False),
config.TextOption("setting", LRC_FILENAME, __default_naming),
config.BoolOption("setting", EXPORT_LRC, False),
Expand All @@ -187,16 +184,16 @@ def __init__(self, parent=None):
self.ui.setupUi(self)

def load(self):
self.ui.lyrics.setChecked(config.setting[ADD_LYRICS])
self.ui.syncedlyrics.setChecked(config.setting[ADD_SYNCEDLYRICS])
self.ui.lyrics.setChecked(config.setting[ADD_UNSYNCED_LYRICS])
self.ui.syncedlyrics.setChecked(config.setting[ADD_SYNCED_LYRICS])
self.ui.replace_embedded.setChecked(config.setting[NEVER_REPLACE_LYRICS])
self.ui.lrc_name.setText(config.setting[LRC_FILENAME])
self.ui.export_lyrics.setChecked(config.setting[EXPORT_LRC])
self.ui.replace_exported.setChecked(config.setting[NEVER_REPLACE_LRC])

def save(self):
config.setting[ADD_LYRICS] = self.ui.lyrics.isChecked()
config.setting[ADD_SYNCEDLYRICS] = self.ui.syncedlyrics.isChecked()
config.setting[ADD_UNSYNCED_LYRICS] = self.ui.lyrics.isChecked()
config.setting[ADD_SYNCED_LYRICS] = self.ui.syncedlyrics.isChecked()
config.setting[NEVER_REPLACE_LYRICS] = self.ui.replace_embedded.isChecked()
config.setting[LRC_FILENAME] = self.ui.lrc_name.text()
config.setting[EXPORT_LRC] = self.ui.export_lyrics.isChecked()
Expand Down
8 changes: 4 additions & 4 deletions plugins/lrclib_lyrics/option_lrclib_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def retranslateUi(self, OptionLrclibLyrics):
_translate = QtCore.QCoreApplication.translate
OptionLrclibLyrics.setWindowTitle(_translate("OptionLrclibLyrics", "Form"))
self.groupBox.setTitle(_translate("OptionLrclibLyrics", "Embedded Lyrics Options"))
self.lyrics.setText(_translate("OptionLrclibLyrics", "Download and embed lyrics"))
self.syncedlyrics.setText(_translate("OptionLrclibLyrics", "Download and embed synchronized lyrics"))
self.replace_embedded.setText(_translate("OptionLrclibLyrics", "Never replace embedded lyrics if already present"))
self.lyrics.setText(_translate("OptionLrclibLyrics", "Download and embed unsynced lyrics"))
self.syncedlyrics.setText(_translate("OptionLrclibLyrics", "Download and embed synced lyrics"))
self.replace_embedded.setText(_translate("OptionLrclibLyrics", "Never replace any embedded lyrics if already present"))
self.groupBox_2.setTitle(_translate("OptionLrclibLyrics", "Lrc Files Options"))
self.label.setText(_translate("OptionLrclibLyrics", "Use the following name pattern for lrc files:"))
self.export_lyrics.setText(_translate("OptionLrclibLyrics", "Export lyrics to lrc file when saving"))
self.export_lyrics.setText(_translate("OptionLrclibLyrics", "Export lyrics to lrc file when saving (priority to synced lyrics)"))
self.replace_exported.setText(_translate("OptionLrclibLyrics", "Never replace lrc files if already present"))
8 changes: 4 additions & 4 deletions plugins/lrclib_lyrics/option_lrclib_lyrics.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<item>
<widget class="QCheckBox" name="lyrics">
<property name="text">
<string>Download and embed lyrics</string>
<string>Download and embed unsynced lyrics</string>
</property>
</widget>
</item>
Expand All @@ -33,7 +33,7 @@
<bool>false</bool>
</property>
<property name="text">
<string>Download and embed synchronized lyrics</string>
<string>Download and embed synced lyrics</string>
</property>
<property name="checkable">
<bool>false</bool>
Expand All @@ -43,7 +43,7 @@
<item>
<widget class="QCheckBox" name="replace_embedded">
<property name="text">
<string>Never replace embedded lyrics if already present</string>
<string>Never replace any embedded lyrics if already present</string>
</property>
</widget>
</item>
Expand All @@ -69,7 +69,7 @@
<item>
<widget class="QCheckBox" name="export_lyrics">
<property name="text">
<string>Export lyrics to lrc file when saving</string>
<string>Export lyrics to lrc file when saving (priority to synced lyrics)</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 6ea9dc5

Please sign in to comment.