Home > Software design >  Spotipy (sp.track() specifically) takes too long to run
Spotipy (sp.track() specifically) takes too long to run

Time:11-27

I am trying to extract the release data, explicit flag and popularity score of approximately 18,000 songs. I want to append these results to my data frame

Initially, I tried this. -

for i,track in enumerate(df['uri']):
     release_dates.append(sp.track(track)['album']['release_date']) 

But I took too long to run, so I assumed that it was the size of the dataset that was the problem.

Then I tried to run it over subsets of 50 songs each -

updated_popularity, explicit_flags, release_dates = [], [], []

for i in range(0,10000,50):
    print("entered first for loop")
    results = sp.track(df['uri'][i])
    print("got track results")
    for i, t in enumerate(results):
        print("Second loop: Track = ", t)
        updated_popularity.append(t['popularity'])
        explicit_flags.append(t['explicit'])
        release_dates.append(t['album']['release_date'])
    print("Exited second loop\n")

However, my code has been running for hours now with no results. I've been stuck on this for a while and any help would be appreciated!

CodePudding user response:

It's much faster to request 50 tracks at once with sp.tracks(uri_list)

# function to divide a list of uris (or ids) into chuncks of 50.
chunker = lambda y, x: [y[i : i   x] for i in range(0, len(y), x)]

# using the function
uri_chunks = chunker(uri_list, 50)

updated_popularity, explicit_flags, release_dates = [], [], []

for chunk in uri_chunks:
    print("entered first for loop")
    results = sp.tracks(chunk)
    print("got tracks results")
    for t in results["tracks"]:
        updated_popularity.append(t['popularity'])
        explicit_flags.append(t['explicit'])
        release_dates.append(t['album']['release_date'])
    print("Exited second loop\n")

print(updated_popularity)
  • Related