add recently stations in favourites

This commit is contained in:
Thomas Hanika 2022-01-24 17:14:12 +01:00
parent 9d903a1a1d
commit 107ce5c0fa
3 changed files with 83 additions and 4 deletions

71
ycast/my_lastheard.py Normal file
View file

@ -0,0 +1,71 @@
import logging
import os
import yaml
VAR_PATH = os.path.expanduser("~") + '/.ycast'
config_file = VAR_PATH + '/lastheared.yml'
def signalStationSelected(name,url,icon):
logging.debug(" %s:%s|%s",name,url,icon)
list_heared_stations = get_stations_list()
if len(list_heared_stations) == 0 :
list_heared_stations.append("recently used:\n")
for line in list_heared_stations:
elements = line.split(':')
if elements[0] == ' '+name:
list_heared_stations.remove(line)
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')
if len(list_heared_stations) > 11:
# remove last
list_heared_stations.pop()
set_stations_yaml(list_heared_stations)
def set_stations_yaml(heared_stations):
try:
os.makedirs(VAR_PATH)
except FileExistsError:
pass
except PermissionError:
logging.error("Could not create folders (%s) because of access permissions", VAR_PATH)
return None
try:
with open(config_file, 'w') as f:
f.writelines(heared_stations)
logging.info("File written '%s'", config_file)
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:
heared_stations = f.readlines()
except FileNotFoundError:
logging.warning("File not found '%s' not found", config_file)
return []
except yaml.YAMLError as e:
logging.error("Station configuration format error: %s", e)
return []
return heared_stations
def get_last_stations_yaml():
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

View file

@ -44,17 +44,24 @@ def get_station_by_id(uid):
def get_stations_yaml():
from ycast.my_lastheard import get_last_stations_yaml
my_last_station = get_last_stations_yaml()
my_stations = None
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
if my_stations:
if my_last_station:
my_stations.append(my_last_station)
else:
return my_last_station
return my_stations
def get_category_directories():
my_stations_yaml = get_stations_yaml()

View file

@ -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_lastheard import signalStationSelected
PATH_ROOT = 'ycast'
PATH_PLAY = 'play'
@ -296,6 +296,7 @@ def get_station_icon():
logging.error("Station icon without station ID requested")
abort(400)
station = get_station_by_id(stationid)
signalStationSelected(station.name,station.url,station.icon)
if not station:
logging.error("Could not get station with id '%s'", stationid)
abort(404)