From b742f20f55787453c4e8ef194d07f1885bc69ce3 Mon Sep 17 00:00:00 2001 From: Dipankar Pal Date: Mon, 17 May 2021 11:35:00 +0530 Subject: [PATCH] feat: discover capabilities --- radioactive/__main__.py | 64 ++++++++++++++++--- radioactive/args.py | 34 +++++++++- radioactive/handler.py | 133 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 216 insertions(+), 15 deletions(-) diff --git a/radioactive/__main__.py b/radioactive/__main__.py index ca02edf..c00bd53 100755 --- a/radioactive/__main__.py +++ b/radioactive/__main__.py @@ -2,10 +2,11 @@ import signal import sys -import sentry_sdk +# import sentry_sdk from rich import print from rich.console import Console from rich.panel import Panel +from rich.text import Text from rich.table import Table from zenlog import log @@ -23,11 +24,12 @@ def main(): - sentry_sdk.init( - "https://e3c430f3b03f49b6bd9e9d61e7b3dc37@o615507.ingest.sentry.io/5749950", - traces_sample_rate=1.0, - debug=False, - ) + # removing sentry from 2.3.0 + # sentry_sdk.init( + # "https://e3c430f3b03f49b6bd9e9d61e7b3dc37@o615507.ingest.sentry.io/5749950", + # traces_sample_rate=1.0, + # debug=False, + # ) log.level("info") @@ -40,7 +42,13 @@ def main(): station_name = args.station_name station_uuid = args.station_uuid log_level = args.log_level - discover = args.discover + + discover_country_code = args.discover_country_code + discover_state = args.discover_state + discover_language = args.discover_language + discover_tag = args.discover_tag + + limit = args.limit add_station = args.new_station add_to_favourite = args.add_to_favourite show_favourite_list = args.show_favourite_list @@ -129,6 +137,36 @@ def main(): log.info("New entry: {}={} added\n".format(left, right)) sys.exit(0) + +# ------------------ discover ------------------ # + _limit = int(limit) if limit else 100 + + if discover_country_code: + # search for stations in your country + handler.discover_by_country(discover_country_code,_limit) + + if discover_state: + handler.discover_by_state(discover_state,_limit) + + if discover_language: + handler.discover_by_language(discover_language,_limit) + + if discover_tag: + handler.discover_by_tag(discover_tag,_limit) + + + + + + + + +# -------------------------------------------------- # + + + + + # -------------------- NOTHING PROVIDED --------------------- # # if neither of --station and --uuid provided , look in last_station file @@ -149,6 +187,7 @@ def main(): # last station was an alias, don't save it again skip_saving_current_station = True station_uuid_or_url = last_station_info["uuid_or_url"] + station_name = last_station_info['name'] # here we are setting the name but will not be used for API call if station_uuid_or_url.find("://") != -1: # Its a URL log.debug( @@ -235,7 +274,10 @@ def main(): if mode_of_search == "uuid": handler.play_by_station_uuid(station_uuid) else: - handler.play_by_station_name(station_name) + if not alias.found: + # when alias was found, we have set the station name to print it correctly, + # not to do an API call + handler.play_by_station_name(station_name) global player @@ -260,6 +302,12 @@ def main(): if add_to_favourite: alias.add_entry(add_to_favourite, handler.target_station["url"]) + + curr_station_name = "\n" + station_name if alias.found else handler.target_station['name'] + "/n" + panel_station_name = Text(curr_station_name,justify="center") + + station_panel = Panel(panel_station_name,title="[blink]:radio:[/blink]",width=85) + console.print(station_panel) signal.pause() diff --git a/radioactive/args.py b/radioactive/args.py index 7032036..595c207 100644 --- a/radioactive/args.py +++ b/radioactive/args.py @@ -55,11 +55,39 @@ def __init__(self): ) self.parser.add_argument( - "--discover", + "--discover-by-country", "-D", action="store", - dest="discover", - help="Discover stations in your country", + dest="discover_country_code", + help="Discover stations with country code", + ) + + self.parser.add_argument( + "--discover-by-tag", + action="store", + dest="discover_tag", + help="Discover stations with tag", + ) + + self.parser.add_argument( + "--discover-by-state", + action="store", + dest="discover_state", + help="Discover stations with state name", + ) + + self.parser.add_argument( + "--discover-by-language", + action="store", + dest="discover_language", + help="Discover stations with state name", + ) + self.parser.add_argument( + "--limit", + action="store", + dest="limit", + default=100, + help="Limit of entries in discover table", ) self.parser.add_argument( diff --git a/radioactive/handler.py b/radioactive/handler.py index f7e736f..ceb5806 100644 --- a/radioactive/handler.py +++ b/radioactive/handler.py @@ -3,6 +3,7 @@ """ import json +from logging import exception import sys from pyradios import RadioBrowser @@ -43,8 +44,10 @@ def station_validator(self): if len(self.response) > 1: table = Table(show_header=True, header_style="bold magenta") table.add_column("Station", justify="left") - table.add_column("Country", justify="center") table.add_column("UUID", justify="center") + table.add_column("Country", justify="center") + table.add_column("Tags", justify="center") + log.warn( "{} stations found by the name, select one and run with UUID instead".format( @@ -59,10 +62,12 @@ def station_validator(self): # data["country"] = station["country"] # log.info(json.dumps(data, indent=3)) table.add_row( - station["name"], station["countrycode"], station["stationuuid"] + station["name"], station["stationuuid"],station["countrycode"],station['tags'] ) console.print(table) + log.info("If the table does not fit into your screen, \ntry to maximize the window , decrease the font by a bit and retry") + sys.exit(1) # when exactly one response found @@ -79,10 +84,130 @@ def play_by_station_name(self, _name=None): self.response = self.API.search(name=_name, name_exact=False) self.station_validator() + +# ------------------ by uuid --------------- + def play_by_station_uuid(self, _uuid): """search and play station by its stationuuid""" self.response = self.API.station_by_uuid(_uuid) self.station_validator() - def discover_by_country(self, country_code): - pass + def discover_by_country(self, _country_code, _limit): + try: + discover_result = self.API.search(countrycode=_country_code, limit=_limit) + except Exception as e: + # print(e) + log.error("Something went wrong") + sys.exit(1) + + if len(discover_result) > 1: + log.info("Result for country: {}".format(discover_result[0]["country"])) + table = Table(show_header=True, header_style="bold magenta") + table.add_column("Station", justify="left") + table.add_column("UUID", justify="center") + table.add_column("State", justify="center") + table.add_column("Tags", justify="center") + table.add_column("Language", justify="center") + + for res in discover_result: + table.add_row( + res["name"], res["stationuuid"], res["state"],res["tags"],res["language"] + ) + console.print(table) + log.info("If the table does not fit into your screen, \ntry to maximize the window , decrease the font by a bit and retry") + + sys.exit(0) + else: + log.error("No stations found for the country code, recheck it") + sys.exit(1) + +#------------------- by state --------------------- + + def discover_by_state(self, _state, _limit): + try: + discover_result = self.API.search(state=_state, limit=_limit) + except Exception as e: + # print(e) + log.error("Something went wrong") + sys.exit(1) + + if len(discover_result) > 1: + table = Table(show_header=True, header_style="bold magenta") + table.add_column("Station", justify="left") + table.add_column("UUID", justify="center") + table.add_column("Country", justify="center") + table.add_column("Tags", justify="center") + table.add_column("Language", justify="center") + + for res in discover_result: + table.add_row( + res["name"], res["stationuuid"], res["country"],res["tags"],res["language"] + ) + console.print(table) + log.info("If the table does not fit into your screen, \ntry to maximize the window , decrease the font by a bit and retry") + + sys.exit(0) + else: + log.error("No stations found for the state, recheck it") + sys.exit(1) + +# -----------------by language -------------------- + + def discover_by_language(self, _language, _limit): + try: + discover_result = self.API.search(language=_language, limit=_limit) + except Exception as e: + # print(e) + log.error("Something went wrong") + sys.exit(1) + + if len(discover_result) > 1: + table = Table(show_header=True, header_style="bold magenta") + table.add_column("Station", justify="left") + table.add_column("UUID", justify="center") + table.add_column("Country", justify="center") + table.add_column("Tags", justify="center") + + for res in discover_result: + table.add_row( + res["name"], res["stationuuid"], res["country"],res["tags"] + ) + console.print(table) + log.info("If the table does not fit into your screen, \ntry to maximize the window , decrease the font by a bit and retry") + + sys.exit(0) + else: + log.error("No stations found for the language, recheck it") + sys.exit(1) + + + + +# -------------------- by tag ---------------------- + + def discover_by_tag(self, _tag, _limit): + try: + discover_result = self.API.search(tag=_tag, limit=_limit) + except Exception as e: + # print(e) + log.error("Something went wrong") + sys.exit(1) + + if len(discover_result) > 1: + table = Table(show_header=True, header_style="bold magenta") + table.add_column("Station", justify="left") + table.add_column("UUID", justify="center") + table.add_column("country", justify="center") + table.add_column("Language", justify="center") + + for res in discover_result: + table.add_row( + res["name"], res["stationuuid"],res["country"],res["language"] + ) + console.print(table) + log.info("If the table does not fit into your screen, \ntry to maximize the window , decrease the font by a bit and retry") + + sys.exit(0) + else: + log.error("No stations found for the tag, recheck it") + sys.exit(1) \ No newline at end of file