funkwlmpv/src/fw_libraries.py

79 lines
2.9 KiB
Python
Raw Normal View History

from src.fw_api import current_instance, list_libraries, federate_remote_library, scan_remote_library, follow_on_remote_library
2022-11-08 22:34:33 +00:00
from pyfzf.pyfzf import FzfPrompt
from loguru import logger
import time
2022-11-08 22:34:33 +00:00
fzf = FzfPrompt()
2022-12-24 01:08:23 +00:00
@logger.catch
2023-08-02 21:29:11 +00:00
def libraries(pg=None, radio=False, search=None):
libs_res = list_libraries(pg=pg, q=search)
2022-11-08 22:34:33 +00:00
libs_count = libs_res.get('count')
libs_next = libs_res.get('next')
libs_prev = libs_res.get('previous')
2022-12-24 01:08:23 +00:00
libs = libs_res.get('results')
2023-08-02 21:29:11 +00:00
libraries_listing = ['Search']
2022-11-08 22:34:33 +00:00
if libs_next:
libraries_listing.append('Next')
if libs_prev:
libraries_listing.append('Prev')
2023-06-14 15:34:26 +00:00
if current_instance.s.headers.get('Authorization'):
libraries_listing.append('Add remote library')
2022-11-08 22:34:33 +00:00
for lib_i in libs:
index = libs.index(lib_i)
lib_name = lib_i.get('name')
2023-06-14 15:34:26 +00:00
lib_tracks_count = lib_i.get('uploads_count')
2023-06-17 22:34:44 +00:00
lib_access = lib_i.get('privacy_level')
lib_by = lib_i.get('actor').get('full_username')
2023-06-17 22:34:44 +00:00
libraries_listing.append(f'{index}.{lib_name} | {lib_by} | {lib_tracks_count} | {lib_access}')
2022-12-18 01:02:53 +00:00
lib_select = fzf.prompt(
2023-06-14 15:34:26 +00:00
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)
2022-11-08 22:34:33 +00:00
if lib_select[0] == 'Next':
2023-08-02 21:29:11 +00:00
return libraries(pg=libs_next, search=search)
2022-11-08 22:34:33 +00:00
elif lib_select[0] == 'Prev':
2023-08-02 21:29:11 +00:00
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
2022-11-08 22:34:33 +00: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