diff --git a/spotifyvis/utils.py b/spotifyvis/utils.py index dd0b087..c459904 100644 --- a/spotifyvis/utils.py +++ b/spotifyvis/utils.py @@ -23,31 +23,32 @@ FEATURES_LIMIT = 100 # parse_library {{{ # -def parse_library(headers, tracks, user): - """Scans user's library for certain number of tracks and store the information in a database +def parse_library(headers, num_tracks, user): + """Scans user's library for num_tracks and store the information in a database :headers: For API call. - :tracks: Number of tracks to get from user's library. + :num_tracks: Number of tracks to get from user's library (0 scans the entire + library). :user: a User object representing the user whose library we are parsing :returns: None """ - # TODO: implement importing entire library with 0 as tracks param - # keeps track of point to get songs from offset = 0 payload = {'limit': str(USER_TRACKS_LIMIT)} artist_genre_queue = [] features_queue = [] - # iterate until hit requested num of tracks - for i in range(0, tracks, USER_TRACKS_LIMIT): + # create this obj so loop runs at least once + saved_tracks_response = [0] + # scan until reach num_tracks or no tracks left if scanning entire library + while (num_tracks == 0 or offset < num_tracks) and len(saved_tracks_response) > 0: payload['offset'] = str(offset) saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks', headers=headers, - params=payload).json() + params=payload).json()['items'] - for track_dict in saved_tracks_response['items']: + for track_dict in saved_tracks_response: # add artists {{{ # # update artist info before track so that Track object can reference diff --git a/spotifyvis/views.py b/spotifyvis/views.py index dc2d708..ed25b54 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -21,7 +21,8 @@ from .models import User, Track, AudioFeatures, Artist # global vars {{{ # TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' -TRACKS_TO_QUERY = 200 +# TRACKS_TO_QUERY = 200 +TRACKS_TO_QUERY = 0 # }}} global vars #