예제 #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
파일: track.py 프로젝트: kamath/PyRate
 def post_clean(obj, to_connect: dict) -> StructuredNode:
     # Associate Spotify
     if 'spotify_data' in to_connect:
         spotify: SpotifyTrack = SpotifyTrack.inst(
             **to_connect['spotify_data'])
         spotify.genius_data.connect(obj)
     return obj
예제 #3
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)
예제 #4
0
    def get_minibatches(stopval, offset=0, interval=500):
        while offset < stopval:
            to_analyze: List[Track] = Track.get_songs_not_in_playlists(
                interval, offset=offset)
            yield to_analyze

            offset += len(to_analyze)
            print(f'{offset}/{stopval}')
예제 #5
0
def main():
    from model.conv_autoencoder import Autoencoder

    from neomodel import config, db
    import pandas as pd
    import os
    import numpy as np
    from ast import literal_eval as eval
    from tqdm import tqdm

    from model.graph import connection_url
    from model.graph.spotify.track import Track
    from model.graph.spotify.playlist import Playlist

    config.DATABASE_URL = connection_url()
    db.set_connection(connection_url())

    # Segment analysis
    def segment_to_vector(segment):
        return list(map(abs, [segment['duration'], segment['loudness_start'], segment['loudness_max_time'],
                              segment['loudness_max']] \
                        + segment['pitches'] + segment['timbre']))

    stopval = len(Track.nodes)
    print('Number of tracks:', stopval)
    print('Number of playlists:', len(Playlist.nodes))

    def get_minibatches(stopval, offset=0, interval=500):
        while offset < stopval:
            to_analyze: List[Track] = Track.get_songs_not_in_playlists(
                interval, offset=offset)
            yield to_analyze

            offset += len(to_analyze)
            print(f'{offset}/{stopval}')

    for i, x in enumerate(get_minibatches(stopval)):
        tracks = x
        X = [a.analysis['segments'] for a in x if a.analysis]
        arrs = [list(map(segment_to_vector, sample)) for sample in X]
        print(tracks)
        latest = list(
            filter(lambda x: 'segment_analysis' in x, os.listdir('models')))
        if latest:
            latest = os.path.join('models', max(latest))
        else:
            latest = None

        to_pad = 800
        arrs = Autoencoder.pad_and_normalize(arrs, to_pad=to_pad)
        auto = Autoencoder.train('segment_analysis',
                                 arrs,
                                 weights_path=latest,
                                 epochs=10)
        df = pd.DataFrame({'uri': [a.uri for a in tracks]})
        df = Autoencoder.store_zVectors(auto, arrs, df)
        for i, row in tqdm(df.iterrows()):
            track = Track.add_zVector(row['uri'], row['zVector'].tolist())
예제 #6
0
파일: server.py 프로젝트: kamath/PyRate
def combine_artists():
    artists = request.args.get('artists').split(',')
    context = request.args.get('context_uri')
    tracks = Artist.get_tracks_with_multiple_artists(context, *artists)
    print('Common tracks', tracks)
    if len(tracks):
        to_play = randint(0, len(tracks) - 1)
        album = Track.get_album(tracks[to_play].spotify_id)[0]
        print(album.name, tracks[to_play].track_number)
        tor = Spotify.play(context_uri=album.uri, offset={'position':  tracks[to_play].track_number - 1})
    return redirect('/')
예제 #7
0
파일: artist.py 프로젝트: kamath/PyRate
    def get_tracks_with_multiple_artists(cls, context, *artist_ids):
        '''
        Gets tracks with multiple artists on it

        :param artist_ids: the given artist IDs
        :param context: the Spotify ID of the playlist
        :return:
        '''
        artists = ','.join([
            "(a%s:Artist { uri: '%s' })" % (i, a)
            for i, a in enumerate(artist_ids)
        ])
        query_constructor = '''MATCH %s, 
        p = allShortestPaths((a0)-[*]-(a1))
        WHERE EXISTS((a0) <-[:BY]- (:Track) -[:`FEATURED IN`]-> (:Playlist {spotify_id: "%s"}))
        RETURN nodes(p)
        ''' % (artists, context)
        from model.graph.spotify.track import Track
        db.set_connection(connection_url())
        results, meta = db.cypher_query(query_constructor)
        return [Track.inflate(result[0][1]) for result in results]
예제 #8
0
def main():
    '''
    Gets all the albums in the database, loops through associated tracks, and searches Genius
        - if Spotify URI is included in Genius data, the node is updated to include Genius data
    :return:
    '''
    from time import sleep

    from neomodel import config

    from scraper.genius import Genius

    from model.graph import connection_url
    from model.graph.spotify.track import Track
    from model.graph.spotify.album import Album

    config.DATABASE_URL = connection_url()
    print(len(Album.nodes))
    for i, node in enumerate(Album.nodes):
        if i > 5:
            break

        print('\n\nCurrent Album:', node.name)
        for track in node.tracks:
            to_search = f'{track.name} {track.artists[0].name}'
            print('\nSearching for', to_search)
            resp = Genius.search(to_search)
            print(resp)
            if resp['meta']['status'] == 200:
                resp = resp['response']
                if 'hits' in resp:
                    for hit in resp['hits']:
                        song_data = Genius.get_song(
                            hit['result']['id'])['response']['song']
                        print(song_data['media'])
                        if 'spotify' in [
                                a['provider'] for a in song_data['media']
                        ]:
                            print('Spotify exists!')
                            for i, a in enumerate(song_data['media']):
                                print(a)
                                if a['provider'] == 'spotify':
                                    in_db = Track.nodes.get_or_none(
                                        uri=song_data['media'][i]
                                        ['native_uri'])
                                    if in_db:
                                        print('Track exists:', in_db.name)
                                    else:
                                        print('Track is not in database')
                                    Track.add_genius(
                                        song_data['media'][i]['native_uri'],
                                        song_data)
                                    new_track = Track.nodes.get_or_none(
                                        uri=song_data['media'][i]
                                        ['native_uri'])
                                    if new_track:
                                        print('Track updated',
                                              new_track.genius_data)
                                    else:
                                        print('Track not updated', new_track)
                        sleep(1)