funkwlmpv/funkwhale_cli.py
localhost_frssoft 06b2ebacd1 Fix menus when exit ESC or CTRL+C
So... After a long time i did fix for some menus
Reduce errors hell in terminal
2023-06-15 00:52:34 +03:00

148 lines
5.7 KiB
Python
Executable file

#!/usr/bin/env python3
from src.fw_api import current_instance, get_instance_settings
from src.fw_radios import list_radios
from src.fw_artists import list_artists
from src.fw_albums import list_albums
from src.fw_tracks import list_tracks
from src.fw_channels import list_channels
from src.fw_playlists import list_playlists
from src.fw_recents import list_fav_or_history
import src.settings as settings
import src.mpv_control
import json
import os
from shlex import quote
from pyfzf.pyfzf import FzfPrompt
fzf = FzfPrompt()
def main():
while True:
support_message = ''
instance_title = ''
menu = ['Radios',
'Artists',
'Albums',
'Tracks',
'Channels',
'Playlists',
'Favorites',
'Recently listened',
'About instance',
'Switch instance']
try:
ins_settings, ins_nodeinfo = get_instance_settings()
for i in ins_settings:
if i.get('name') == 'support_message':
support_message = i.get('value')
if i.get('name') == 'name':
instance_title = i.get('value')
instance_stats = []
for k, v in ins_nodeinfo['metadata']['library'].items():
if k == 'anonymousCanListen' and v == False and not current_instance.s.headers.get('Authorization'):
instance_stats.append(f'!!! {k}: {v} !!!')
menu = ['Switch instance', 'About instance']
continue
instance_stats.append(f'{k}: {v}')
instance_stats.append(ins_nodeinfo['software']['version'])
instance_stats = '\n'.join(instance_stats)
main_menu_header = quote(f'''{instance_title}\n{instance_stats}'''.strip())
except Exception as E:
splitted = ':\n'.join(str(E).split(':'))
main_menu_header = quote(f'''Connection failed:\n{splitted}'''.strip())
menu = ['Switch instance']
if not current_instance.s.headers.get('Authorization'):
menu.append('Sign in')
if support_message != '':
menu.append('Donate')
if src.mpv_control.player.playlist_playing_pos != -1:
menu.insert(0, 'Player')
selected = fzf.prompt(menu, f"--header={main_menu_header}")
if selected == []:
print('bye-bye :3')
break
else:
selected = selected[0]
if selected == 'Radios':
list_radios()
if selected == 'Artists':
list_artists()
if selected == 'Albums':
list_albums()
if selected == 'Tracks':
list_tracks()
if selected == 'Channels':
list_channels()
if selected == 'Playlists':
list_playlists()
if selected == 'Favorites':
list_fav_or_history()
if selected == 'Recently listened':
list_fav_or_history(is_history_view=True)
if selected == 'Switch instance':
with open('config.json', 'rt') as f:
conf = json.loads(f.read())
public_server_list_instances = settings.get_new_funkwhale_servers()
new_ins_count = len(settings.get_new_funkwhale_servers())
list_instances = conf.get(
'public_list_instances') + public_server_list_instances
settings.set_config('public_list_instances', list_instances)
instance = fzf.prompt(list_instances,
'--header='+quote(f'Select instance\nServer instances: +{new_ins_count}'))
if instance == []:
continue
else:
instance = instance[0]
current_instance.select_instance(instance)
if selected == 'Sign in':
print(f'''
If You want sign in, please visit:
https://{current_instance.instance}/settings/applications/new
And fill Name funkwhale-cli
Scopes: Read, Write (optional): write:favorites write:listenings write:filters
Insert token from "Access token" here''')
register_token = input()
with open('.auth.json', 'rt') as f:
tkns = json.loads(f.read())
with open('.auth.json', 'wt') as f:
tkns[current_instance.instance] = register_token
f.write(json.dumps(tkns))
del tkns
del register_token
del f
os.system('clear')
current_instance.select_instance(current_instance.instance)
if selected == 'Donate':
os.system(f'less <<EOF\nSupport instance message:\n{support_message}')
if selected == 'About instance':
about_instance_info = []
for i in ins_settings:
k, v = i.get('verbose_name'), i.get('value')
about_instance_info.append(f'{k}: {v}')
about_instance_info.append('|||||Some stats:')
for k, v in ins_nodeinfo['metadata']['usage'].items():
about_instance_info.append(f'{k}: {v}')
for k, v in ins_nodeinfo['metadata']['library'].items():
about_instance_info.append(f'{k}: {v}')
for k, v in ins_nodeinfo['usage'].items():
about_instance_info.append(f'{k}: {v}')
about_instance_info = '\n'.join(about_instance_info)
os.system(f'less <<EOF\n{about_instance_info}')
del about_instance_info
if selected == 'Player':
src.mpv_control.player_menu(
storage=src.mpv_control.player_fw_storage.storage)
if __name__ == '__main__':
main()