Browse Source

Implement getting user's entire library

By passing 0 as num_tracks to parse_library.
master
Kevin Mok 7 years ago
parent
commit
a0a1b86164
  1. 19
      spotifyvis/utils.py
  2. 3
      spotifyvis/views.py

19
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

3
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 #

Loading…
Cancel
Save