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 fzf = FzfPrompt() @logger.catch def libraries(pg=None, radio=False, search=None): libs_res = list_libraries(pg=pg, q=search) libs_count = libs_res.get('count') libs_next = libs_res.get('next') libs_prev = libs_res.get('previous') libs = libs_res.get('results') libraries_listing = ['Search'] if libs_next: libraries_listing.append('Next') if libs_prev: libraries_listing.append('Prev') if current_instance.s.headers.get('Authorization'): libraries_listing.append('Add remote library') for lib_i in libs: index = libs.index(lib_i) lib_name = lib_i.get('name') lib_tracks_count = lib_i.get('uploads_count') lib_access = lib_i.get('privacy_level') lib_by = lib_i.get('actor').get('full_username') libraries_listing.append(f'{index}.{lib_name} | {lib_by} | {lib_tracks_count} | {lib_access}') lib_select = fzf.prompt( libraries_listing, f'--header=\'found {libs_count} libraries\nmap: library name | owner | tracks count\'') if lib_select == []: return else: lib_select = lib_select[0].split('.', 1) if lib_select[0] == 'Next': return libraries(pg=libs_next, search=search) elif lib_select[0] == 'Prev': return libraries(pg=libs_prev, search=search) elif lib_select[0] == 'Search': q = input('Name of library:\n') return libraries(search=q) elif lib_select[0] == 'Add remote library': print('Search a remote library (url\\fid):') 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']) return status = scan['status'] if status == 'scheduled': print(f'Scanning {status}. Please wait few minutes for scan and open libraries menu again') else: print(f'Scan is {status}') time.sleep(3) return else: lib_addr = lib_select[0] lib_name = lib_select[1] lib_uuid = libs[int(lib_addr)]['uuid'] lib_fid = libs[int(lib_addr)]['fid'] if radio: return None, 'library', f'{lib_name}\n{lib_fid}', lib_uuid else: return lib_uuid