funkwlmpv/src/fw_libraries.py

65 lines
2.2 KiB
Python
Raw Normal View History

from src.fw_api import list_libraries, federate_remote_library, scan_remote_library
2022-11-09 00:34:33 +02:00
from pyfzf.pyfzf import FzfPrompt
from loguru import logger
import time
2022-11-09 00:34:33 +02:00
fzf = FzfPrompt()
2022-12-24 03:08:23 +02:00
@logger.catch
def libraries(pg=None, radio=False):
2022-11-09 00:34:33 +02:00
libs_res = list_libraries(pg=pg)
libs_count = libs_res.get('count')
libs_next = libs_res.get('next')
libs_prev = libs_res.get('previous')
2022-12-24 03:08:23 +02:00
libs = libs_res.get('results')
2022-11-09 00:34:33 +02:00
libraries_listing = []
if libs_next:
libraries_listing.append('Next')
if libs_prev:
libraries_listing.append('Prev')
if radio == False:
libraries_listing.append('Add remote library')
2022-11-09 00:34:33 +02:00
for lib_i in libs:
index = libs.index(lib_i)
lib_name = lib_i.get('name')
lib_by = lib_i.get('actor').get('full_username')
libraries_listing.append(f'{index}.{lib_name} | by {lib_by}')
2022-12-18 03:02:53 +02:00
lib_select = fzf.prompt(
libraries_listing, f'--header=\'found {libs_count} libraries\'')[0].split('.', 1)
2022-11-09 00:34:33 +02:00
if lib_select[0] == 'Next':
2022-12-24 03:08:23 +02:00
return libraries(pg=libs_next)
2022-11-09 00:34:33 +02:00
elif lib_select[0] == 'Prev':
2022-12-24 03:08:23 +02:00
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())
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]
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
2022-11-09 00:34:33 +02:00
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