2019-08-14 14:01:30 +00:00
|
|
|
import logging
|
2019-08-22 18:38:56 +00:00
|
|
|
import hashlib
|
2019-08-14 14:01:30 +00:00
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
import ycast.vtuner as vtuner
|
2019-08-18 11:50:38 +00:00
|
|
|
import ycast.generic as generic
|
2019-08-14 14:01:30 +00:00
|
|
|
|
2019-08-19 11:57:35 +00:00
|
|
|
ID_PREFIX = "MY"
|
2019-08-14 14:01:30 +00:00
|
|
|
|
2019-08-22 18:40:49 +00:00
|
|
|
config_file = 'stations.yml'
|
2019-08-14 14:01:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Station:
|
2019-08-22 18:38:56 +00:00
|
|
|
def __init__(self, uid, name, url, category):
|
|
|
|
self.id = generic.generate_stationid_with_prefix(uid, ID_PREFIX)
|
2019-08-14 14:01:30 +00:00
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
self.tag = category
|
2019-08-25 15:02:41 +00:00
|
|
|
self.icon = None
|
2019-08-14 14:01:30 +00:00
|
|
|
|
|
|
|
def to_vtuner(self):
|
2019-08-25 15:02:41 +00:00
|
|
|
return vtuner.Station(self.id, self.name, self.tag, self.url, self.icon, self.tag, None, None, None, None)
|
2019-08-14 14:01:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_config(config):
|
|
|
|
global config_file
|
|
|
|
if config:
|
|
|
|
config_file = config
|
2019-08-18 11:50:38 +00:00
|
|
|
if get_stations_yaml():
|
2019-08-14 14:01:30 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-08-19 11:57:35 +00:00
|
|
|
def get_station_by_id(uid):
|
2019-08-22 18:38:56 +00:00
|
|
|
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
|
2019-08-19 11:57:35 +00:00
|
|
|
|
|
|
|
|
2019-08-18 11:50:38 +00:00
|
|
|
def get_stations_yaml():
|
2019-08-14 14:01:30 +00:00
|
|
|
try:
|
|
|
|
with open(config_file, 'r') as f:
|
|
|
|
my_stations = yaml.safe_load(f)
|
|
|
|
except FileNotFoundError:
|
|
|
|
logging.error("Station configuration '%s' not found", config_file)
|
|
|
|
return None
|
|
|
|
except yaml.YAMLError as e:
|
|
|
|
logging.error("Station configuration format error: %s", e)
|
|
|
|
return None
|
|
|
|
return my_stations
|
|
|
|
|
|
|
|
|
2019-08-18 11:50:38 +00:00
|
|
|
def get_category_directories():
|
|
|
|
my_stations_yaml = get_stations_yaml()
|
2019-08-14 14:01:30 +00:00
|
|
|
categories = []
|
|
|
|
if my_stations_yaml:
|
|
|
|
for category in my_stations_yaml:
|
2019-08-18 11:50:38 +00:00
|
|
|
categories.append(generic.Directory(category, len(get_stations_by_category(category))))
|
2019-08-14 14:01:30 +00:00
|
|
|
return categories
|
|
|
|
|
|
|
|
|
|
|
|
def get_stations_by_category(category):
|
2019-08-18 11:50:38 +00:00
|
|
|
my_stations_yaml = get_stations_yaml()
|
2019-08-14 14:01:30 +00:00
|
|
|
stations = []
|
|
|
|
if my_stations_yaml and category in my_stations_yaml:
|
|
|
|
for station_name in my_stations_yaml[category]:
|
2019-08-22 18:38:56 +00:00
|
|
|
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))
|
2019-08-14 14:01:30 +00:00
|
|
|
return stations
|
2019-08-22 18:38:56 +00:00
|
|
|
|
|
|
|
|
2019-08-25 11:42:31 +00:00
|
|
|
def get_checksum(feed, charlimit=12):
|
2019-08-22 18:38:56 +00:00
|
|
|
hash_feed = feed.encode()
|
2019-08-25 11:42:31 +00:00
|
|
|
hash_object = hashlib.md5(hash_feed)
|
|
|
|
digest = hash_object.digest()
|
|
|
|
xor_fold = bytearray(digest[:8])
|
|
|
|
for i, b in enumerate(digest[8:]):
|
|
|
|
xor_fold[i] ^= b
|
|
|
|
digest_xor_fold = ''.join(format(x, '02x') for x in bytes(xor_fold))
|
|
|
|
return digest_xor_fold[:charlimit]
|