'Bare bones' channels support; added federated search by url (scrapped from network monitor inspector)

This commit is contained in:
localhost_frssoft 2022-11-09 04:01:02 +03:00
parent ff26263496
commit 5c3e459cfe
4 changed files with 81 additions and 9 deletions

View file

@ -1,7 +1,8 @@
from src.fw_api import s, select_instance, instance
from src.fw_api import s, select_instance, instance, federate_search_by_url
from src.fw_radios import list_radios
from src.fw_artists import list_artists
from src.fw_albums import list_albums
from src.fw_channels import list_channels
import json, sys
from loguru import logger
from pyfzf.pyfzf import FzfPrompt
@ -13,10 +14,14 @@ def main():
logger.add(sys.stderr, filter='src.fw_api')
logger.add(sys.stderr, filter='src.fw_radios')
logger.add(sys.stderr, filter='src.fw_artists')
logger.add(sys.stderr, filter='src.fw_albums')
logger.add(sys.stderr, filter='src.fw_channels')
while True:
menu = ['Radios',
'Artists',
'Albums',
'Albums',
'Channels',
'Search',
'Switch instance']
if not s.headers.get('Authorization'):
menu.append('Sign in')
@ -29,6 +34,14 @@ def main():
list_artists()
if selected == 'Albums':
list_albums()
if selected == 'Channels':
list_channels()
if selected == 'Search':
search_type = fzf.prompt(('Federated', 'All types'))[0]
if search_type == 'Federated':
print('Input url:')
returned_obj = federate_search_by_url(input())
if selected == 'Switch instance':
with open('config.json', 'rt') as f:
conf = json.loads(f.read())

View file

@ -7,12 +7,12 @@ from loguru import logger
fzf = FzfPrompt()
@logger.catch
def list_albums(albums=None, pg=None, search=None):
def list_albums(albums=None, pg=None, search=None, artist=None, include_channels=None):
albums_next = None
albums_prev = None
play_artist_albums = False
if not albums:
albums = get_albums(q=search, pg=pg)
albums = get_albums(q=search, artist=artist, include_channels=include_channels, pg=pg)
albums_next = albums.get('next')
albums_prev = albums.get('previous')
albums_results = albums.get('results')
@ -46,7 +46,7 @@ def list_albums(albums=None, pg=None, search=None):
def play_album(album_id):
tracks = get_tracks(album=album_id)
tracks = get_tracks(album=album_id, include_channels=True)
tracks_results = tracks.get('results')
storage = {}
for i in tracks_results:

View file

@ -45,14 +45,15 @@ def concatinate_endpoint(endpoint):
@logger.catch
def get_tracks(page=None, q=None, artist=None, album=None, favourites=None, pg=None):
def get_tracks(page=None, q=None, artist=None, album=None, favourites=None, include_channels=None, pg=None):
'''This function get tracks by params'''
params = {
'page': page,
'q': q,
'artist': artist,
'album': album,
'favourites': favourites
'favourites': favourites,
'include_channels': include_channels
}
if pg:
r = s.get(pg)
@ -79,12 +80,13 @@ def get_artists(page=None, q=None, artist=None, album=None, favourites=None, pg=
@logger.catch
def get_albums(page=None, q=None, artist=None, pg=None):
def get_albums(page=None, q=None, artist=None, include_channels=None, pg=None):
'''This function get artists by params'''
params = {
'page': page,
'q': q,
'artist': artist
'artist': artist,
'include_channels': include_channels
}
if pg:
r = s.get(pg)
@ -93,6 +95,20 @@ def get_albums(page=None, q=None, artist=None, pg=None):
return r.json()
@logger.catch
def get_channels(page=None, q=None, tag=None, pg=None):
params = {
'page': page,
'q': q,
'tag': tag
}
if pg:
r = s.get(pg)
else:
r = s.get(f'https://{instance}/api/v1/channels', params=params)
return r.json()
@logger.catch
def list_libraries(page=None, page_size=None, q=None, scope='all', pg=None):
params = {
@ -108,6 +124,15 @@ def list_libraries(page=None, page_size=None, q=None, scope='all', pg=None):
return r.json()
@logger.catch
def federate_search_by_url(object):
params = {
'object': object
}
r = s.post(f'https://{instance}/api/v1/federation/fetches', json=params)
return r.json()
@logger.catch
def favorite_track(track_id):
r = s.post(f'https://{instance}/api/v1/favorites/tracks', json={'track': int(track_id)})

34
src/fw_channels.py Normal file
View file

@ -0,0 +1,34 @@
from src.fw_api import get_channels
from src.fw_albums import list_albums
from loguru import logger
from pyfzf.pyfzf import FzfPrompt
fzf = FzfPrompt()
@logger.catch
def list_channels(pg=None, search=None):
channels = get_channels(q=search, pg=pg)
channels_next = channels.get('next')
channels_prev = channels.get('previous')
channels_results = channels.get('results')
view = ['Search']
if channels_next:
view.append('Next page')
if channels_prev:
view.append('Prev page')
for i in channels_results:
index = channels_results.index(i)
channel_name = i.get('artist').get('name')
view.append(f'{index}.{channel_name}')
select = fzf.prompt(view)[0].split('.', 1)[0]
if select == 'Next page':
list_channels(pg=channels_next)
elif select == 'Prev page':
list_channels(pg=channels_prev)
elif select == 'Search':
print('Search by channel:')
list_channels(search=input())
else:
list_albums(artist=channels_results[int(select)].get('artist').get('id'), include_channels=True)