Add view artists and their albums

This commit is contained in:
localhost_frssoft 2022-11-08 20:58:27 +03:00
parent a1765500ed
commit a654768958
4 changed files with 143 additions and 9 deletions

View file

@ -1,5 +1,6 @@
from src.fw_api import s, select_instance, instance
from src.fw_radios import list_radios
from src.fw_artists import list_artists
import json, sys
from loguru import logger
from pyfzf.pyfzf import FzfPrompt
@ -10,8 +11,11 @@ def main():
logger.remove()
logger.add(sys.stderr, filter='src.fw_api')
logger.add(sys.stderr, filter='src.fw_radios')
logger.add(sys.stderr, filter='src.fw_artists')
while True:
menu =['Radios', 'Switch instance']
menu = ['Radios',
'Artists',
'Switch instance']
if not s.headers.get('Authorization'):
menu.append('Sign in')
ids = fzf.prompt(menu)
@ -19,6 +23,8 @@ def main():
selected = ids[0]
if selected == 'Radios':
list_radios()
if selected == 'Artists':
list_artists()
if selected == 'Switch instance':
with open('config.json', 'rt') as f:
conf = json.loads(f.read())

49
src/fw_albums.py Normal file
View file

@ -0,0 +1,49 @@
from src.fw_api import get_artists, get_tracks, get_albums, concatinate_endpoint
import src.fw_artists
from src.mpv_control import player, player_menu
from pyfzf.pyfzf import FzfPrompt
from loguru import logger
fzf = FzfPrompt()
@logger.catch
def list_albums(albums=None, pg=None):
albums_next = None
albums_prev = None
if not albums:
albums = get_albums(pg=pg)
albums_next = albums.get('next')
albums_prev = albums.get('previous')
albums_results = albums.get('results')
else:
albums_results = albums
view = ['Play all']
if albums_next:
view.append('Next page')
if albums_prev:
view.append('Prev page')
for i in albums_results:
index = albums_results.index(i)
album_name = i.get('title')
view.append(f'{index}.{album_name}')
select = fzf.prompt(view)[0].split('.', 1)[0]
if select == 'Next page':
list_albums(pg=albums_next)
elif select == 'Prev page':
list_albums(pg=albums_prev)
elif select == 'Play all':
src.fw_artists.play_artist(albums_results[0].get('artist'))
else:
play_album(album_id=albums_results[int(select)].get('id'))
def play_album(album_id):
tracks = get_tracks(album=album_id)
tracks_results = tracks.get('results')
storage = {}
for i in tracks_results:
listen_url = concatinate_endpoint(i.get('listen_url'))
storage[listen_url] = i
player.loadfile(listen_url, 'append-play')
player_menu("Album playing...", storage)

View file

@ -1,4 +1,4 @@
from src.mpv_control import player
from src.mpv_control import set_http_header
import requests, json, time
from loguru import logger
@ -13,10 +13,10 @@ if token:
s.headers.update({
"Authorization": "Bearer " + token
})
player.http_header_fields = ['Authorization: ' + 'Bearer ' + token]
set_http_header(['Authorization: ' + 'Bearer ' + token])
else:
s.get(f'https://{instance}/') # Get cookies from unauthorized instance for working some functionality (radios)
player.http_header_fields = []
set_http_header()
def select_instance(new_instance=None):
@ -26,20 +26,22 @@ def select_instance(new_instance=None):
auth = json.loads(f.read())
new_token = auth.get(instance)
s.headers.update({"Authorization": None})
player.http_header_fields = []
set_http_header()
if new_token:
s.get(f'https://{instance}')
s.headers.update({
"Authorization": "Bearer " + new_token
})
player.http_header_fields = ['Authorization: ' + 'Bearer ' + new_token]
set_http_header(['Authorization: ' + 'Bearer ' + token])
def concatinate_endpoint(endpoint):
return 'https://' + instance + endpoint
def list_tracks(page=None, q=None, artist=None, album=None, favourites=None):
@logger.catch
def get_tracks(page=None, q=None, artist=None, album=None, favourites=None, pg=None):
'''This function get tracks by params'''
params = {
'page': page,
@ -48,11 +50,46 @@ def list_tracks(page=None, q=None, artist=None, album=None, favourites=None):
'album': album,
'favourites': favourites
}
r = requests.get(f'https://{instance}/api/v1/tracks', params)
if pg:
r = s.get(pg)
else:
r = s.get(f'https://{instance}/api/v1/tracks', params=params)
return r.json()
@logger.catch
def get_artists(page=None, q=None, artist=None, album=None, favourites=None, pg=None):
'''This function get artists by params'''
params = {
'page': page,
'q': q,
'artist': artist,
'album': album,
'favourites': favourites
}
if pg:
r = s.get(pg)
else:
r = s.get(f'https://{instance}/api/v1/artists', params=params)
return r.json()
@logger.catch
def get_albums(page=None, q=None, artist=None, pg=None):
'''This function get artists by params'''
params = {
'page': page,
'q': q,
'artist': artist
}
if pg:
r = s.get(pg)
else:
r = s.get(f'https://{instance}/api/v1/albums', params=params)
return r.json()
def list_libraries(page=None, page_size=None, q=None, scope='all'):
params = {
'page': page,
@ -60,7 +97,7 @@ def list_libraries(page=None, page_size=None, q=None, scope='all'):
'q': q,
'scope': scope,
}
r = requests.get(f'https://{instance}/api/v1/libraries', params)
r = s.get(f'https://{instance}/api/v1/libraries', params)
return r.json()

42
src/fw_artists.py Normal file
View file

@ -0,0 +1,42 @@
from src.fw_api import get_artists, get_tracks, concatinate_endpoint
from src.fw_albums import list_albums
from src.mpv_control import player, player_menu
from pyfzf.pyfzf import FzfPrompt
from loguru import logger
fzf = FzfPrompt()
@logger.catch
def list_artists(pg=None):
artists = get_artists(pg=pg)
artists_next = artists.get('next')
artists_prev = artists.get('previous')
artists_results = artists.get('results')
view = []
if artists_next:
view.append('Next page')
if artists_prev:
view.append('Prev page')
for i in artists_results:
index = artists_results.index(i)
artist_name = i.get('name')
view.append(f'{index}.{artist_name}')
select = fzf.prompt(view)[0].split('.', 1)[0]
if select == 'Next page':
list_artists(pg=artists_next)
elif select == 'Prev page':
list_artists(pg=artists_prev)
else:
list_albums(albums=artists_results[int(select)].get('albums'))
def play_artist(artist_id):
tracks = get_tracks(artist=artist_id)
tracks_results = tracks.get('results')
storage = {}
for i in tracks_results:
listen_url = concatinate_endpoint(i.get('listen_url'))
storage[listen_url] = i
player.loadfile(listen_url, 'append-play')
player_menu("Artist playing...", storage)