Unify station id generation

This commit is contained in:
milaq 2019-08-19 13:57:35 +02:00
parent 5205ec4e7c
commit 803964ecb4
3 changed files with 38 additions and 5 deletions

View file

@ -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:]

View file

@ -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:

View file

@ -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):