From a4930649fc09f9470fe670de6c565680b283b3c1 Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Sat, 16 Sep 2023 22:09:25 -0400 Subject: [PATCH] Incorporate _search into request(). Add helper functions for returning and printing song results. --- ircradio/irc.py | 58 +++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/ircradio/irc.py b/ircradio/irc.py index 47f8b4d..26df571 100644 --- a/ircradio/irc.py +++ b/ircradio/irc.py @@ -136,35 +136,32 @@ class Commands: if not args: await send_message(target=target, message="usage: !request ") - needle = " ".join(args) - try: - songs = Song.search(needle) - except Exception as ex: - return await send_message(target, f"{ex}") - if not songs: - return await send_message(target, "Not found!") + songs = await Commands._return_song_results(*args, target=target, nick=nick, **kwargs) - if len(songs) >= 2: - random.shuffle(songs) - await send_message(target, "Multiple found:") - for s in songs[:8]: - await send_message(target, f"{s.utube_id} | {s.title}") - return + if songs and len(songs) == 1: + song = songs[0] + await radio_default.queue_push(song.filepath) + msg = f"Added {song.title} to the queue" - song = songs[0] - await radio_default.queue_push(song.filepath) - msg = f"Added {song.title} to the queue" + return await send_message(target, msg) - return await send_message(target, msg) + if songs: + return await Commands._print_song_results(*args, target=target, nick=nick, songs=songs, **kwargs) @staticmethod async def search(*args, target=None, nick=None, **kwargs): from ircradio.models import Song + if not args: + return await send_message(target=target, message="usage: !search ") + return await Commands._search(*args, target=target, nick=nick, **kwargs) @staticmethod async def searchq(*args, target=None, nick=None, **kwargs): from ircradio.models import Song + if not args: + return await send_message(target=target, message="usage: !searchq ") + return await Commands._search(*args, target=target, nick=nick, report_quality=True, **kwargs) @staticmethod @@ -172,10 +169,17 @@ class Commands: """search for a title""" from ircradio.models import Song - if not args: - return await send_message(target=target, message="usage: !search ") - report_quality = kwargs.get('report_quality') + + songs = await Commands._return_song_results(*args, target=target, nick=nick, **kwargs) + + if songs: + return await Commands._print_song_results(*args, target=target, nick=nick, report_quality=report_quality, songs=songs, **kwargs) + + @staticmethod + async def _return_song_results(*args, target=None, nick=None, **kwargs) -> Optional[List['Song']]: + from ircradio.models import Song + needle = " ".join(args) # https://git.wownero.com/dsc/ircradio/issues/1 @@ -187,13 +191,25 @@ class Commands: needle = a needle_2nd = b - songs = Song.search(needle) + try: + songs = Song.search(needle) + except Exception as ex: + return await send_message(target, f"{ex}") if not songs: return await send_message(target, "No song(s) found!") if songs and needle_2nd: songs = [s for s in songs if s.title and needle_2nd in s.title.lower()] + if not songs: + return await send_message(target, "No song(s) found after '|'!") + + return songs + + @staticmethod + async def _print_song_results(*args, target=None, nick=None, report_quality=None, songs=None, **kwargs): + from ircradio.models import Song + len_songs = len(songs) max_songs = 6 moar = len_songs > max_songs