text
stringlengths
1
93.6k
config = json.load(f)
return config
except Exception:
print(colors.RED + "Error loading config.json. Have you forgot the comma?" + colors.ENDC)
sys.exit()
# Outputs a nicly fomatted list of all stations.
def show_stations():
stations = load_config()["Local Stations"]
print("\nAvailable stations:\n")
for key, value in stations.items():
# Formatted print, dynamic for longest station name
print((colors.GREEN + "{0:" + str(len(max(stations, key=len))) + "}"
+ colors.ENDC + colors.YELLOW + " @ " + colors.ENDC + colors.UNDERLINE + "{1}" + colors.ENDC)
.format(key, value))
sys.exit()
# Searches local database, query TuneIn if necessary, perform connection check
def initalize_radio(station_name, database):
config = load_config()
try:
# Check config.json
station_url = config["Local Stations"][station_name]
print(colors.YELLOW + "Station found in local database." + colors.ENDC)
except KeyError:
# User may have disabled TuneIn search. If so, throw error.
if database:
print(colors.RED + "Invalid station. Use --print to list all available stations." + colors.ENDC)
sys.exit()
# This is our automatic link detection
if station_name.lower().startswith("http"):
print(colors.YELLOW + "Detected direct URL! " + colors.ENDC)
station_url = station_name
else:
print(colors.YELLOW + "Station not found in local database." + colors.ENDC)
station_data = tunein.query_data(station_name)
station_url = tunein.get_stream_link(station_data[1])
station_name = station_data[0] # Set station name as reported by TuneIn
print(colors.BOLD + "Adding station to database... " + colors.ENDC, end="")
# Add new station to local database
config["Local Stations"][station_name] = station_url
with open('config.json', 'w') as f:
json.dump(config, f, indent=4, sort_keys=True)
print(colors.GREEN + "OK!" + colors.ENDC)
# Checking if the url is reachable. If not,
# there might be a typo in the config.json file
print(colors.BOLD + "Contacting station... " + colors.ENDC, end="")
try:
# Set stream to true so we only get the headers and dont load the body
# otherwise it would hang and try to load the infinite stream
with requests.get(station_url, stream=True) as r:
# 200 is the default HTTP response code, if we get something else,
# we can not play the stream (Debug : https://httpstat.us/)
if r.status_code != 200:
print(colors.RED + "Error!" + colors.ENDC)
print(colors.RED + "Station unreachable: Site not working! Error " +
str(r.status_code) + "!" + colors.ENDC)
sys.exit()
except requests.ConnectionError:
print(colors.RED + "Error!" + colors.ENDC)
print(colors.RED + "Station unreachable: Unable to connect!" + colors.ENDC)
sys.exit()
print(colors.GREEN + "OK!" + colors.ENDC)
return [station_name, station_url]
# Main radio logic
def play_station(station_name, station_url, volume):
# TODO: MOVE THIS
config = load_config()
# VLC player magic
instance = vlc.Instance("--quiet")
# This are our player objects, which can play music.
stream_player = instance.media_player_new()
adblock_player = instance.media_player_new()
# This are our media objects, one for each player.
stream_media = instance.media_new(station_url)
adblock_media = instance.media_new(config["Replace with"])
stream_player.set_media(stream_media)
adblock_player.set_media(adblock_media)