示例#1
0
    def get_minibatches(stopval, count=0, interval=20):
        while count < stopval:
            to_analyze: List[Track] = Track.get_songs_in_playlists(
                interval, count)

            X = [a.get_song_features(as_list=True) for a in to_analyze]
            y = [[playlists[x.uri] for x in Track.get_playlists(a.spotify_id)]
                 for a in to_analyze]
            print(count, interval)
            yield np.array(list(
                map(lambda x: list(map(abs, x)),
                    X))), MultiLabelBinarizer().fit_transform(y)
            count += len(to_analyze)
示例#2
0
文件: server.py 项目: kamath/PyRate
def hello_world():
    start = time()
    spotify_data = Spotify.get_currently_playing()
    playlists = Track.get_playlists(spotify_data['item']['id'])

    print('Currently analyzing:', spotify_data['item']['name'])
    playlist_id = spotify_data['context']['uri'].split(':')[-1]
    playlist_tracks = Playlist.get_tracks(playlist_id)
    print('Fetched tracks in playlist')
    print('Time taken:', time() - start)
    arrs = [json.loads(a.zVector)['zVector'] for a in playlist_tracks]
    arrs = np.array(arrs)
    print('Arrs shape:', arrs.shape)

    track = Track.find(uri=spotify_data['item']['uri'])[0]
    ll = []
    for i in arrs:
        ll.append(create_cosine_similarity(track.zVector['zVector'], i))
    tracks = list(sorted(zip(playlist_tracks, ll), key=lambda x: x[1]))
    tracks = tracks[:5] + tracks[-6:-1]
    track_images = Spotify.get_images(*[a[0].uri for a in tracks])
    print('Most similar')
    for a in tracks[:5]:
        print(a[0].name)
    print('Time taken to get similar:', time() - start)

    similar_playlists = Track.get_similar_playlists(spotify_data['item']['id'],
                                            spotify_data['context']['uri'].split(':')[-1])

    similar_playlists = set(similar_playlists)

    associated_artists = Artist.get_associated_artists(spotify_data['item']['artists'][0]['id'])

    if len(associated_artists):
        artist_data = Spotify.get_artists([artist.uri for artist in associated_artists])

        for i in range(len(artist_data)):
            if len(artist_data[i]['images']) == 0:
                artist_data[i]['images'] = [{'url': '/static/stock_anon.png'}]

    else:
        artist_data = []

    print('Total time', time()-start)
    return render_template('home.html', similar_tracks=list(zip(tracks, track_images)), spotify_data=spotify_data, playlists=playlists,
                           similar_playlists=similar_playlists, artist_data=artist_data)