mpv player menu

This commit is contained in:
localhost_frssoft 2022-11-08 21:02:07 +03:00
parent a654768958
commit 7f2a3ae4c9
2 changed files with 50 additions and 3 deletions

View File

@ -1,6 +1,11 @@
from src.utils import download_track
from loguru import logger from loguru import logger
from pyfzf.pyfzf import FzfPrompt
import mpv import mpv
fzf = FzfPrompt()
def mpv_log(loglevel, component, message): def mpv_log(loglevel, component, message):
if loglevel == 'info': if loglevel == 'info':
logger.info(f'{component} {message}') logger.info(f'{component} {message}')
@ -9,3 +14,45 @@ def mpv_log(loglevel, component, message):
player = mpv.MPV(log_handler=mpv_log, ytdl=False, player = mpv.MPV(log_handler=mpv_log, ytdl=False,
prefetch_playlist=True) prefetch_playlist=True)
def set_http_header(headers=[]):
player.http_header_fields = headers
def player_menu(header=None, storage={}):
while True:
try:
select = fzf.prompt(('Next', 'Prev', 'Pause', 'Download', 'Info', 'Exit'), f"--header=\'{header}\'")[0]
if select == 'Next':
player.playlist_next()
elif select == 'Prev':
player.playlist_prev()
elif select == 'Pause':
if player.pause:
player.pause = False
else:
player.pause = True
elif select == 'Download':
print('Downloading...')
name_downloaded = download_track(player.filename)
print(f'Downloaded: {name_downloaded}')
elif select == 'Info':
track = storage.get(player.filename)
for i in ('title', 'fid', 'license', 'album', 'artist'):
if i in ('album', 'artist'):
name_aa = track.get(i).get('name')
if not name_aa:
name_aa = track.get(i).get('title')
print(i + ': '+ name_aa)
key = track.get(i)
if key and isinstance(key, str):
print(i + ': ' + key)
input()
elif select == 'Exit':
player.playlist_clear()
player.stop()
break
except KeyboardInterrupt:
break

View File

@ -1,17 +1,17 @@
from src.fw_api import s import src.fw_api
from urllib.parse import unquote from urllib.parse import unquote
def get_remote_file_name(url): def get_remote_file_name(url):
'''This function return filename by content-disposition header''' '''This function return filename by content-disposition header'''
r = s.head(url) r = src.fw_api.s.head(url)
content_dispos = r.headers.get('content-disposition') content_dispos = r.headers.get('content-disposition')
if content_dispos.startswith('attachment; filename*=UTF-8\'\''): if content_dispos.startswith('attachment; filename*=UTF-8\'\''):
return unquote(content_dispos.split('attachment; filename*=UTF-8\'\'')[-1]) return unquote(content_dispos.split('attachment; filename*=UTF-8\'\'')[-1])
def download_track(url, name=None): def download_track(url, name=None):
r = s.get(url) r = src.fw_api.s.get(url)
if not name: if not name:
name = get_remote_file_name(url) name = get_remote_file_name(url)
if not name: if not name: