-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_config.py
133 lines (114 loc) · 4.67 KB
/
radio_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import os
from dataclasses import dataclass, field
from typing import List, Dict
@dataclass
class RadioStation:
name: str
url: str
logo: str
@dataclass
class FavoritesList:
filename: str
stations: List[RadioStation] = field(default_factory=list)
def __init__(self, favorites_dir, file_name: str):
self.stations = self.load_stations_from_json(os.path.join(favorites_dir, file_name))
self.filename = file_name
@classmethod
def load_stations_from_json(cls, file_path: str) -> List[RadioStation]:
with open(file_path, 'r') as file:
data = json.load(file)
stations = [RadioStation(**station) for station in data]
return stations
@dataclass
class Config:
favorites_dict: Dict[str, FavoritesList]
favorites_dir: str
last_favlist: int
last_favlist_name: str
last_station: int
last_station_name: str
last_station_url:str
last_station_logo: str
last_volume: int
screensaver_after_s: int
auto_save_config: bool
is_dirty: bool = False
filepath: str = None
all_stations_dict = {}
@staticmethod
def from_json(config_filepath: str) -> 'Config':
with open(config_filepath, 'r') as file:
data = json.load(file)
favorites_dict = {}
for favorite in data['favorites']:
favorites_dict[favorite['name']] = FavoritesList(favorites_dir=data['favorites_dir'], file_name=favorite['file'])
return Config(
favorites_dict=favorites_dict,
favorites_dir=data['favorites_dir'],
last_favlist=data['last_favlist'],
last_favlist_name=data['last_favlist_name'],
last_station=data['last_station'],
last_station_name=data['last_station_name'],
last_station_url=data['last_station_url'],
last_station_logo=data['last_station_logo'],
last_volume=data['last_volume'],
screensaver_after_s=data['screensaver_after_s'],
auto_save_config=data['auto_save_config'],
filepath = config_filepath
)
def to_json(self):
data = {
'favorites': [{'name': fav_list[0], 'file': fav_list[1].filename} for fav_list in self.favorites_dict.items()],
'favorites_dir': self.favorites_dir,
'last_favlist': self.last_favlist,
'last_favlist_name': self.last_favlist_name,
'last_station': self.last_station,
'last_station_name': self.last_station_name,
'last_station_url': self.last_station_url,
'last_station_logo': self.last_station_logo,
'last_volume': self.last_volume,
'screensaver_after_s': self.screensaver_after_s,
'auto_save_config': self.auto_save_config
}
with open(self.filepath, 'w') as file:
json.dump(data, file, indent=4)
def get_last_station_data(self):
return RadioStation(name=self.last_station_name, url=self.last_station_url, logo=self.last_station_logo)
def get_favlist_by_idx(self, idx):
for i, name in enumerate(self.favorites_dict.keys()):
if i == 0:
fallback = name
if i == idx:
return name
return fallback
def change_property(self, prop_name, prop_value):
if hasattr(self, prop_name):
self.__setattr__(prop_name, prop_value)
self.is_dirty = True
else:
print(f"Error: Config property '{prop_name}' does not exist.")
def __post_init__(self):
self.all_stations_dict = self.get_all_stations_dict()
print(f"all {len(self.all_stations_dict)} stations: {self.all_stations_dict} ")
def get_all_stations_dict(self):
all_stations = {}
for i, name in enumerate(self.favorites_dict.keys()):
for station in self.favorites_dict[name].stations:
if all_stations.get(station.name) is not None:
print(f"station data {all_stations.get(station.name)} already exists. Skipping new {station}")
else:
all_stations[station.name] = station
return all_stations
def get_station_data_by_name(self, name):
data = self.all_stations_dict.get(name)
if data is None:
print(f"Station data for '{name}' does not exist.")
return data
#_favorites_path = 'favorites'
# = Config.from_json('config.json')
#print(config)
#config.to_json()
#favorites_list = FavoritesList('favorites/fav_news.json')
#for station in favorites_list.stations:
# print(f"Name: {station.name}, URL: {station.url}, Logo: {station.logo}")