From 0fb4edc655f90c9106b2fd7a266ee892fb898339 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Sat, 8 Jul 2023 12:50:20 +0300 Subject: [PATCH] changed if's hell --- src/mpv_control.py | 61 +++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/mpv_control.py b/src/mpv_control.py index 4148d55..d026ba7 100644 --- a/src/mpv_control.py +++ b/src/mpv_control.py @@ -54,40 +54,41 @@ if track_activity_history or scrobbler_to_fediverse_token != '': # Here, _value is either None if nothing is playing or a float containing # fractional seconds since the beginning of the file. if value: - if value >= 30.0 and value <= 30.1: - track = player_fw_storage.storage.get(track_url_to_uuid()) + if value >= 30.0 and value <= 30.1 and player.pause is False: + # detect 30 secs for reporting listen activity + try: + track = player_fw_storage.storage.get(track_url_to_uuid()) + except: + return + if src.fw_api.current_instance.token is not None: + track_id = track.get('id') + + if track_id: + if track_activity_history: + src.fw_api.record_track_in_history(track_id) + else: + logger.error("Can't write track to history: No track id") + if scrobbler_to_fediverse_token != '': + fid = track.get('fid') + artist = track['artist'].get('name') + album = track['album'].get('title') + title = track.get('title') + tags = track.get('tags') + if tags: + tags = [f'#{tag}' for tag in tags] + tags = ' '.join(tags) + if tags == []: + tags = '' + status_obj = {'spoiler_text': 'funkwhale-cli music scrobbler', + 'visibility': 'unlisted', + 'status': f'🎧 {artist} - {album} - {title}\n{fid}\n#NowPlaying {tags}'} + requests.post(f'https://{scrobbler_to_fediverse_instance}/api/v1/statuses', + json=status_obj, + headers={'Authorization': f'Bearer {scrobbler_to_fediverse_token}'}) time.sleep(0.100) if value > 30.1: time.sleep(1) return - if value and src.fw_api.current_instance.token != None and player.pause is False: - if value >= 30.0 and value <= 30.1: - # detect 30 secs for reporting listen activity - track_id = track.get('id') - - if track_id: - if track_activity_history: - src.fw_api.record_track_in_history(track_id) - else: - logger.error("Can't write track to history: No track id") - if value and scrobbler_to_fediverse_token != '' and player.pause is False: - if value >= 30.0 and value <= 30.1: - fid = track.get('fid') - artist = track['artist'].get('name') - album = track['album'].get('title') - title = track.get('title') - tags = track.get('tags') - if tags: - tags = [f'#{tag}' for tag in tags] - tags = ' '.join(tags) - if tags == []: - tags = '' - status_obj = {'spoiler_text': 'funkwhale-cli music scrobbler', - 'visibility': 'unlisted', - 'status': f'🎧 {artist} - {album} - {title}\n{fid}\n#NowPlaying {tags}'} - requests.post(f'https://{scrobbler_to_fediverse_instance}/api/v1/statuses', - json=status_obj, - headers={'Authorization': f'Bearer {scrobbler_to_fediverse_token}'}) def osd_observer(value):