From 35879ac63e6a1cdac1ec7e9de94848b2c978750e Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Thu, 15 Jun 2023 15:11:21 +0300 Subject: [PATCH] Automatically follow on library when adding --- funkwhale_cli.py | 8 +++++++- src/fw_api.py | 27 +++++++++++++++++++++++++-- src/fw_libraries.py | 8 ++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/funkwhale_cli.py b/funkwhale_cli.py index 61d4b69..3d28352 100755 --- a/funkwhale_cli.py +++ b/funkwhale_cli.py @@ -106,7 +106,13 @@ def main(): If You want sign in, please visit: https://{current_instance.instance}/settings/applications/new And fill Name funkwhale-cli -Scopes: Read, Write (optional): write:favorites write:listenings write:filters +Scopes: +Read | Write (optional): + write:libraries + write:favorites + write:listenings + write:follows + write:filters Insert token from "Access token" here''') register_token = input() diff --git a/src/fw_api.py b/src/fw_api.py index ae352ec..1147784 100644 --- a/src/fw_api.py +++ b/src/fw_api.py @@ -306,11 +306,10 @@ def federate_remote_library(fid): @logger.catch def scan_remote_library(uuid): - '''Scan remote library''' for i in range(attempts_for_federate): try: r = current_instance.s.post( - f'https://{current_instance.instance}/api/v1/federation/libraries/{uuid}') + f'https://{current_instance.instance}/api/v1/federation/libraries/{uuid}/scan') r.raise_for_status() break except Exception as Err: @@ -319,6 +318,30 @@ def scan_remote_library(uuid): return r.json() +@logger.catch +def follow_on_remote_library(uuid): + params = {'target': uuid} + for i in range(attempts_for_federate): + try: + r = current_instance.s.post( + f'https://{current_instance.instance}/api/v1/federation/follows/library/', + json=params) + r.raise_for_status() + break + except Exception as Err: + logger.error(f'Attempt follow {i}: {Err}') + time.sleep(3) + return r.json() + + +@logger.catch +def unfollow_remote_library(uuid): + r = current_instance.s.delete( + f'https://{current_instance.instance}/api/v1/federation/follows/library/{uuid}/') + r.raise_for_status() + return r.json() + + @logger.catch def record_track_in_history(track_id): params = { diff --git a/src/fw_libraries.py b/src/fw_libraries.py index 9a9e5b4..0bef12f 100644 --- a/src/fw_libraries.py +++ b/src/fw_libraries.py @@ -1,4 +1,4 @@ -from src.fw_api import current_instance, list_libraries, federate_remote_library, scan_remote_library +from src.fw_api import current_instance, list_libraries, federate_remote_library, scan_remote_library, follow_on_remote_library from pyfzf.pyfzf import FzfPrompt from loguru import logger import time @@ -41,13 +41,17 @@ def libraries(pg=None, radio=False): return libraries(pg=libs_prev) elif lib_select[0] == 'Add remote library': print('Search a remote library (url\\fid):') - new_library = federate_remote_library(input()) + new_library = federate_remote_library(input().strip()) if new_library.get('detail'): logger.error(new_library['detail']) return if new_library.get('count') > 0: print('Library found') one_lib = new_library['results'][0] + if one_lib['privacy_level'] == 'private': + logger.warning('This library is private, you should wait until your request is approved') + + follow_on_remote_library(one_lib['uuid']) scan = scan_remote_library(one_lib['uuid']) if scan.get('detail'): logger.error(scan['detail'])