funkwlmpv/src/mpv_control.py

83 lines
2.8 KiB
Python

import src.fw_api
from src.utils import download_track
from src.settings import get_config
from loguru import logger
from pyfzf.pyfzf import FzfPrompt
import mpv
fzf = FzfPrompt()
player = mpv.MPV()
player.ytdl = False # Prevent attempts load track with yt-dlp
player.prefetch_playlist = get_config('prefetch_playlist') # Fast loading next track, but high network traffic
show_like_button = get_config('show_like_button')
class player_fw_storage:
storage = {}
def set_http_header(headers=[]):
player.http_header_fields = headers
def track_url_to_uuid(listen_url=None):
'''Attempt get uuid from track listen url or current playing url'''
if listen_url:
uuid = listen_url.split(r'/')[-2]
else:
uuid = player.stream_open_filename.split(r'/')[-2]
return uuid
@logger.catch
def player_menu(header='', storage={}):
player_fw_storage.storage = storage
player.volume = get_config("mpv_volume")
while True:
try:
player_items_menu = ['Next', 'Prev', 'Pause',
'Download', 'Info']
if show_like_button:
player_items_menu.append('Like')
player_items_menu.extend(['Hide artist', 'Exit'])
select = fzf.prompt(player_items_menu, 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.stream_open_filename)
print(f'Downloaded: {name_downloaded}')
elif select == 'Info':
track = storage.get(track_url_to_uuid())
for i in track.keys():
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 == 'Like':
src.fw_api.favorite_track(storage.get(track_url_to_uuid())['id'])
elif select == 'Hide artist':
track = storage.get(track_url_to_uuid())
src.fw_api.hide_content({'target': {'id': track.get('artist').get('id'), 'type': 'artist'}})
elif select == 'Exit':
player.playlist_clear()
player.stop()
break
except KeyboardInterrupt:
break