2022-12-04 03:01:11 +02:00
|
|
|
from src.fw_api import current_instance, get_radios, post_radio_session, get_track_radio, list_libraries, favorite_track, get_audio_file
|
2022-11-09 00:34:33 +02:00
|
|
|
from src.fw_libraries import libraries
|
2022-11-30 02:33:14 +02:00
|
|
|
from src.fw_tags import list_tags
|
2022-11-06 02:45:56 +02:00
|
|
|
from src.utils import download_track
|
2022-12-08 01:24:48 +02:00
|
|
|
from src.mpv_control import player, track_url_to_uuid, player_fw_storage
|
2022-11-14 01:26:47 +02:00
|
|
|
from src.settings import get_config
|
2022-11-01 12:15:28 +02:00
|
|
|
from pyfzf.pyfzf import FzfPrompt
|
2022-11-06 02:45:56 +02:00
|
|
|
from loguru import logger
|
2022-11-02 02:05:08 +02:00
|
|
|
import threading
|
|
|
|
import time
|
2022-12-21 22:08:46 +02:00
|
|
|
import sys
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-11-01 12:15:28 +02:00
|
|
|
fzf = FzfPrompt()
|
|
|
|
|
|
|
|
|
2022-11-08 22:39:01 +02:00
|
|
|
@logger.catch
|
2022-11-01 12:15:28 +02:00
|
|
|
def list_radios():
|
|
|
|
radios = get_radios()
|
|
|
|
count = radios.get('count')
|
|
|
|
|
|
|
|
results = radios.get('results')
|
|
|
|
view = []
|
2022-12-18 03:02:53 +02:00
|
|
|
for i in results:
|
2022-11-01 12:15:28 +02:00
|
|
|
index = results.index(i)
|
|
|
|
id_radio = i.get('id')
|
|
|
|
name = i.get('name')
|
2022-11-24 00:48:59 +02:00
|
|
|
descr = i.get('description')
|
|
|
|
radio_option = name
|
|
|
|
if descr and descr != "":
|
|
|
|
radio_option += f' | {descr}'
|
|
|
|
view.append(f'{index}.{radio_option}')
|
2022-12-18 03:02:53 +02:00
|
|
|
# Radios avalaible only for auth user
|
|
|
|
if current_instance.s.headers.get('Authorization'):
|
2022-11-06 03:26:50 +02:00
|
|
|
view.append('Favourites')
|
|
|
|
view.append('Less listened')
|
2022-11-30 02:33:14 +02:00
|
|
|
view.extend(['Tag', 'Random', 'Libraries', 'Users', 'Recently Added'])
|
2022-11-03 01:58:55 +02:00
|
|
|
|
2022-12-18 03:02:53 +02:00
|
|
|
selected = fzf.prompt(
|
|
|
|
view, f'--header \'Found {count} radios\' --read0', delimiter="\0")[0].split('.', 1)
|
2022-11-03 01:58:55 +02:00
|
|
|
if 'Favourites' in selected:
|
2022-11-24 00:58:17 +02:00
|
|
|
radio_load(id_radio, 'favorites', name='your favorites tracks')
|
2022-11-30 02:33:14 +02:00
|
|
|
elif 'Tag' in selected:
|
|
|
|
tag = list_tags()
|
2022-11-30 02:41:14 +02:00
|
|
|
radio_load(type_radio='tag', name=f'by tag: {tag}', related_object=tag)
|
2022-11-06 03:26:50 +02:00
|
|
|
elif 'Random' in selected:
|
2022-11-24 00:58:17 +02:00
|
|
|
radio_load(id_radio, 'random', name='totally random')
|
2022-11-08 22:39:01 +02:00
|
|
|
elif 'Libraries' in selected:
|
2022-11-09 00:34:33 +02:00
|
|
|
id_radio, type_radio, name_radio, related_obj = libraries()
|
|
|
|
radio_load(id_radio, type_radio, name_radio, related_obj)
|
2022-11-08 22:39:01 +02:00
|
|
|
elif 'Users' in selected:
|
2022-11-08 02:29:44 +02:00
|
|
|
libs = list_libraries()['results']
|
|
|
|
libraries_listing = []
|
|
|
|
for lib_i in libs:
|
|
|
|
lib_name = lib_i.get('actor').get('full_username')
|
|
|
|
if lib_name not in libraries_listing:
|
|
|
|
libraries_listing.append(lib_name)
|
|
|
|
libraries_listing.append('Custom')
|
|
|
|
lib_addr = fzf.prompt(libraries_listing)[0]
|
|
|
|
if lib_addr == 'Custom':
|
|
|
|
print('Input remote user library (ex. nick@funkwhale.domain.example: ')
|
|
|
|
lib_addr = input()
|
2022-11-08 01:40:26 +02:00
|
|
|
radio_load(None, 'actor-content', lib_addr, lib_addr)
|
2022-11-06 03:26:50 +02:00
|
|
|
elif 'Recently Added' in selected:
|
2022-12-18 03:02:53 +02:00
|
|
|
radio_load(id_radio, 'recently-added',
|
|
|
|
name='Newest content on the network')
|
2022-11-06 03:26:50 +02:00
|
|
|
elif 'Less listened' in selected:
|
2022-11-24 00:58:17 +02:00
|
|
|
radio_load(id_radio, 'less-listened', name="Less listened tracks")
|
2022-11-03 01:58:55 +02:00
|
|
|
else:
|
|
|
|
id_selected = selected[0]
|
|
|
|
id_radio = results[int(id_selected)].get('id')
|
2022-11-07 13:36:02 +02:00
|
|
|
name_radio = results[int(id_selected)].get('name')
|
|
|
|
radio_load(id_radio, name=name_radio)
|
2022-11-06 02:45:56 +02:00
|
|
|
|
2022-11-01 12:15:28 +02:00
|
|
|
|
2022-11-02 02:05:08 +02:00
|
|
|
def radio_generator(radio_session_id):
|
2022-11-06 02:45:56 +02:00
|
|
|
count_t = 0
|
2022-11-02 02:05:08 +02:00
|
|
|
while radio_session_id != '':
|
2022-11-06 02:45:56 +02:00
|
|
|
time.sleep(1)
|
2022-12-18 03:02:53 +02:00
|
|
|
if not radio_event_gen.wait(0):
|
2022-11-06 02:45:56 +02:00
|
|
|
break
|
|
|
|
count_t += 1
|
|
|
|
if count_t >= 60:
|
2022-11-07 00:21:49 +02:00
|
|
|
count_t = 0
|
2022-12-18 03:02:53 +02:00
|
|
|
playlist_remaining = len(player.playlist) - \
|
|
|
|
player.playlist_current_pos
|
|
|
|
if playlist_remaining <= 2:
|
2022-11-24 03:12:34 +02:00
|
|
|
radio_get_track(radio_session_id)
|
2022-12-21 22:08:46 +02:00
|
|
|
sys.stdout.write('\rRadio generator stopped')
|
|
|
|
sys.stdout.flush()
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-12-18 03:02:53 +02:00
|
|
|
|
2022-11-06 02:45:56 +02:00
|
|
|
radio_event_gen = threading.Event()
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-11-08 20:37:43 +02:00
|
|
|
|
|
|
|
@logger.catch
|
2022-11-08 01:40:26 +02:00
|
|
|
def radio_load(id_radio=None, type_radio='custom', name=None, related_object=None):
|
2022-11-14 01:26:47 +02:00
|
|
|
show_like_button = get_config('show_like_button')
|
2022-11-15 01:02:22 +02:00
|
|
|
player.volume = get_config('mpv_volume')
|
2022-11-01 12:15:28 +02:00
|
|
|
requested_radio = {
|
2022-12-18 03:02:53 +02:00
|
|
|
'custom_radio': id_radio,
|
|
|
|
'radio_type': type_radio,
|
|
|
|
'related_object_id': related_object
|
|
|
|
}
|
2022-11-01 12:15:28 +02:00
|
|
|
radio_session_id = post_radio_session(requested_radio).get('id')
|
2022-11-08 01:40:26 +02:00
|
|
|
for i in range(0, 2):
|
2022-11-01 12:15:28 +02:00
|
|
|
radio_get_track(radio_session_id)
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-11-06 02:45:56 +02:00
|
|
|
radio_event_gen.set()
|
2022-12-18 03:02:53 +02:00
|
|
|
radio_task = threading.Thread(
|
|
|
|
target=radio_generator, args=(radio_session_id,), daemon=True)
|
2022-11-02 02:05:08 +02:00
|
|
|
radio_task.start()
|
2022-12-18 03:02:53 +02:00
|
|
|
player_items_menu = ['Next', 'Prev', 'Pause', 'Download', 'Info']
|
2022-11-29 20:56:33 +02:00
|
|
|
|
2022-11-18 12:22:55 +02:00
|
|
|
if show_like_button:
|
|
|
|
player_items_menu.append('Like')
|
|
|
|
player_items_menu.extend(['Hide artist', 'Exit'])
|
2022-11-02 02:05:08 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2022-11-29 20:56:33 +02:00
|
|
|
if player.pause:
|
|
|
|
player_items_menu[2] = 'Play'
|
|
|
|
else:
|
|
|
|
player_items_menu[2] = 'Pause'
|
2022-12-18 03:02:53 +02:00
|
|
|
select = fzf.prompt(player_items_menu,
|
|
|
|
f"--header=\'Radio {name} playing...\'")[0]
|
2022-11-02 02:05:08 +02:00
|
|
|
if select == 'Next':
|
2022-12-18 03:02:53 +02:00
|
|
|
playlist_remaining = len(
|
|
|
|
player.playlist) - player.playlist_current_pos
|
2022-11-24 03:12:34 +02:00
|
|
|
if playlist_remaining <= 2:
|
2022-12-18 03:02:53 +02:00
|
|
|
threading.Thread(target=radio_get_track, args=(
|
|
|
|
radio_session_id,), daemon=True).start()
|
2022-12-20 03:33:29 +02:00
|
|
|
if playlist_remaining > 1:
|
|
|
|
player.playlist_next()
|
|
|
|
else:
|
|
|
|
print('No more tracks, please wait for new...')
|
|
|
|
time.sleep(3)
|
2022-11-02 02:05:08 +02:00
|
|
|
elif select == 'Prev':
|
|
|
|
player.playlist_prev()
|
2022-11-18 12:22:55 +02:00
|
|
|
elif select in ('Pause', 'Play'):
|
2022-11-02 02:05:08 +02:00
|
|
|
if player.pause:
|
|
|
|
player.pause = False
|
|
|
|
else:
|
|
|
|
player.pause = True
|
2022-11-06 02:45:56 +02:00
|
|
|
elif select == 'Download':
|
2022-11-18 17:57:25 +02:00
|
|
|
name_downloaded = download_track(player.stream_open_filename)
|
2022-11-08 01:40:26 +02:00
|
|
|
elif select == 'Info':
|
2022-12-08 01:24:48 +02:00
|
|
|
track = player_fw_storage.storage.get(track_url_to_uuid())
|
2022-11-08 01:40:26 +02:00
|
|
|
for i in ('title', 'fid', 'license', 'album', 'artist'):
|
|
|
|
if i in ('album', 'artist'):
|
|
|
|
name_aa = track.get(i).get('name')
|
|
|
|
if not name_aa:
|
2022-11-18 12:22:55 +02:00
|
|
|
name_aa = track[i]['title']
|
2022-12-18 03:02:53 +02:00
|
|
|
print(i + ': ' + name_aa)
|
2022-11-08 01:40:26 +02:00
|
|
|
key = track.get(i)
|
|
|
|
if key and isinstance(key, str):
|
|
|
|
print(i + ': ' + key)
|
2022-12-20 03:07:08 +02:00
|
|
|
print('Direct link: ' + player.stream_open_filename)
|
2022-11-08 01:40:26 +02:00
|
|
|
input()
|
2022-11-18 12:22:55 +02:00
|
|
|
elif select == 'Like':
|
2022-12-18 03:02:53 +02:00
|
|
|
favorite_track(player_fw_storage.storage.get(
|
|
|
|
track_url_to_uuid())['id'])
|
2022-11-02 02:05:08 +02:00
|
|
|
elif select == 'Exit':
|
|
|
|
try:
|
2022-11-06 02:45:56 +02:00
|
|
|
radio_event_gen.clear()
|
2022-11-02 02:05:08 +02:00
|
|
|
except:
|
2022-11-06 02:45:56 +02:00
|
|
|
logger.exception('Error stopping Thread radio generator')
|
2022-11-02 02:05:08 +02:00
|
|
|
pass
|
|
|
|
player.playlist_clear()
|
|
|
|
player.stop()
|
2022-12-08 01:24:48 +02:00
|
|
|
player_fw_storage.storage = {}
|
2022-11-02 02:05:08 +02:00
|
|
|
break
|
2022-12-18 03:02:53 +02:00
|
|
|
except:
|
2022-11-09 20:51:20 +02:00
|
|
|
try:
|
|
|
|
radio_event_gen.clear()
|
|
|
|
except:
|
|
|
|
logger.exception('Error stopping Thread radio generator')
|
|
|
|
pass
|
|
|
|
player.playlist_clear()
|
|
|
|
player.stop()
|
2022-12-08 01:24:48 +02:00
|
|
|
player_fw_storage.storage = {}
|
2022-11-09 20:51:20 +02:00
|
|
|
logger.exception('Radio force stopped')
|
2022-11-02 02:05:08 +02:00
|
|
|
break
|
2022-11-01 12:15:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def radio_get_track(radio_session_id):
|
|
|
|
radio_context = get_track_radio({'session': radio_session_id})
|
2022-11-07 00:21:49 +02:00
|
|
|
if not radio_context:
|
|
|
|
return
|
2022-11-09 20:51:20 +02:00
|
|
|
if isinstance(radio_context, str):
|
|
|
|
logger.error(radio_context)
|
2022-11-10 22:21:56 +02:00
|
|
|
if radio_context == "Radio doesn't have more candidates":
|
|
|
|
radio_event_gen.clear()
|
2022-11-09 20:51:20 +02:00
|
|
|
return
|
|
|
|
if radio_context.get('error'):
|
|
|
|
logger.error(radio_context.get('error'))
|
2022-11-03 01:58:55 +02:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
track = radio_context.get('track')
|
2022-11-18 17:57:25 +02:00
|
|
|
listen_url = track['listen_url']
|
2022-12-08 01:24:48 +02:00
|
|
|
player_fw_storage.storage[track_url_to_uuid(listen_url)] = track
|
2022-12-18 03:02:53 +02:00
|
|
|
player.loadfile(get_audio_file(
|
|
|
|
listen_url, listen_url=True), 'append-play')
|