From a4694a4dbe8e64e47ef45652b147ab8f21092458 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Sun, 6 Nov 2022 03:45:56 +0300 Subject: [PATCH] Added download func; fix radio generator --- src/fw_api.py | 1 - src/fw_radios.py | 24 +++++++++++++++++++----- src/utils.py | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/utils.py diff --git a/src/fw_api.py b/src/fw_api.py index ac8496c..1f1d8b5 100644 --- a/src/fw_api.py +++ b/src/fw_api.py @@ -33,7 +33,6 @@ def select_instance(new_instance=None): player.http_header_fields = ['Authorization: ' + 'Bearer ' + new_token] - def concatinate_endpoint(endpoint): return 'https://' + instance + endpoint diff --git a/src/fw_radios.py b/src/fw_radios.py index 5d50e81..9ca7a7c 100644 --- a/src/fw_radios.py +++ b/src/fw_radios.py @@ -1,6 +1,8 @@ from src.fw_api import get_radios, post_radio_session, get_track_radio, concatinate_endpoint +from src.utils import download_track from src.mpv_control import player from pyfzf.pyfzf import FzfPrompt +from loguru import logger import threading import time @@ -29,13 +31,20 @@ def list_radios(): id_selected = selected[0] id_radio = results[int(id_selected)].get('id') radio_load(id_radio) - + def radio_generator(radio_session_id): + count_t = 0 while radio_session_id != '': - radio_get_track(radio_session_id) - time.sleep(60) + time.sleep(1) + if not radio_event_gen.wait(0): + break + count_t += 1 + if count_t >= 60: + radio_get_track(radio_session_id) + logger.info('Radio generator stopped') +radio_event_gen = threading.Event() def radio_load(id_radio=None, type_radio='custom'): requested_radio = { @@ -48,11 +57,13 @@ def radio_load(id_radio=None, type_radio='custom'): radio_get_track(radio_session_id) player.playlist_pos = 0 + radio_event_gen.set() radio_task = threading.Thread(target=radio_generator, args=(radio_session_id,), daemon=True) radio_task.start() while True: try: - select = fzf.prompt(('Next', 'Prev', 'Pause', 'Exit'), f'--header=\'Radio playing...\'')[0] + input() + select = fzf.prompt(('Next', 'Prev', 'Pause', 'Download', 'Exit'), f'--header=\'Radio playing...\'')[0] if select == 'Next': radio_get_track(radio_session_id) player.playlist_next() @@ -63,10 +74,13 @@ def radio_load(id_radio=None, type_radio='custom'): player.pause = False else: player.pause = True + elif select == 'Download': + download_track(player.filename) elif select == 'Exit': try: - radio_task.join(1) + radio_event_gen.clear() except: + logger.exception('Error stopping Thread radio generator') pass player.playlist_clear() player.stop() diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..ccbbc30 --- /dev/null +++ b/src/utils.py @@ -0,0 +1,22 @@ +from src.fw_api import s +from urllib.parse import unquote + + +def get_remote_file_name(url): + '''This function return filename by content-disposition header''' + r = s.head(url) + content_dispos = r.headers.get('content-disposition') + if content_dispos.startswith('attachment; filename*=UTF-8\'\''): + return unquote(content_dispos.split('attachment; filename*=UTF-8\'\'')[-1]) + + +def download_track(url, name=None): + r = s.get(url) + if not name: + name = get_remote_file_name(url) + if not name: + name = url.split(r'/')[-1] + + with open(name, 'wb') as f: + f.write(r.content) +