2022-11-08 21:23:19 +02:00
|
|
|
import src.fw_api
|
2022-11-08 20:02:07 +02:00
|
|
|
from src.utils import download_track
|
2022-11-10 02:23:46 +02:00
|
|
|
from src.settings import get_config
|
2022-11-01 12:15:28 +02:00
|
|
|
from loguru import logger
|
2022-11-08 20:02:07 +02:00
|
|
|
from pyfzf.pyfzf import FzfPrompt
|
2022-11-02 02:05:08 +02:00
|
|
|
import mpv
|
2022-12-08 01:36:45 +02:00
|
|
|
import time
|
2022-12-20 03:07:08 +02:00
|
|
|
import sys
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-11-08 20:02:07 +02:00
|
|
|
fzf = FzfPrompt()
|
|
|
|
|
2022-12-21 03:21:04 +02:00
|
|
|
player = mpv.MPV(cache_secs=10)
|
2022-12-18 03:02:53 +02:00
|
|
|
player.ytdl = False # Prevent attempts load track with yt-dlp
|
|
|
|
player.volume = get_config('mpv_volume')
|
|
|
|
player.prefetch_playlist = get_config('prefetch_playlist')
|
2022-11-13 04:16:46 +02:00
|
|
|
show_like_button = get_config('show_like_button')
|
2022-12-08 01:36:45 +02:00
|
|
|
track_activity_history = get_config('track_activity_history')
|
2022-11-08 20:02:07 +02:00
|
|
|
|
2022-11-14 22:28:01 +02:00
|
|
|
|
|
|
|
class player_fw_storage:
|
|
|
|
storage = {}
|
|
|
|
|
|
|
|
|
2022-12-08 01:36:45 +02:00
|
|
|
@logger.catch
|
2022-11-18 12:22:55 +02:00
|
|
|
def track_url_to_uuid(listen_url=None):
|
2022-11-18 17:57:25 +02:00
|
|
|
'''Attempt get uuid from track listen url or current playing url'''
|
2022-11-18 12:22:55 +02:00
|
|
|
if listen_url:
|
|
|
|
uuid = listen_url.split(r'/')[-2]
|
|
|
|
else:
|
2022-11-18 17:57:25 +02:00
|
|
|
uuid = player.stream_open_filename.split(r'/')[-2]
|
2022-11-18 12:22:55 +02:00
|
|
|
return uuid
|
|
|
|
|
|
|
|
|
2022-12-18 03:02:53 +02:00
|
|
|
if track_activity_history:
|
2022-12-08 01:36:45 +02:00
|
|
|
@player.property_observer('time-pos')
|
|
|
|
@logger.catch
|
|
|
|
def time_observer(_name, value):
|
|
|
|
# Here, _value is either None if nothing is playing or a float containing
|
|
|
|
# fractional seconds since the beginning of the file.
|
2022-12-08 02:35:10 +02:00
|
|
|
if value and player.http_header_fields != [] and player.pause is False:
|
2022-12-18 03:02:53 +02:00
|
|
|
if value >= 30.0 and value <= 30.1:
|
2022-12-08 01:36:45 +02:00
|
|
|
# detect 30 secs for reporting listen activity
|
|
|
|
track = player_fw_storage.storage.get(track_url_to_uuid())
|
|
|
|
track_id = track.get('id')
|
|
|
|
if track_id:
|
|
|
|
src.fw_api.record_track_in_history(track_id)
|
|
|
|
else:
|
|
|
|
logger.error("Can't write track to history: No track id")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
2022-12-20 03:07:08 +02:00
|
|
|
@player.property_observer('filtered-metadata')
|
|
|
|
@logger.catch
|
|
|
|
def osd_observer(_name, value):
|
|
|
|
'''Sumulate osd playing message in console'''
|
|
|
|
if value:
|
|
|
|
osd_message = []
|
|
|
|
for i in value.items():
|
|
|
|
if i[0] in ('Artist', 'Album', 'Title'):
|
|
|
|
osd_message.append(i[1])
|
|
|
|
sys.stdout.write('\r ')
|
|
|
|
sys.stdout.write('\r'+' — '.join(osd_message))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
2022-12-08 01:36:45 +02:00
|
|
|
def set_http_header(headers=[]):
|
|
|
|
player.http_header_fields = headers
|
|
|
|
|
|
|
|
|
2022-11-08 20:37:43 +02:00
|
|
|
@logger.catch
|
2022-11-14 22:28:01 +02:00
|
|
|
def player_menu(header='', storage={}):
|
2022-12-08 01:24:48 +02:00
|
|
|
player_fw_storage.storage.update(storage)
|
2022-11-15 01:02:22 +02:00
|
|
|
player.volume = get_config("mpv_volume")
|
2022-11-08 20:02:07 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2022-11-13 04:16:46 +02:00
|
|
|
player_items_menu = ['Next', 'Prev', 'Pause',
|
2022-12-18 03:02:53 +02:00
|
|
|
'Download', 'Info']
|
2022-11-29 20:56:33 +02:00
|
|
|
if player.pause:
|
|
|
|
player_items_menu[2] = 'Play'
|
|
|
|
else:
|
|
|
|
player_items_menu[2] = 'Pause'
|
2022-11-13 04:16:46 +02:00
|
|
|
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]
|
2022-11-08 20:02:07 +02:00
|
|
|
if select == 'Next':
|
2022-12-20 03:33:29 +02:00
|
|
|
try:
|
|
|
|
player.playlist_next()
|
|
|
|
except:
|
|
|
|
print('No more next tracks')
|
2022-11-08 20:02:07 +02:00
|
|
|
elif select == 'Prev':
|
|
|
|
player.playlist_prev()
|
2022-11-29 20:56:33 +02:00
|
|
|
elif select in ('Pause', 'Play'):
|
2022-11-08 20:02:07 +02:00
|
|
|
if player.pause:
|
|
|
|
player.pause = False
|
|
|
|
else:
|
|
|
|
player.pause = True
|
|
|
|
elif select == 'Download':
|
2022-11-18 17:57:25 +02:00
|
|
|
name_downloaded = download_track(player.stream_open_filename)
|
2022-11-08 20:02:07 +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-18 12:22:55 +02:00
|
|
|
for i in track.keys():
|
2022-11-08 20:02:07 +02:00
|
|
|
if i in ('album', 'artist'):
|
|
|
|
name_aa = track.get(i).get('name')
|
|
|
|
if not name_aa:
|
|
|
|
name_aa = track.get(i).get('title')
|
2022-12-18 03:02:53 +02:00
|
|
|
print(i + ': ' + name_aa)
|
2022-11-08 20:02:07 +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 20:02:07 +02:00
|
|
|
input()
|
2022-11-13 04:16:46 +02:00
|
|
|
elif select == 'Like':
|
2022-12-18 03:02:53 +02:00
|
|
|
src.fw_api.favorite_track(
|
|
|
|
player_fw_storage.storage.get(track_url_to_uuid())['id'])
|
2022-11-08 21:23:19 +02:00
|
|
|
elif select == 'Hide artist':
|
2022-12-08 01:24:48 +02:00
|
|
|
track = player_fw_storage.storage.get(track_url_to_uuid())
|
2022-12-18 03:02:53 +02:00
|
|
|
src.fw_api.hide_content(
|
|
|
|
{'target': {'id': track.get('artist').get('id'), 'type': 'artist'}})
|
2022-11-08 20:02:07 +02:00
|
|
|
elif select == 'Exit':
|
|
|
|
player.playlist_clear()
|
|
|
|
player.stop()
|
2022-12-08 01:24:48 +02:00
|
|
|
player_fw_storage.storage = {}
|
2022-11-08 20:02:07 +02:00
|
|
|
break
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
|
|
|
|
2022-12-17 03:16:36 +02:00
|
|
|
|
|
|
|
def play_track(track, multi=False):
|
|
|
|
listen_url = src.fw_api.get_audio_file(track['listen_url'], True)
|
|
|
|
player_fw_storage.storage[track_url_to_uuid(listen_url)] = track
|
|
|
|
if multi:
|
|
|
|
player.loadfile(listen_url, 'append-play')
|
|
|
|
else:
|
|
|
|
player.loadfile(listen_url, 'append-play')
|
|
|
|
track_name = track.get('title')
|
|
|
|
player_menu(f"{track_name} playing...", player_fw_storage.storage)
|