Skip to content

Commit

Permalink
feat: ✨ add: new column for sorted results
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doe committed Oct 25, 2023
1 parent ab197ff commit 724606e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 47 deletions.
2 changes: 1 addition & 1 deletion radioactive/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion radioactive/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(self):
"--sort",
action="store",
dest="stations_sort_by",
default="clickcount",
default="name",
help="Sort stations",
)

Expand Down
94 changes: 61 additions & 33 deletions radioactive/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand All @@ -101,51 +116,62 @@ 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))
log.error("Something went wrong. please try again.")
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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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.")
Expand Down
21 changes: 9 additions & 12 deletions radioactive/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 724606e

Please sign in to comment.