From 2349e06a2ef10c7542e7bf2d92eb2c183c6718bb Mon Sep 17 00:00:00 2001 From: milaq Date: Thu, 22 Aug 2019 20:38:56 +0200 Subject: [PATCH] Add custom station ID generation Generate IDs by hashing the station name and URL. These IDs should be sufficiently unique. We _may_ run into performance issues with a very large definition, but it should be fine for a sane amount of custom stations. Revisit once we implement a custom station database. --- ycast/my_stations.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/ycast/my_stations.py b/ycast/my_stations.py index 252443c..afae7c2 100644 --- a/ycast/my_stations.py +++ b/ycast/my_stations.py @@ -1,4 +1,5 @@ import logging +import hashlib import yaml @@ -11,8 +12,8 @@ config_file = 'my_stations.yml' class Station: - def __init__(self, name, url, category): - self.id = generic.generate_stationid_with_prefix('000000', ID_PREFIX) # TODO: generate meaningful ID + def __init__(self, uid, name, url, category): + self.id = generic.generate_stationid_with_prefix(uid, ID_PREFIX) self.name = name self.url = url self.tag = category @@ -32,7 +33,13 @@ def set_config(config): def get_station_by_id(uid): - return None # TODO: return correct station when custom station id generation is implemented + my_stations_yaml = get_stations_yaml() + if my_stations_yaml: + for category in my_stations_yaml: + for station in get_stations_by_category(category): + if uid == generic.get_stationid_without_prefix(station.id): + return station + return None def get_stations_yaml(): @@ -62,5 +69,13 @@ def get_stations_by_category(category): stations = [] if my_stations_yaml and category in my_stations_yaml: for station_name in my_stations_yaml[category]: - stations.append(Station(station_name, my_stations_yaml[category][station_name], category)) + station_url = my_stations_yaml[category][station_name] + station_id = str(get_checksum(station_name + station_url)).upper() + stations.append(Station(station_id, station_name, station_url, category)) return stations + + +def get_checksum(feed): + hash_feed = feed.encode() + hash_object = hashlib.sha256(hash_feed) + return hash_object.hexdigest()