diff --git a/addon.xml b/addon.xml index b16d267..f7a7108 100644 --- a/addon.xml +++ b/addon.xml @@ -1,4 +1,4 @@ - + diff --git a/resources/lib/routes.py b/resources/lib/routes.py index a546695..2c3134d 100644 --- a/resources/lib/routes.py +++ b/resources/lib/routes.py @@ -52,45 +52,7 @@ def get_stations(addon_handle, kind, page, orderby, reverse): station_list = [] for station in response: - votes = [f"[B]{station['votes']} votes[/B]"] - # TODO: localize language and location. pycountry maybe? - language = station["language"].split(",") - language = [i.title() for i in language] - - location = [station["state"], station["country"]] - tags = station["tags"].split(",") - - cleaned_tags = [i for i in votes + language + location + tags if i] - genre = ", ".join(cleaned_tags) - - if station["lastcheckok"] == 0: - genre = "[B]Offline![/B] " + genre - - li = xbmcgui.ListItem(station["name"], genre) - - li.setInfo( - "music", - { - "title": station["name"], - "tracknumber": (page * 50) + len(station_list) + 1, - "size": station["bitrate"], - "genre": genre, - }, - ) - li.setArt( - { - "thumb": station["favicon"], - "poster": station["favicon"], - "fanart": station["favicon"], - "landscape": station["favicon"], - "icon": station["favicon"], - } - ) - li.setProperty("IsPlayable", "true") - url = utils.build_url( - {"mode": "listen", "url": station["url_resolved"], "uuid": station["stationuuid"]} - ) - station_list.append((url, li, False)) + station_list.append(utils.station_item(station, (page * 50) + len(station_list) + 1)) station_list.append( utils.next_page_item(response, "stations", page, {"kind": kind}) @@ -149,223 +111,13 @@ def open_search_directory(addon_handle): def open_stations_sort_directory(addon_handle, kind): - menu_list = [] - - li = xbmcgui.ListItem("Most Voted First") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "votes", "reverse": "true"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Least Voted First") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "votes", "reverse": "false"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Most Listeners First") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "clickcount", "reverse": "true"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Least Listeners First") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "clickcount", "reverse": "false"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Name (A-Z)") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "name", "reverse": "false"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Name (Z-A)") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "name", "reverse": "true"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Bitrate (highest first)") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "bitrate", "reverse": "true"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Bitrate (lowest and undefined first)") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "bitrate", "reverse": "false"} - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Recently Changed (oldest first)") - url = utils.build_url( - { - "mode": "stations", - "kind": kind, - "orderby": "changetimestamp", - "reverse": "false", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Recently Changed (newest first)") - url = utils.build_url( - { - "mode": "stations", - "kind": kind, - "orderby": "changetimestamp", - "reverse": "true", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Random") - url = utils.build_url( - {"mode": "stations", "kind": kind, "orderby": "random", "reverse": "false"} - ) - menu_list.append((url, li, True)) - + menu_list = utils.sort_menu("stations", {"kind": kind}) xbmcplugin.addDirectoryItems(addon_handle, menu_list) xbmcplugin.endOfDirectory(addon_handle) def open_search_sort_directory(addon_handle, kind, search_text): - menu_list = [] - - li = xbmcgui.ListItem("Most Voted First") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "votes", - "reverse": "true", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Least Voted First") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "votes", - "reverse": "false", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Most Listeners First") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "clickcount", - "reverse": "true", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Least Listeners First") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "clickcount", - "reverse": "false", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Name (A-Z)") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "name", - "reverse": "false", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Name (Z-A)") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "name", - "reverse": "true", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Bitrate (highest first)") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "bitrate", - "reverse": "true", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Bitrate (lowest and undefined first)") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "bitrate", - "reverse": "false", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Recently Changed (oldest first)") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "changetimestamp", - "reverse": "false", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Sort by Recently Changed (newest first)") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "changetimestamp", - "reverse": "true", - } - ) - menu_list.append((url, li, True)) - - li = xbmcgui.ListItem("Random") - url = utils.build_url( - { - "mode": "results", - "kind": kind, - "search_text": search_text, - "orderby": "random", - "reverse": "false", - } - ) - menu_list.append((url, li, True)) - + menu_list = utils.sort_menu("results", {"kind": kind, "search_text": search_text}) xbmcplugin.addDirectoryItems(addon_handle, menu_list) xbmcplugin.endOfDirectory(addon_handle) @@ -590,46 +342,7 @@ def perform_search(addon_handle, kind, search_text, orderby, reverse, page): results_list = [] for station in response: - votes = [f"[B]{station['votes']} votes[/B]"] - # TODO: localize language and location. pycountry maybe? - language = station["language"].split(",") - language = [i.title() for i in language] - - location = [station["state"], station["country"]] - tags = station["tags"].split(",") - - cleaned_tags = [i for i in votes + language + location + tags if i] - genre = ", ".join(cleaned_tags) - - if station["lastcheckok"] == 0: - genre = "[B]Offline![/B] " + genre - - li = xbmcgui.ListItem(station["name"], genre) - - li.setInfo( - "music", - { - "title": station["name"], - "tracknumber": (page * 50) + len(results_list) + 1, - "size": station["bitrate"], - "genre": genre, - "playcount": station["clickcount"], - }, - ) - li.setArt( - { - "thumb": station["favicon"], - "poster": station["favicon"], - "fanart": station["favicon"], - "landscape": station["favicon"], - "icon": station["favicon"], - } - ) - li.setProperty("IsPlayable", "true") - url = utils.build_url( - {"mode": "listen", "url": station["url_resolved"], "uuid": station["stationuuid"]} - ) - results_list.append((url, li, False)) + results_list.append(utils.station_item(station, (page * 50) + len(results_list) + 1)) results_list.append( utils.next_page_item( diff --git a/resources/lib/server.py b/resources/lib/server.py index 209f8b4..3136d95 100644 --- a/resources/lib/server.py +++ b/resources/lib/server.py @@ -1,9 +1,8 @@ import socket import random import requests -import xbmcgui -headers = {"User-Agent": "RadioBrowser2/0.6.0"} +headers = {"User-Agent": "RadioBrowser2/0.6.1"} server_url = "" diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 5fa3896..36c259f 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -17,3 +17,165 @@ def next_page_item(response, mode, current_page, params={}): query.update(params) url = build_url(query) return (url, li, True) + + +def station_item(station, number): + votes = [f"[B]{station['votes']} votes[/B]"] + # TODO: localize language and location. pycountry maybe? + language = station["language"].split(",") + language = [i.title() for i in language] + + location = [station["state"], station["country"]] + tags = station["tags"].split(",") + + cleaned_tags = [i for i in votes + language + location + tags if i] + genre = ", ".join(cleaned_tags) + + if station["lastcheckok"] == 0: + genre = "[B]Offline![/B] " + genre + + li = xbmcgui.ListItem(station["name"], genre) + + li.setInfo( + "music", + { + "title": station["name"], + "tracknumber": number, + "size": station["bitrate"], + "genre": genre, + }, + ) + li.setArt( + { + "thumb": station["favicon"], + "poster": station["favicon"], + "fanart": station["favicon"], + "landscape": station["favicon"], + "icon": station["favicon"], + } + ) + li.setProperty("IsPlayable", "true") + url = build_url( + { + "mode": "listen", + "url": station["url_resolved"], + "uuid": station["stationuuid"], + } + ) + return (url, li, False) + + +def sort_menu(mode, params={}): + menu_list = [] + + li = xbmcgui.ListItem("Most Voted First") + query = { + "mode": mode, + "orderby": "votes", + "reverse": "true", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Least Voted First") + query = { + "mode": mode, + "orderby": "votes", + "reverse": "false", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Most Listeners First") + query = { + "mode": mode, + "orderby": "clickcount", + "reverse": "true", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Least Listeners First") + query = { + "mode": mode, + "orderby": "clickcount", + "reverse": "false", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("A-Z") + query = { + "mode": mode, + "orderby": "name", + "reverse": "false", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Z-A") + query = { + "mode": mode, + "orderby": "name", + "reverse": "true", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Highest Bitrate First") + query = { + "mode": mode, + "orderby": "bitrate", + "reverse": "true", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Lowest/Undefined Bitrate First") + query = { + "mode": mode, + "orderby": "bitrate", + "reverse": "false", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Oldest Change First") + query = { + "mode": mode, + "orderby": "changetimestamp", + "reverse": "false", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Newest Change First") + query = { + "mode": mode, + "orderby": "changetimestamp", + "reverse": "true", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + li = xbmcgui.ListItem("Random") + query = { + "mode": mode, + "orderby": "random", + "reverse": "false", + } + query.update(params) + url = build_url(query) + menu_list.append((url, li, True)) + + return menu_list