Compare commits

...

4 commits

5 changed files with 50 additions and 14 deletions

4
funkwhale_cli.py Normal file → Executable file
View file

@ -1,3 +1,5 @@
#!/usr/bin/env python3
from src.fw_api import current_instance, federate_search_by_url, get_instance_settings
from src.fw_radios import list_radios
from src.fw_artists import list_artists
@ -85,7 +87,7 @@ def main():
If You want sign in, please visit:
https://{instance}/settings/applications/new
And fill Name funkwhale-cli
Scopes: Read (only listen music), Write (optional)
Scopes: Read, Write (optional): write:favorites write:listenings write:filters
Insert token from "Access token" here''')
register_token = input()

View file

@ -228,6 +228,16 @@ def federate_search_by_url(object):
}
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/federation/fetches', json=params)
return r.json()
@logger.catch
def record_track_in_history(track_id):
params = {
'track': int(track_id)
}
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/history/listenings', json=params)
r.raise_for_status()
return r.json
@logger.catch

View file

@ -2,7 +2,7 @@ from src.fw_api import current_instance, get_radios, post_radio_session, get_tra
from src.fw_libraries import libraries
from src.fw_tags import list_tags
from src.utils import download_track
from src.mpv_control import player, track_url_to_uuid
from src.mpv_control import player, track_url_to_uuid, player_fw_storage
from src.settings import get_config
from pyfzf.pyfzf import FzfPrompt
from loguru import logger
@ -11,7 +11,6 @@ import time
fzf = FzfPrompt()
audio_info = {}
@logger.catch
def list_radios():
@ -131,7 +130,7 @@ def radio_load(id_radio=None, type_radio='custom', name=None, related_object=Non
name_downloaded = download_track(player.stream_open_filename)
print(f'Downloaded: {name_downloaded}')
elif select == 'Info':
track = audio_info.get(track_url_to_uuid())
track = player_fw_storage.storage.get(track_url_to_uuid())
for i in ('title', 'fid', 'license', 'album', 'artist'):
if i in ('album', 'artist'):
name_aa = track.get(i).get('name')
@ -143,7 +142,7 @@ def radio_load(id_radio=None, type_radio='custom', name=None, related_object=Non
print(i + ': ' + key)
input()
elif select == 'Like':
favorite_track(audio_info.get(track_url_to_uuid())['id'])
favorite_track(player_fw_storage.storage.get(track_url_to_uuid())['id'])
elif select == 'Exit':
try:
radio_event_gen.clear()
@ -152,6 +151,7 @@ def radio_load(id_radio=None, type_radio='custom', name=None, related_object=Non
pass
player.playlist_clear()
player.stop()
player_fw_storage.storage = {}
break
except:
try:
@ -161,6 +161,7 @@ def radio_load(id_radio=None, type_radio='custom', name=None, related_object=Non
pass
player.playlist_clear()
player.stop()
player_fw_storage.storage = {}
logger.exception('Radio force stopped')
break
@ -180,7 +181,7 @@ def radio_get_track(radio_session_id):
else:
track = radio_context.get('track')
listen_url = track['listen_url']
audio_info[track_url_to_uuid(listen_url)] = track
player_fw_storage.storage[track_url_to_uuid(listen_url)] = track
player.loadfile(get_audio_file(listen_url, listen_url=True), 'append-play')

View file

@ -4,6 +4,7 @@ from src.settings import get_config
from loguru import logger
from pyfzf.pyfzf import FzfPrompt
import mpv
import time
fzf = FzfPrompt()
@ -11,16 +12,14 @@ 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')
track_activity_history = get_config('track_activity_history')
class player_fw_storage:
storage = {}
def set_http_header(headers=[]):
player.http_header_fields = headers
@logger.catch
def track_url_to_uuid(listen_url=None):
'''Attempt get uuid from track listen url or current playing url'''
if listen_url:
@ -30,9 +29,31 @@ def track_url_to_uuid(listen_url=None):
return uuid
if track_activity_history:
@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.
if value and player.http_header_fields != []:
if value >= 30.0 and value <= 30.1:
# 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)
def set_http_header(headers=[]):
player.http_header_fields = headers
@logger.catch
def player_menu(header='', storage={}):
player_fw_storage.storage = storage
player_fw_storage.storage.update(storage)
player.volume = get_config("mpv_volume")
while True:
try:
@ -61,7 +82,7 @@ def player_menu(header='', storage={}):
name_downloaded = download_track(player.stream_open_filename)
print(f'Downloaded: {name_downloaded}')
elif select == 'Info':
track = storage.get(track_url_to_uuid())
track = player_fw_storage.storage.get(track_url_to_uuid())
for i in track.keys():
if i in ('album', 'artist'):
name_aa = track.get(i).get('name')
@ -73,13 +94,14 @@ def player_menu(header='', storage={}):
print(i + ': ' + key)
input()
elif select == 'Like':
src.fw_api.favorite_track(storage.get(track_url_to_uuid())['id'])
src.fw_api.favorite_track(player_fw_storage.storage.get(track_url_to_uuid())['id'])
elif select == 'Hide artist':
track = storage.get(track_url_to_uuid())
track = player_fw_storage.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()
player_fw_storage.storage = {}
break
except KeyboardInterrupt:
break

View file

@ -39,6 +39,7 @@ default_conf = {
"shitnoise.monster"
],
'enable_server_transcoding': False,
'track_activity_history': False,
'prefetch_playlist': True,
'mpv_volume': 100,
'show_like_button': True,