From 803964ecb40aa2b0e3e374825679c8ec26da0960 Mon Sep 17 00:00:00 2001 From: milaq Date: Mon, 19 Aug 2019 13:57:35 +0200 Subject: [PATCH] Unify station id generation --- ycast/generic.py | 26 ++++++++++++++++++++++++++ ycast/my_stations.py | 8 ++++++-- ycast/radiobrowser.py | 9 ++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ycast/generic.py b/ycast/generic.py index cce80ab..11c1f2d 100644 --- a/ycast/generic.py +++ b/ycast/generic.py @@ -1,3 +1,5 @@ +import logging + USER_AGENT = 'YCast' @@ -5,3 +7,27 @@ class Directory: def __init__(self, name, item_count): self.name = name self.item_count = item_count + + +def generate_stationid_with_prefix(uid, prefix): + if not prefix or len(prefix) != 2: + logging.error("Invalid station prefix length (must be 2)") + return None + if not uid: + logging.error("Missing station id for full station id generation") + return None + return str(prefix) + '_' + str(uid) + + +def get_stationid_prefix(uid): + if len(uid) < 4: + logging.error("Could not extract stationid (Invalid station id length)") + return None + return uid[:2] + + +def get_stationid_without_prefix(uid): + if len(uid) < 4: + logging.error("Could not extract stationid (Invalid station id length)") + return None + return uid[3:] diff --git a/ycast/my_stations.py b/ycast/my_stations.py index 4f30ec5..252443c 100644 --- a/ycast/my_stations.py +++ b/ycast/my_stations.py @@ -5,14 +5,14 @@ import yaml import ycast.vtuner as vtuner import ycast.generic as generic -ID_PREFIX = "MY_" +ID_PREFIX = "MY" config_file = 'my_stations.yml' class Station: def __init__(self, name, url, category): - self.id = ID_PREFIX + '000000' # TODO: generate meaningful ID + self.id = generic.generate_stationid_with_prefix('000000', ID_PREFIX) # TODO: generate meaningful ID self.name = name self.url = url self.tag = category @@ -31,6 +31,10 @@ def set_config(config): return False +def get_station_by_id(uid): + return None # TODO: return correct station when custom station id generation is implemented + + def get_stations_yaml(): try: with open(config_file, 'r') as f: diff --git a/ycast/radiobrowser.py b/ycast/radiobrowser.py index 5359c68..04d1d45 100644 --- a/ycast/radiobrowser.py +++ b/ycast/radiobrowser.py @@ -9,7 +9,7 @@ MINIMUM_COUNT_GENRE = 5 MINIMUM_COUNT_COUNTRY = 5 DEFAULT_STATION_LIMIT = 200 SHOW_BROKEN_STATIONS = False -ID_PREFIX = "RB_" +ID_PREFIX = "RB" def get_json_attr(json, attr): @@ -21,7 +21,7 @@ def get_json_attr(json, attr): class Station: def __init__(self, station_json): - self.id = ID_PREFIX + get_json_attr(station_json, 'id') + self.id = generic.generate_stationid_with_prefix(get_json_attr(station_json, 'id'), ID_PREFIX) self.name = get_json_attr(station_json, 'name') self.url = get_json_attr(station_json, 'url') self.icon = get_json_attr(station_json, 'favicon') @@ -49,7 +49,10 @@ def request(url): def get_station_by_id(uid): station_json = request('stations/byid/' + str(uid)) - return Station(station_json[0]) + if station_json and len(station_json): + return Station(station_json[0]) + else: + return None def search(name, limit=DEFAULT_STATION_LIMIT):