From 724606e4b31ee4893faaf4f3929663942dfcd7ca Mon Sep 17 00:00:00 2001 From: John Doe Date: Thu, 26 Oct 2023 01:12:03 +0530 Subject: [PATCH] feat: :sparkles: add: new column for sorted results --- radioactive/__main__.py | 2 +- radioactive/args.py | 2 +- radioactive/handler.py | 94 ++++++++++++++++++++++++++-------------- radioactive/utilities.py | 21 ++++----- 4 files changed, 72 insertions(+), 47 deletions(-) diff --git a/radioactive/__main__.py b/radioactive/__main__.py index 394b8c9..837da7a 100755 --- a/radioactive/__main__.py +++ b/radioactive/__main__.py @@ -156,7 +156,7 @@ def main(): if options["add_station"]: handle_add_station(alias) - check_sort_by_parameter(options["sort_by"]) + options["sort_by"] = check_sort_by_parameter(options["sort_by"]) handle_update_screen(app) diff --git a/radioactive/args.py b/radioactive/args.py index b312230..29b0187 100644 --- a/radioactive/args.py +++ b/radioactive/args.py @@ -111,7 +111,7 @@ def __init__(self): "--sort", action="store", dest="stations_sort_by", - default="clickcount", + default="name", help="Sort stations", ) diff --git a/radioactive/handler.py b/radioactive/handler.py index 4b40be7..5a5030c 100644 --- a/radioactive/handler.py +++ b/radioactive/handler.py @@ -51,9 +51,8 @@ def get_country_code(self, name): return country["iso_3166_1"] return None - def station_validator(self): + def station_validator(self, sort_by: str = "name"): """Validates a response from the API and takes appropriate decision""" - # when no response from the API if not self.response: log.error("No stations found by the name") @@ -67,20 +66,36 @@ def station_validator(self): # table.add_column("UUID", justify="center") table.add_column("Country", justify="center") table.add_column("Tags", justify="center") + if sort_by != "name": + table.add_column(sort_by, justify="left") log.info("showing {} stations with the name!".format(len(self.response))) for i in range(0, len(self.response)): station = self.response[i] - table.add_row( - str(i + 1), - trim_string(station["name"], max_length=50), - # station["stationuuid"], - station["countrycode"], - trim_string( - station["tags"] - ), # trimming tags to make the table shorter - ) + if sort_by != "name": + # TODO: find a better way of doing this, + # if name is the parameter for sort don't add extra column + table.add_row( + str(i + 1), + trim_string(station["name"], max_length=50), + # station["stationuuid"], + station["countrycode"], + trim_string( + station["tags"] + ), # trimming tags to make the table shorter + str(station[sort_by]), + ) + else: + table.add_row( + str(i + 1), + trim_string(station["name"], max_length=50), + # station["stationuuid"], + station["countrycode"], + trim_string( + station["tags"] + ), # trimming tags to make the table shorter + ) console.print(table) # log.info( @@ -101,38 +116,46 @@ def station_validator(self): # return self.response[0]["name"].strip() # ---------------------------- NAME -------------------------------- # - def search_by_station_name(self, _name=None, limit=100, sort_by="clickcount"): + def search_by_station_name(self, _name=None, limit=100, sort_by: str = "name"): """search and play a station by its name""" - try: - self.response = self.API.search( - name=_name, name_exact=False, limit=limit, order=sort_by - ) - return self.station_validator() - except Exception as e: - log.debug("Error: {}".format(e)) - log.error("Something went wrong. please try again.") - sys.exit(1) + if sort_by == "name": + reversed = False + else: + reversed = True + # try: + self.response = self.API.search( + name=_name, + name_exact=False, + limit=limit, + order=str(sort_by), + reverse=reversed, + ) + return self.station_validator(sort_by) + # except Exception as e: + # log.debug("Error: {}".format(e)) + # log.error("Something went wrong. please try again.") + # sys.exit(1) # ------------------------- UUID ------------------------ # def play_by_station_uuid(self, _uuid): """search and play station by its stationuuid""" try: self.response = self.API.station_by_uuid(_uuid) - return self.station_validator() # should return a station name also + return self.station_validator("name") # should return a station name also except Exception as e: log.debug("Error: {}".format(e)) log.error("Something went wrong. please try again.") sys.exit(1) # -------------------------- COUNTRY ----------------------# - def discover_by_country(self, country_code_or_name, limit, sort_by): + def discover_by_country(self, country_code_or_name, limit, sort_by: str = "name"): # check if it is a code or name if len(country_code_or_name.strip()) == 2: # it's a code - log.debug("Country code {} provided".format(country_code_or_name)) + log.debug("Country code '{}' provided".format(country_code_or_name)) try: response = self.API.search( - countrycode=country_code_or_name, limit=limit, order=sort_by + countrycode=country_code_or_name, limit=limit, order=str(sort_by) ) except Exception as e: log.debug("Error: {}".format(e)) @@ -140,12 +163,15 @@ def discover_by_country(self, country_code_or_name, limit, sort_by): sys.exit(1) else: # it's name - log.debug("Country name {} provided".format(country_code_or_name)) + log.debug("Country name '{}' provided".format(country_code_or_name)) code = self.get_country_code(country_code_or_name) if code: try: response = self.API.search( - countrycode=code, limit=limit, country_exact=True + countrycode=code, + limit=limit, + country_exact=True, + order=str(sort_by), ) except Exception as e: log.debug("Error: {}".format(e)) @@ -188,9 +214,11 @@ def discover_by_country(self, country_code_or_name, limit, sort_by): # ------------------- by state --------------------- - def discover_by_state(self, state, limit, sort_by): + def discover_by_state(self, state, limit, sort_by: str = "name"): try: - discover_result = self.API.search(state=state, limit=limit, order=sort_by) + discover_result = self.API.search( + state=state, limit=limit, order=str(sort_by) + ) except Exception: log.error("Something went wrong. please try again.") sys.exit(1) @@ -226,10 +254,10 @@ def discover_by_state(self, state, limit, sort_by): # -----------------by language -------------------- - def discover_by_language(self, language, limit, sort_by): + def discover_by_language(self, language, limit, sort_by: str = "name"): try: discover_result = self.API.search( - language=language, limit=limit, order=sort_by + language=language, limit=limit, order=str(sort_by) ) except Exception as e: log.debug("Error: {}".format(e)) @@ -265,9 +293,9 @@ def discover_by_language(self, language, limit, sort_by): # -------------------- by tag ---------------------- # - def discover_by_tag(self, tag, limit, sort_by): + def discover_by_tag(self, tag, limit, sort_by: str = "name"): try: - discover_result = self.API.search(tag=tag, limit=limit, order=sort_by) + discover_result = self.API.search(tag=tag, limit=limit, order=str(sort_by)) except Exception as e: log.debug("Error: {}".format(e)) log.error("Something went wrong. please try again.") diff --git a/radioactive/utilities.py b/radioactive/utilities.py index a4aa5f1..98450d3 100644 --- a/radioactive/utilities.py +++ b/radioactive/utilities.py @@ -200,11 +200,13 @@ def check_sort_by_parameter(sort_by): ] if sort_by not in accepted_parameters: - log.warning("Sort parameter is unknown. Falling back to 'clickcount'") + log.warning("Sort parameter is unknown. Falling back to 'name'") log.warning( "choose from: name,votes,codec,bitrate,lastcheckok,lastchecktime,clickcount,clicktrend,random" ) + return "name" + return sort_by def handle_search_stations(handler, station_name, limit, sort_by): @@ -293,7 +295,7 @@ def handle_listen_keypress( log.info("Press '?' to see available commands\n") while True: user_input = input("Enter a command to perform an action: ") - if user_input == "r" or user_input == "R" or user_input == "record": + if user_input in ["r", "R", "record"]: handle_record( target_url, station_name, @@ -302,7 +304,7 @@ def handle_listen_keypress( record_file_format, loglevel, ) - elif user_input == "rf" or user_input == "RF" or user_input == "recordfile": + elif user_input in ["rf", "RF", "recordfile"]: # if no filename is provided try to auto detect # else if ".mp3" is provided, use libmp3lame to force write to mp3 @@ -332,22 +334,17 @@ def handle_listen_keypress( loglevel, ) - elif user_input == "f" or user_input == "F" or user_input == "fav": + elif user_input in ["f", "F", "fav"]: handle_add_to_favorite(alias, station_name, station_url) - elif user_input == "q" or user_input == "Q" or user_input == "quit": + elif user_input in ["q", "Q", "quit"]: kill_background_ffplays() sys.exit(0) - elif user_input == "w" or user_input == "W" or user_input == "list": + elif user_input in ["w", "W", "list"]: alias.generate_map() handle_favorite_table(alias) - elif ( - user_input == "h" - or user_input == "H" - or user_input == "?" - or user_input == "help" - ): + elif user_input in ["h", "H", "?", "help"]: log.info("h/help/?: Show this help message") log.info("q/quit: Quit radioactive") log.info("r/record: Record a station")