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.
This commit is contained in:
milaq 2019-08-22 20:38:56 +02:00
parent 7c3161aff9
commit 2349e06a2e

View file

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