예제 #1
0
def fetch_playlist(playlist_id, allow_every=False):
    try:
        results = sp.playlist(playlist_id)
    except:
        print(f'Invalid PID: {playlist_id}')
        return False
    description, name, owner = results['description'], results[
        'name'], results['owner']['id']
    total_tracks = results['tracks']['total']
    desc_lemmas, desc_lang = lemmatize(description, return_lang=True)
    name_lemmas, name_lang = lemmatize(name, return_lang=True)
    valid_tracks = get_valid_tracks(sp, results['tracks'])
    playlist_length = len(valid_tracks)
    if not allow_every and (playlist_length > 2000 or playlist_length <= 1
                            or desc_lang != 'en' or name_lang != 'en'):
        print(f'{playlist_id} rejected.  Invalid # of tracks or non-english.')
        return False

    tracks_to_add = list()  # playlist tracks that don't exist in DB
    artists_to_add = set()
    playlist_tids = list(
    )  # playlist tracks that already exist or are initialized without error

    for tid, track in valid_tracks.items():
        if not db.track_exists(tid):
            try:
                track_data = initialize_track(track, playlist_id)
                artist_ids = track_data['artist_ids']
                for artist_id in artist_ids:
                    if not db.artist_exists(artist_id):
                        artists_to_add.add(artist_id)
                tracks_to_add.append(track_data)
                playlist_tids.append(tid)
            except:
                print(f'Error initializing track {tid}')
                continue
        else:
            playlist_tids.append(tid)
            db.add_pid_to_track(tid, playlist_id)

    tracks_to_add = add_audio_features(tracks_to_add)
    playlist_to_add = dict(_id=playlist_id,
                           name=name,
                           name_lemmas=name_lemmas,
                           owner=owner,
                           last_updated=datetime.datetime.now(),
                           description=description,
                           description_lemmas=desc_lemmas,
                           tids=playlist_tids)
    artists_to_add = fetch_artist_info(artists_to_add)
    db.insert_artists(artists_to_add)
    db.insert_tracks(tracks_to_add)
    db.insert_playlist(playlist_to_add)
    return True  # returns True if it successfully adds playlist to db, False otherwise
예제 #2
0
def fetch_playlist(sp, subset=False, playlist_id=None, library=False):
    if playlist_id:
        results = sp.playlist(playlist_id)
        valid_tracks = get_valid_tracks(sp, results['tracks'], subset=subset)
    elif library:
        results = sp.current_user_saved_tracks()
        valid_tracks = get_valid_tracks(sp, results, subset=subset)

    new_tracks = list()  # playlist tracks that don't exist in DB
    indices_to_add_new_tracks = list()
    new_artists = set()
    saved_tracks = list(
    )  # playlist tracks that already exist or are initialized without error

    for index, (tid, track) in enumerate(valid_tracks.items()):
        if not db.track_exists(tid):
            try:
                track_data = initialize_track(track)
                artist_id = track_data['artist_id']
                if not db.artist_exists(artist_id):
                    new_artists.add(artist_id)
                new_tracks.append(track_data)
                indices_to_add_new_tracks.append(index)
                saved_tracks.append(0)
            except:
                print(f'Error initializing track {tid}')
                continue
        else:
            track_data = db.get_track(tid)
            saved_tracks.append(track_data)
    new_tracks = parse_playlist.add_audio_features(new_tracks)
    # print(len(new_tracks), ' ', len(indices_to_add_new_tracks), ' ', len(saved_tracks))
    for track, index in zip(new_tracks, indices_to_add_new_tracks):
        saved_tracks[index] = track
    new_artists = parse_playlist.fetch_artist_info(new_artists)

    return saved_tracks, new_artists
예제 #3
0
def add_playlist(playlist_id):
    if playlist_id == '' or playlist_id == None:
        return None
    try:
        results = sp.user_playlist(playlist_id=playlist_id, user=None)
    except:
        print(f'cant get playlist {playlist_id}')
        return None

    description = results['description']
    name = results['name']
    owner = results['owner']['id']
    total_tracks = results['tracks']['total']
    desc_lemmas, desc_lang = lemmatize(description, return_lang=True)
    name_lemmas, name_lang = lemmatize(name, return_lang=True)

    playlist_tracks = parse_tracks(results, sp)
    playlist_length = len(playlist_tracks)
    if playlist_length > 1000 or playlist_length < 5 or desc_lang != 'en' or name_lang != 'en':
        print(f'Too many/few tracks or not english {playlist_id}')
        return None

    tracks_to_add = list()
    existing_tids = set()
    artists_to_add = set()

    for tid, track in playlist_tracks.items():
        if not db.track_exists(tid):
            try:
                track_data = initialize_track(track, playlist_id)
                artist_id = track_data['artist_id']
                if not db.artist_exists(artist_id):
                    artists_to_add.add(artist_id)
                tracks_to_add.append(track_data)
            except:
                print(f'Error initializing track {tid}')
                continue
        else:
            existing_tids.add(tid)
            tracks_db.update_one({'_id': tid},
                                 {'$push': {
                                     'pids': playlist_id
                                 }})

    tracks_to_add = add_audio_features(tracks_to_add)
    playlist_to_add = dict(_id=playlist_id,
                           name=name,
                           name_lemmas=name_lemmas,
                           owner=owner,
                           description=description,
                           description_lemmas=desc_lemmas,
                           tids=list())
    artists_to_add = list(artists_to_add)
    artists_to_add = add_genres(artists_to_add)
    if artists_to_add:
        try:
            artists_db.insert_many(artists_to_add)
        except BulkWriteError as bwe:
            for artist in artists_to_add:
                try:
                    artists_db.insert_one(artist)
                except:
                    print(f'cant insert artist {artist}')
    if tracks_to_add:
        try:
            tracks_db.insert_many(tracks_to_add)
            tids = [track['_id'] for track in tracks_to_add]
            playlist_to_add['tids'].extend(tids)
        except:
            for track in tracks_to_add:
                try:
                    tracks_db.insert_one(track)
                    playlist_to_add['tids'].append(track['_id'])
                except:
                    print(f'cannot insert track {track}')
    playlist_to_add['tids'].extend(list(existing_tids))
    playlists_db.insert_one(playlist_to_add)