From 14303023aa0c25389675ee77c9cdd0e487ca55ed Mon Sep 17 00:00:00 2001 From: BarsTiger Date: Mon, 4 Dec 2023 17:03:00 +0200 Subject: [PATCH] Add non-exact matches for spotify (in settings) --- bot/handlers/on_chosen/spotify.py | 33 ++++++++++++++++++------------- bot/modules/settings/model.py | 18 ++++++++++++++++- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/bot/handlers/on_chosen/spotify.py b/bot/handlers/on_chosen/spotify.py index f801b84..607d2b8 100644 --- a/bot/handlers/on_chosen/spotify.py +++ b/bot/handlers/on_chosen/spotify.py @@ -34,16 +34,17 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot, song.full_name, exact_match=True, ) - if ((song.all_artists != yt_song.all_artists or song.name != yt_song.name) - and not not_strict_name(song, yt_song)): - await bot.edit_message_caption( - inline_message_id=chosen_result.inline_message_id, - caption='🙄 Cannot find this song on YouTube, trying Deezer...', - reply_markup=None, - parse_mode='HTML', - ) - yt_song = None - bytestream = False + if settings['exact_spotify_search'].value == 'yes': + if ((song.all_artists != yt_song.all_artists or song.name != yt_song.name) + and not not_strict_name(song, yt_song)): + await bot.edit_message_caption( + inline_message_id=chosen_result.inline_message_id, + caption='🙄 Cannot find this song on YouTube, trying Deezer...', + reply_markup=None, + parse_mode='HTML', + ) + yt_song = None + bytestream = False try: if bytestream is None: @@ -98,7 +99,8 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot, assert e if audio: - db.spotify[song.id] = audio.audio.file_id + if settings['exact_spotify_search'].value == 'yes': + db.spotify[song.id] = audio.audio.file_id await bot.edit_message_media( inline_message_id=chosen_result.inline_message_id, @@ -135,9 +137,11 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot, duration=bytestream.duration, ) db.youtube[yt_song.id] = audio.audio.file_id - db.spotify[song.id] = audio.audio.file_id db.recoded[yt_song.id] = True - db.recoded[song.id] = True + + if settings['exact_spotify_search'].value == 'yes': + db.spotify[song.id] = audio.audio.file_id + db.recoded[song.id] = True await bot.edit_message_caption( inline_message_id=chosen_result.inline_message_id, @@ -150,6 +154,7 @@ async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot, ) elif yt_song and settings['recode_youtube'].value == 'no': db.recoded[yt_song.id] = audio.message_id - db.recoded[song.id] = audio.message_id + if settings['exact_spotify_search'].value == 'yes': + db.recoded[song.id] = audio.message_id await db.occasionally_write() diff --git a/bot/modules/settings/model.py b/bot/modules/settings/model.py index 4fb09eb..a3e5012 100644 --- a/bot/modules/settings/model.py +++ b/bot/modules/settings/model.py @@ -28,6 +28,18 @@ settings_strings: dict[str, Setting] = { 'no': 'Send original file', 'yes': 'Recode to libmp3lame' }, + ), + 'exact_spotify_search': Setting( + name='Only exact Spotify matches', + description='When searching on Youtube from Spotify, show only exact matches, ' + 'may protect against inaccurate matches, but at the same time it ' + 'can lose reuploaded tracks. Should be enabled always, except in ' + 'situations where the track is not found on both YouTube and ' + 'Deezer', + choices={ + 'yes': 'Only exact matches', + 'no': 'Fuzzy matches also' + }, ) } @@ -50,7 +62,11 @@ class UserSettings: s = settings_strings.get(item) if s is None: return None - s.value = db.settings[self.user_id][item] + try: + s.value = db.settings[self.user_id][item] + except KeyError: + s.value = list(s.choices)[0] + self[item] = s.value return s def __setitem__(self, key, value):