From 9c65117e9c6452b13cb7ca1859004987d23a4ea5 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Thu, 8 Dec 2022 02:36:45 +0300 Subject: [PATCH] Optional feature: Track listen activity to history (default is disabled) --- src/fw_api.py | 10 ++++++++++ src/mpv_control.py | 29 +++++++++++++++++++++++++---- src/settings.py | 1 + 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/fw_api.py b/src/fw_api.py index 613605b..efe229f 100644 --- a/src/fw_api.py +++ b/src/fw_api.py @@ -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 diff --git a/src/mpv_control.py b/src/mpv_control.py index 4ab33c4..d9ef044 100644 --- a/src/mpv_control.py +++ b/src/mpv_control.py @@ -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,6 +29,28 @@ 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.update(storage) diff --git a/src/settings.py b/src/settings.py index 913ea6c..65ad8b2 100644 --- a/src/settings.py +++ b/src/settings.py @@ -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,