refactor my_recentlystation

This commit is contained in:
Thomas Hanika 2022-01-29 12:03:25 +01:00
parent f38b1f566c
commit a80d6f15f9
3 changed files with 71 additions and 40 deletions

View file

@ -1,57 +1,88 @@
from ycast import generic
MAX_ENTRIES = 15
# define a max, so after 5 hits, an other station is get better votes
MAX_VOTES = 5
DIRECTORY_NAME = "recently used"
recently_file = generic.get_var_path() + '/recently.yml'
is_yml_file_loadable = True
recently_station_dictionary = None
class StationVote:
def __init__(self, name, params_txt):
self.name = name
params = params_txt.split('|')
self.url = params[0]
self.icon = ''
self.vote = 0
if len(params) > 0:
self.icon = params[1]
if len(params) > 1:
self.vote = int(params[2])
def to_params_txt(self):
text_line = self.url + '|' + self.icon + '|' + str(self.vote) + '\n'
return text_line
def signal_station_selected(name, url, icon):
list_heard_stations = get_stations_list()
if not list_heard_stations:
list_heard_stations = []
list_heard_stations.append(DIRECTORY_NAME + ":\n")
# make name yaml - like
name = name.replace(":", " -")
for line in list_heard_stations:
elements = line.split(': ')
if elements[0] == ' '+name:
list_heard_stations.remove(line)
piped_icon = ''
if icon and len(icon) > 0:
piped_icon = '|' + icon
recently_station_list = get_stations_list()
station_hit = StationVote(name, url + '|' + icon)
for recently_station in recently_station_list:
if name == recently_station.name:
station_hit.vote = recently_station.vote + 1
recently_station_list.remove(recently_station)
break
recently_station_list.insert(0, station_hit)
list_heard_stations.insert(1, ' '+name+': '+url+piped_icon+'\n')
if len(list_heard_stations) > MAX_ENTRIES+1:
if station_hit.vote > MAX_VOTES:
for recently_station in recently_station_list:
if recently_station.vote > 0:
recently_station.vote = recently_station.vote - 1
if len(recently_station_list) > MAX_ENTRIES:
# remove last (oldest) entry
list_heard_stations.pop()
recently_station_list.pop()
set_stations_yaml(list_heard_stations)
set_station_dictionary(directory_name(), recently_station_list)
def set_stations_yaml(heard_stations):
global is_yml_file_loadable
is_yml_file_loadable = generic.writelns_txt_file(recently_file, heard_stations)
def set_station_dictionary(cathegory, station_list):
global recently_station_dictionary
new_cathegory_dictionary = {}
station_dictionary = {}
for station in station_list:
station_dictionary[station.name] = station.to_params_txt()
new_cathegory_dictionary[cathegory] = station_dictionary
recently_station_dictionary = new_cathegory_dictionary
generic.write_yaml_file(recently_file, recently_station_dictionary)
def get_stations_list():
return generic.readlns_txt_file(recently_file)
stations_list = []
cathegory_dict = get_recently_stations_yaml()
if cathegory_dict:
for cat_key in cathegory_dict:
station_dict = cathegory_dict[cat_key]
for station_key in station_dict:
stations_list.append(StationVote(station_key, station_dict[station_key]))
return stations_list
def get_recently_stations_yaml():
global is_yml_file_loadable
dict_stations = None
if is_yml_file_loadable:
dict_stations = generic.read_yaml_file(recently_file)
if not dict_stations:
is_yml_file_loadable = False
return dict_stations
# cached recently
global recently_station_dictionary
if not recently_station_dictionary:
recently_station_dictionary = generic.read_yaml_file(recently_file)
if not recently_station_dictionary:
recently_station_dictionary[DIRECTORY_NAME] = None
return recently_station_dictionary
def directory_name():
dir = generic.read_yaml_file(recently_file)
if dir:
return list(dir.keys())[0]
return None
return list(get_recently_stations_yaml().keys())[0]

View file

@ -8,8 +8,9 @@ config_file = generic.get_var_path() + '/stations.yml'
class Station:
def __init__(self, uid, name, url, category, icon):
self.id = generic.generate_stationid_with_prefix(uid, ID_PREFIX)
def __init__(self, name, url, category, icon):
self.id = generic.generate_stationid_with_prefix(
generic.get_checksum(name + url), ID_PREFIX)
self.name = name
self.url = url
self.tag = category
@ -66,11 +67,10 @@ def get_stations_by_category(category):
if my_stations_yaml and category in my_stations_yaml:
for station_name in my_stations_yaml[category]:
station_urls = my_stations_yaml[category][station_name]
url_list = station_urls.split('|')
station_url = url_list[0]
param_list = station_urls.split('|')
station_url = param_list[0]
station_icon = None
if len(url_list) > 1:
station_icon = url_list[1]
station_id = generic.get_checksum(station_name + station_url)
stations.append(Station(station_id, station_name, station_url, category, station_icon))
if len(param_list) > 1:
station_icon = param_list[1]
stations.append(Station(station_name, station_url, category, station_icon))
return stations

View file

@ -156,7 +156,7 @@ def landing(path=''):
stations = my_stations.get_stations_by_category(my_recentlystation.directory_name())
if stations and len(stations) > 0:
# emulate Sp
# make blank line (display is not shown)
page.add(vtuner.Directory(' ', url_for('my_stations_landing', _external=True),
len(my_stations.get_category_directories())))
count = 0