Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 44 additions & 83 deletions ycast/radiobrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import ycast.generic as generic

API_ENDPOINT = "http://all.api.radio-browser.info"
MINIMUM_COUNT_GENRE = 5
MINIMUM_COUNT_COUNTRY = 5
MINIMUM_COUNT_LANGUAGE = 5
DEFAULT_STATION_LIMIT = 200
SHOW_BROKEN_STATIONS = False
ID_PREFIX = "RB"
Expand All @@ -21,6 +18,31 @@ def get_json_attr(json, attr):
return None


def format_displayname(displayname, apiparam):
if apiparam == 'languages':
displayname = displayname.title()
elif apiparam == 'tags':
displayname = displayname.capitalize()
else:
displayname = None
return displayname


def request_station_builder(paramtype, param, limit):
prefix = 'stations/search?order='
order = 'name&reverse=false'
lim = '&limit=' + str(limit)
req = '&' + str(paramtype) + 'Exact=true&' + str(paramtype) + '=' + str(param)
hidebroken = '&hidebroken=true'
if SHOW_BROKEN_STATIONS:
hidebroken = '&hidebroken=false'
if paramtype == 'votes':
order = 'votes&reverse=true'
if paramtype == 'search':
req = '&name=' + str(param)
return prefix + order + lim + req + hidebroken


class Station:
def __init__(self, station_json):
self.id = generic.generate_stationid_with_prefix(get_json_attr(station_json, 'stationuuid'), ID_PREFIX)
Expand Down Expand Up @@ -68,90 +90,29 @@ def get_station_by_id(uid):
return None


def search(name, limit=DEFAULT_STATION_LIMIT):
def get_stations(paramtype, param='', limit=DEFAULT_STATION_LIMIT):
"""
Generic Function for getting Stations. paramtype must be one
of search, country, language, tag(getting genres),votes. See
request_station_builder(paramtype, param, limit) to expand functionality
"""
stations = []
stations_json = request('stations/search?order=name&reverse=false&limit=' + str(limit) + '&name=' + str(name))
stations_json = request(request_station_builder(paramtype, param, limit))
for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json))
stations.append(Station(station_json))
return stations


def get_country_directories():
country_directories = []
apicall = 'countries'
if not SHOW_BROKEN_STATIONS:
apicall += '?hidebroken=true'
countries_raw = request(apicall)
for country_raw in countries_raw:
if get_json_attr(country_raw, 'name') and get_json_attr(country_raw, 'stationcount') and \
int(get_json_attr(country_raw, 'stationcount')) > MINIMUM_COUNT_COUNTRY:
country_directories.append(generic.Directory(get_json_attr(country_raw, 'name'),
get_json_attr(country_raw, 'stationcount')))
return country_directories


def get_language_directories():
language_directories = []
apicall = 'languages'
def get_directories(apiparam, minimumcount=5):
apicall = apiparam
directories = []
if not SHOW_BROKEN_STATIONS:
apicall += '?hidebroken=true'
languages_raw = request(apicall)
for language_raw in languages_raw:
if get_json_attr(language_raw, 'name') and get_json_attr(language_raw, 'stationcount') and \
int(get_json_attr(language_raw, 'stationcount')) > MINIMUM_COUNT_LANGUAGE:
language_directories.append(generic.Directory(get_json_attr(language_raw, 'name'),
get_json_attr(language_raw, 'stationcount'),
get_json_attr(language_raw, 'name').title()))
return language_directories


def get_genre_directories():
genre_directories = []
apicall = 'tags'
if not SHOW_BROKEN_STATIONS:
apicall += '?hidebroken=true'
genres_raw = request(apicall)
for genre_raw in genres_raw:
if get_json_attr(genre_raw, 'name') and get_json_attr(genre_raw, 'stationcount') and \
int(get_json_attr(genre_raw, 'stationcount')) > MINIMUM_COUNT_GENRE:
genre_directories.append(generic.Directory(get_json_attr(genre_raw, 'name'),
get_json_attr(genre_raw, 'stationcount'),
get_json_attr(genre_raw, 'name').capitalize()))
return genre_directories


def get_stations_by_country(country):
stations = []
stations_json = request('stations/search?order=name&reverse=false&countryExact=true&country=' + str(country))
for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json))
return stations


def get_stations_by_language(language):
stations = []
stations_json = request('stations/search?order=name&reverse=false&languageExact=true&language=' + str(language))
for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json))
return stations


def get_stations_by_genre(genre):
stations = []
stations_json = request('stations/search?order=name&reverse=false&tagExact=true&tag=' + str(genre))
for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json))
return stations


