def api_v1_song_search(): plman = app.bard.playlist_manager sq = SearchQuery.from_request(request, current_user.userID, plman) if not sq: raise ValueError('No SearchQuery!') pl = plman.get_search_result_playlist(sq) if not pl: pl = SearchPlaylist(sq) plman.add_search_playlist(pl) songs = MusicBrainzDatabase.search_songs_for_webui(sq.query, sq.offset, sq.page_size) song_ids = [song['song_id'] for song in songs] for song_id in song_ids: pl.append_song(song_id) ratings = MusicDatabase.get_songs_ratings(song_ids, current_user.userID) songs = [{ 'rating': ratings[song['song_id']], **dict(song) } for song in songs] result = { 'search_playlist_id': pl.searchPlaylistID, 'search_query': sq.as_dict(), 'songs': songs } return jsonify(result)
def playlist_tracks(): if request.method != 'GET': return None playlistID = request.args.get('id', type=int) songs = MusicBrainzDatabase.get_playlist_songs_information_for_webui( playlistID) song_ids = [song['song_id'] for song in songs] ratings = MusicDatabase.get_songs_ratings(song_ids, current_user.userID) result = [{ 'rating': ratings[song['song_id']], **dict(song) } for song in songs] return jsonify(result)
def album_tracks(): if request.method != 'GET': return None albumID = request.args.get('id', type=int) # songIDs = {x['releasetrackid']: x['song_id'] # for x in MusicBrainzDatabase.get_album_songs(albumID)} all_tracks = MusicBrainzDatabase.get_album_tracks(albumID) existing_tracks = MusicBrainzDatabase. \ get_album_songs_information_for_webui(albumID) song_ids = [track['song_id'] for track in existing_tracks] ratings = MusicDatabase.get_songs_ratings(song_ids, current_user.userID) existing_tracks = { x['track_mbid']: { 'rating': ratings[x['song_id']], **dict(x) } for x in existing_tracks } result = [] medium = {'number': None, 'tracks': []} current_medium_number = None for track in all_tracks: if track['medium_number'] != current_medium_number: if current_medium_number: result.append(medium) current_medium_number = track['medium_number'] format_id = track['medium_format_id'] medium_format_name = (MediumFormatEnum.name(format_id) or 'Unknown Format') medium = { 'number': track['medium_number'], 'name': track['medium_name'], 'format': medium_format_name, 'tracks': [] } try: medium['tracks'].append(existing_tracks[track['track_mbid']]) except KeyError: trk = dict(track) trk['song_id'] = None trk['rating'] = (5, None) medium['tracks'].append(trk) result.append(medium) return jsonify(result)