Skip to content

Commit

Permalink
feat: discover capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
deep5050 committed May 17, 2021
1 parent a904d11 commit b742f20
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 15 deletions.
64 changes: 56 additions & 8 deletions radioactive/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -23,11 +24,12 @@


def main():
sentry_sdk.init(
"https://[email protected]/5749950",
traces_sample_rate=1.0,
debug=False,
)
# removing sentry from 2.3.0
# sentry_sdk.init(
# "https://[email protected]/5749950",
# traces_sample_rate=1.0,
# debug=False,
# )

log.level("info")

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

Expand All @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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()


Expand Down
34 changes: 31 additions & 3 deletions radioactive/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
133 changes: 129 additions & 4 deletions radioactive/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import json
from logging import exception
import sys

from pyradios import RadioBrowser
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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)

1 comment on commit b742f20

@codacy-production
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy detected an issue:

Message: 'logging.exception' imported but unused (F401)

Occurred on:

Currently on:

Please sign in to comment.