Skip to content

Commit 17e573d

Browse files
committed
stations icons are now being cached in the DATA_DIR, folder
stations-logos
1 parent 9a748ea commit 17e573d

File tree

5 files changed

+100
-22
lines changed

5 files changed

+100
-22
lines changed

pyradio/client.py

-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ def _get_files(self):
157157
path.join(state_dir, 'server-headless.txt'),
158158
path.join(state_dir, 'server.txt')
159159
)
160-
print(f'{self._files}')
161160

162161
def print_addresses(self):
163162
self._get_files()

pyradio/config.py

+48
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,8 @@ class PyRadioConfig(PyRadioStations):
13801380

13811381
need_to_fix_desktop_file_icon = False
13821382

1383+
notification_image_file = None
1384+
13831385
def __init__(self, user_config_dir=None, headless=False):
13841386
# keep old recording / new recording dir
13851387
self.rec_dirs = ()
@@ -2089,6 +2091,50 @@ def read_config(self, distro_config=False):
20892091
def re_read_config(self):
20902092
self._read_config()
20912093
self.xdg.ensure_paths_exist()
2094+
self.create_stations_images_dir()
2095+
2096+
def create_stations_images_dir(self, to_console=False):
2097+
if int(self.enable_notifications) >= 0:
2098+
try:
2099+
makedirs(self.stations_images_dir, exist_ok = True)
2100+
except:
2101+
if to_console:
2102+
print('[red]Error[/red]: Cannot create fir: "{}"\n'.format(self.stations_images_dir))
2103+
return False
2104+
2105+
2106+
src_dir = path.join(
2107+
getenv(
2108+
'XDG_CONFIG_HOME',
2109+
path.join(path.expanduser('~'), '.config')),
2110+
'pyradio', 'data', 'stations-logos'
2111+
)
2112+
if path.exists(src_dir) and \
2113+
src_dir != self.stations_images_dir:
2114+
try:
2115+
# Copy files, remove directories is source
2116+
for item in listdir(src_dir):
2117+
src_path = path.join(src_dir, item)
2118+
dst_path = path.join(self.stations_images_dir, item)
2119+
if path.isfile(src_path):
2120+
copyfile(src_path, dst_path)
2121+
elif path.isdir(src_path):
2122+
remove_tree(src_path) # Remove directory instead of copying it
2123+
2124+
# Check if any directories were left after removing them in the first loop
2125+
for item in listdir(src_dir):
2126+
src_path = path.join(src_dir, item)
2127+
if path.isfile(src_path):
2128+
remove(src_path)
2129+
elif path.isdir(src_path):
2130+
remove_tree(src_path)
2131+
2132+
# Remove the source directory itself
2133+
remove_tree(src_dir)
2134+
except Exception as e:
2135+
# If an error occurs, we do not care :)
2136+
pass
2137+
return True
20922138

20932139
def _read_config(self, distro_config=False):
20942140
xdg_compliant_read_from_file = False
@@ -2368,6 +2414,8 @@ def _read_config(self, distro_config=False):
23682414
print('[magenta]XDG Dirs[/magenta] found; enabling [magenta]XDG Base compliant[/magenta] operation')
23692415
self.xdg_compliant = True
23702416
self.need_to_fix_desktop_file_icon = True
2417+
''' do this here to get XDG state '''
2418+
self.stations_images_dir = path.join(self.data_dir, 'stations-logos')
23712419

23722420
def _make_sure_dirs_exist(self):
23732421
if self.opts['recording_dir'][1] == '':

pyradio/log.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def __init__(self, config, get_web_song_title):
8888
self.width = None
8989
self._get_startup_window_title()
9090
self._enable_notifications = int(self._cnf.enable_notifications)
91-
self._repeat_notification = RepeatDesktopNotification(lambda: self._enable_notifications)
91+
self._repeat_notification = RepeatDesktopNotification(
92+
self._cnf, lambda: self._enable_notifications
93+
)
9294
self._get_icon_path()
9395

9496
def __del__(self):
@@ -372,7 +374,7 @@ def _set_web_title(self, msg):
372374

373375