def get_stations_by_votes(limit=DEFAULT_STATION_LIMIT):
stations = []
stations_json = request('stations?order=votes&reverse=true&limit=' + str(limit))
for station_json in stations_json:
if SHOW_BROKEN_STATIONS or get_json_attr(station_json, 'lastcheckok') == 1:
stations.append(Station(station_json))
return stations
raw_directories = request(apicall)
for raw_directory in raw_directories:
if get_json_attr(raw_directory, 'name') and get_json_attr(raw_directory, 'stationcount') and \
int(get_json_attr(raw_directory, 'stationcount')) > minimumcount:
directories.append(generic.Directory(get_json_attr(raw_directory, 'name'),
get_json_attr(raw_directory, 'stationcount'),
format_displayname(get_json_attr(raw_directory, 'name'), apiparam)))
return directories
28 changes: 16 additions & 12 deletions ycast/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
PATH_RADIOBROWSER_GENRE = 'genre'
PATH_RADIOBROWSER_POPULAR = 'popular'

MINIMUM_COUNT_GENRE = 5
MINIMUM_COUNT_COUNTRY = 5
MINIMUM_COUNT_LANGUAGE = 5

station_tracking = False
my_stations_enabled = False
app = Flask(__name__)
Expand Down Expand Up @@ -174,63 +178,63 @@ def my_stations_category(directory):
def radiobrowser_landing():
page = vtuner.Page()
page.add(vtuner.Directory('Genres', url_for('radiobrowser_genres', _external=True),
len(radiobrowser.get_genre_directories())))
len(radiobrowser.get_directories('tags', MINIMUM_COUNT_GENRE))))
page.add(vtuner.Directory('Countries', url_for('radiobrowser_countries', _external=True),
len(radiobrowser.get_country_directories())))
len(radiobrowser.get_directories('countries', MINIMUM_COUNT_COUNTRY))))
page.add(vtuner.Directory('Languages', url_for('radiobrowser_languages', _external=True),
len(radiobrowser.get_language_directories())))
len(radiobrowser.get_directories('languages', MINIMUM_COUNT_LANGUAGE))))
page.add(vtuner.Directory('Most Popular', url_for('radiobrowser_popular', _external=True),
len(radiobrowser.get_stations_by_votes())))
len(radiobrowser.get_stations('votes'))))
page.set_count(4)
return page.to_string()


@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/',
methods=['GET', 'POST'])
def radiobrowser_countries():
directories = radiobrowser.get_country_directories()
directories = radiobrowser.get_directories('countries', MINIMUM_COUNT_COUNTRY)
return get_directories_page('radiobrowser_country_stations', directories, request).to_string()


@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/<directory>',
methods=['GET', 'POST'])
def radiobrowser_country_stations(directory):
stations = radiobrowser.get_stations_by_country(directory)
stations = radiobrowser.get_stations('country', directory)
return get_stations_page(stations, request).to_string()


@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_LANGUAGE + '/',
methods=['GET', 'POST'])
def radiobrowser_languages():
directories = radiobrowser.get_language_directories()
directories = radiobrowser.get_directories('languages', MINIMUM_COUNT_LANGUAGE)
return get_directories_page('radiobrowser_language_stations', directories, request).to_string()


@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_LANGUAGE + '/<directory>',
methods=['GET', 'POST'])
def radiobrowser_language_stations(directory):
stations = radiobrowser.get_stations_by_language(directory)
stations = radiobrowser.get_stations('language', directory)
return get_stations_page(stations, request).to_string()


@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/',
methods=['GET', 'POST'])
def radiobrowser_genres():
directories = radiobrowser.get_genre_directories()
directories = radiobrowser.get_directories('tags', MINIMUM_COUNT_GENRE)
return get_directories_page('radiobrowser_genre_stations', directories, request).to_string()


@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/<directory>',
methods=['GET', 'POST'])
def radiobrowser_genre_stations(directory):
stations = radiobrowser.get_stations_by_genre(directory)
stations = radiobrowser.get_stations('tag', directory)
return get_stations_page(stations, request).to_string()


@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_POPULAR + '/',
methods=['GET', 'POST'])
def radiobrowser_popular():
stations = radiobrowser.get_stations_by_votes()
stations = radiobrowser.get_stations('votes')
return get_stations_page(stations, request).to_string()


Expand All @@ -245,7 +249,7 @@ def station_search():
return page.to_string()
else:
# TODO: we also need to include 'my station' elements
stations = radiobrowser.search(query)
stations = radiobrowser.get_stations('search', query)
return get_stations_page(stations, request).to_string()


Expand Down