Added download func; fix radio generator

This commit is contained in:
localhost_frssoft 2022-11-06 03:45:56 +03:00
parent 67fa8e4a6a
commit a4694a4dbe
3 changed files with 41 additions and 6 deletions

View file

@ -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

View file

@ -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()

22
src/utils.py Normal file
View file

@ -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)