374376
def _get_icon_path(self):
375-
self.icon_path = None
377+
self.icon_path = self._cnf.notification_image_file
376378
if self.icon_path is None:
377379
if platform_system().lower().startswith('win'):
378380
the_path = (
@@ -422,6 +424,7 @@ def _show_notification(self, msg):
422424
self._desktop_notification_thread = None
423425
return
424426

427+
self._get_icon_path()
425428
if platform.lower().startswith('win'):
426429
if HAS_WIN10TOAST:
427430
d_title, d_msg = self._get_desktop_notification_data(msg)
@@ -727,7 +730,8 @@ def set_win_title(msg=None):
727730

728731
class RepeatDesktopNotification(object):
729732

730-
def __init__(self, timeout):
733+
def __init__(self, config, timeout):
734+
self._cnf = config
731735
self._a_lock = self._start_time = None
732736
self.timeout = timeout
733737

@@ -835,16 +839,17 @@ def _populate_notification_command(self, a_notification_command, d_title, d_msg)
835839
if 'MSG' in notification_command[i]:
836840
notification_command[i] = notification_command[i].replace('MSG', d_msg)
837841
if 'ICON' in notification_command[i]:
838-
if platform.lower().startswith('win'):
839-
icon = self.icon_path
840-
else:
841-
temp_dir = gettempdir()
842-
ic = (
843-
join(temp_dir, 'station.jpg'),
844-
join(temp_dir, 'station.png'),
845-
self.icon_path
846-
)
847-
icon = [x for x in ic if exists(x)][0]
842+
# if platform.lower().startswith('win'):
843+
# icon = self.icon_path
844+
# else:
845+
# ic = (
846+
# join(self._cnf.stations_images_dir, 'station.jpg'),
847+
# join(self._cnf.stations_images_dir, 'station.png'),
848+
# self.icon_path
849+
# )
850+
# logger.error('\n\nic\n{}\n'.format(ic))
851+
# icon = [x for x in ic if exists(x)][0]
852+
icon = self.icon_path
848853
notification_command[i] = notification_command[i].replace('ICON', icon)
849854
return notification_command
850855

pyradio/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ def shell():
394394

395395
with pyradio_config_file(user_config_dir, args.headless) as pyradio_config:
396396
read_config(pyradio_config)
397+
if not pyradio_config.create_stations_images_dir(True):
398+
return
397399

398400
if not system().lower().startswith('darwin') and \
399401
not system().lower().startswith('win'):

pyradio/radio.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ def __init__(self, pyradio_config,
425425
record=False):
426426
temp_dir = gettempdir()
427427
self._station_images = (
428-
join(temp_dir, 'station.jpg'),
429-
join(temp_dir, 'station.png'),
430-
join(temp_dir, 'station-icon.raw'),
428+
join(pyradio_config.stations_images_dir, 'station.jpg'),
429+
join(pyradio_config.stations_images_dir, 'station.png'),
430+
join(pyradio_config.stations_images_dir, 'station-icon.raw'),
431431
)
432432
self._message_system_default_operation_mode = self.ws.MESSAGING_MODE
433433
self._request_recording = record
@@ -2256,6 +2256,7 @@ def playSelection(self, restart=False):
22562256
self.playing = self._last_played_station_id
22572257
else:
22582258
self._remove_station_images()
2259+
self._cnf.notification_image_file = None
22592260
if self._cnf.enable_notifications and \
22602261
self._cnf.use_station_icon and \
22612262
not platform.startswith('win'):
@@ -2265,6 +2266,7 @@ def playSelection(self, restart=False):
22652266
self.log.write(msg='Downloading icon...')
22662267
self._download_station_image(
22672268
self.stations[self.selection][3]['image'],
2269+
self.stations[self.selection][0],
22682270
lambda: self.stop_update_notification_thread
22692271
)
22702272
# if self._cnf.browsing_station_service:
@@ -2458,18 +2460,39 @@ def _show_player_is_stopped(self, from_update_thread=False):
24582460
help_msg=True, suffix=self._status_suffix, counter=''
24592461
)
24602462

2461-
def _download_station_image(self, url, stop):
2463+
def _download_station_image(self, url, station_name, stop):
24622464
threading.Thread(
24632465
target=self._thread_download_station_image,
2464-
args=(url, stop),
2466+
args=(url, station_name, self._notification_icon, stop),
24652467
).start()
24662468

2467-
def _thread_download_station_image(self, url, stop):
2469+
def _notification_icon(self, a_name):
2470+
if path.exists(a_name):
2471+
self._cnf.notification_image_file = a_name
2472+
if logger.isEnabledFor(logging.DEBUG):
2473+
logger.debug('Notification image: "{}"'.format(self._cnf.notification_image_file))
2474+
else:
2475+
if logger.isEnabledFor(logging.DEBUG):
2476+
logger.debug('Notification image is invalid; reverting to default...')
2477+
2478+
2479+
def _thread_download_station_image(
2480+
self,
2481+
url,
2482+
station_name,
2483+
update_icon_name_function,
2484+
stop
2485+
):
24682486
if url:
24692487
if logger.isEnabledFor(logging.DEBUG):
24702488
logger.debug('+++ downloading icon...')
2471-
logger.error('\n\n{}\n\n'.format(self._station_images))
2472-
file_to_write = self._station_images[0] if url.endswith('.jpg') else self._station_images[1]
2489+
template_image = self._station_images[0] if url.endswith('.jpg') else self._station_images[1]
2490+
file_to_write = template_image.replace('station.', station_name.replace(' ', '_') + '.')
2491+
if path.exists(file_to_write):
2492+
if logger.isEnabledFor(logging.DEBUG):
2493+
logger.debug('+++ icon download: aleready downloaded...')
2494+
update_icon_name_function(file_to_write)
2495+
return
24732496
if stop():
24742497
if logger.isEnabledFor(logging.DEBUG):
24752498
logger.debug('+++ icon download: asked to stop. Stopping...')
@@ -2503,6 +2526,7 @@ def _thread_download_station_image(self, url, stop):
25032526
self._remove_station_images()
25042527
if logger.isEnabledFor(logging.DEBUG):
25052528
logger.debug('+++ icon downloaded...')
2529+
update_icon_name_function(file_to_write)
25062530
else:
25072531
if logger.isEnabledFor(logging.DEBUG):
25082532
logger.debug('+++ icon downloaded, but already exists...')

0 commit comments

Comments
 (0)