refactor
This commit is contained in:
parent
36b7f69539
commit
a490e13b99
5 changed files with 23 additions and 23 deletions
|
@ -34,13 +34,6 @@ def get_stationid_prefix(uid):
|
|||
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:]
|
||||
|
||||
|
||||
def get_cache_path(cache_name):
|
||||
cache_path = CACHE_PATH + '/' + cache_name
|
||||
try:
|
||||
|
@ -52,6 +45,7 @@ def get_cache_path(cache_name):
|
|||
return None
|
||||
return cache_path
|
||||
|
||||
|
||||
def get_checksum(feed, charlimit=12):
|
||||
hash_feed = feed.encode()
|
||||
hash_object = hashlib.md5(hash_feed)
|
||||
|
@ -60,4 +54,4 @@ def get_checksum(feed, charlimit=12):
|
|||
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]
|
||||
return str(digest_xor_fold[:charlimit]).upper()
|
||||
|
|
|
@ -8,28 +8,32 @@ MAX_ENTRIES = 15
|
|||
|
||||
config_file = VAR_PATH + '/recently.yml'
|
||||
|
||||
def signalStationSelected(name,url,icon):
|
||||
logging.debug(" %s:%s|%s",name,url,icon)
|
||||
|
||||
def signal_station_selected(name, url, icon):
|
||||
logging.debug(" %s:%s|%s", name, url, icon)
|
||||
list_heared_stations = get_stations_list()
|
||||
if len(list_heared_stations) == 0 :
|
||||
if len(list_heared_stations) == 0:
|
||||
list_heared_stations.append("recently used:\n")
|
||||
# make name yaml - like
|
||||
name = name.replace(":", " -")
|
||||
|
||||
for line in list_heared_stations:
|
||||
elements = line.split(':')
|
||||
elements = line.split(': ')
|
||||
if elements[0] == ' '+name:
|
||||
list_heared_stations.remove(line)
|
||||
logging.debug("Name '%s' exists and deleted",name)
|
||||
logging.debug("Name '%s' exists and deleted", name)
|
||||
piped_icon = ''
|
||||
if icon and len(icon) > 0:
|
||||
piped_icon = '|' + icon
|
||||
|
||||
list_heared_stations.insert(1,' '+name+': '+url+piped_icon+'\n')
|
||||
list_heared_stations.insert(1, ' '+name+': '+url+piped_icon+'\n')
|
||||
if len(list_heared_stations) > MAX_ENTRIES+1:
|
||||
# remove last (oldest) entry
|
||||
list_heared_stations.pop()
|
||||
|
||||
set_stations_yaml(list_heared_stations)
|
||||
|
||||
|
||||
def set_stations_yaml(heared_stations):
|
||||
try:
|
||||
os.makedirs(VAR_PATH)
|
||||
|
@ -47,6 +51,7 @@ def set_stations_yaml(heared_stations):
|
|||
except Exception as ex:
|
||||
logging.error("File not written '%s': %s", config_file, ex)
|
||||
|
||||
|
||||
def get_stations_list():
|
||||
try:
|
||||
with open(config_file, 'r') as f:
|
||||
|
@ -59,6 +64,7 @@ def get_stations_list():
|
|||
return []
|
||||
return heared_stations
|
||||
|
||||
|
||||
def get_recently_stations_yaml():
|
||||
try:
|
||||
with open(config_file, 'r') as f:
|
||||
|
|
|
@ -31,12 +31,12 @@ def set_config(config):
|
|||
return False
|
||||
|
||||
|
||||
def get_station_by_id(uid):
|
||||
def get_station_by_id(vtune_id):
|
||||
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):
|
||||
if vtune_id == station.id:
|
||||
return station
|
||||
return None
|
||||
|
||||
|
@ -82,6 +82,6 @@ def get_stations_by_category(category):
|
|||
station_icon = None
|
||||
if len(url_list) > 1:
|
||||
station_icon = url_list[1]
|
||||
station_id = str(generic.get_checksum(station_name + station_url)).upper()
|
||||
station_id = generic.get_checksum(station_name + station_url)
|
||||
stations.append(Station(station_id, station_name, station_url, category, station_icon))
|
||||
return stations
|
||||
|
|
|
@ -28,7 +28,7 @@ def get_json_attr(json, attr):
|
|||
class Station:
|
||||
def __init__(self, station_json):
|
||||
self.stationuuid = get_json_attr(station_json, 'stationuuid')
|
||||
self.id = generic.get_checksum(self.stationuuid)
|
||||
self.id = generic.generate_stationid_with_prefix(generic.get_checksum(self.stationuuid), 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')
|
||||
|
@ -40,7 +40,7 @@ class Station:
|
|||
self.bitrate = get_json_attr(station_json, 'bitrate')
|
||||
|
||||
def to_vtuner(self):
|
||||
return vtuner.Station(generic.generate_stationid_with_prefix(self.id, ID_PREFIX), self.name,
|
||||
return vtuner.Station(self.id, self.name,
|
||||
', '.join(self.tags), self.url, self.icon,
|
||||
self.tags[0], self.countrycode, self.codec, self.bitrate, None)
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import ycast.radiobrowser as radiobrowser
|
|||
import ycast.my_stations as my_stations
|
||||
import ycast.generic as generic
|
||||
import ycast.station_icons as station_icons
|
||||
from ycast.my_recentlystation import signalStationSelected
|
||||
from ycast.my_recentlystation import signal_station_selected
|
||||
|
||||
PATH_ROOT = 'ycast'
|
||||
PATH_PLAY = 'play'
|
||||
|
@ -102,9 +102,9 @@ def get_paged_elements(items, requestargs):
|
|||
def get_station_by_id(stationid, additional_info=False):
|
||||
station_id_prefix = generic.get_stationid_prefix(stationid)
|
||||
if station_id_prefix == my_stations.ID_PREFIX:
|
||||
return my_stations.get_station_by_id(generic.get_stationid_without_prefix(stationid))
|
||||
return my_stations.get_station_by_id(stationid)
|
||||
elif station_id_prefix == radiobrowser.ID_PREFIX:
|
||||
station = radiobrowser.get_station_by_id(generic.get_stationid_without_prefix(stationid))
|
||||
station = radiobrowser.get_station_by_id(stationid)
|
||||
if additional_info:
|
||||
station.get_playable_url()
|
||||
return station
|
||||
|
@ -299,7 +299,7 @@ def get_station_icon():
|
|||
if not station:
|
||||
logging.error("Could not get station with id '%s'", stationid)
|
||||
abort(404)
|
||||
signalStationSelected(station.name,station.url,station.icon)
|
||||
signal_station_selected(station.name, station.url, station.icon)
|
||||
if not hasattr(station, 'icon') or not station.icon:
|
||||
logging.warning("No icon information found for station with id '%s'", stationid)
|
||||
abort(404)
|
||||
|
|
Loading…
Add table
Reference in a new issue