[GH-ISSUE #472] Is there a way to get the time a track was added to a playlist? #277

Closed
opened 2026-02-27 23:21:45 +03:00 by kerem · 4 comments
Owner

Originally created by @eliasbenb on GitHub (Apr 14, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/472

Just as the title says, is there a way to get the time a track was added to a playlist? Any help is appreciated. Also this library is amazing!

I'm looking for a port of this: https://developer.spotify.com/documentation/web-api/reference/playlists/reorder-playlists-tracks/

Originally created by @eliasbenb on GitHub (Apr 14, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/472 Just as the title says, is there a way to get the time a track was added to a playlist? Any help is appreciated. Also this library is amazing! I'm looking for a port of this: https://developer.spotify.com/documentation/web-api/reference/playlists/reorder-playlists-tracks/
kerem closed this issue 2026-02-27 23:21:46 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Apr 14, 2020):

Hi @eliasbenb,

Any field you see in the official Spotify web-api doc should be returned by spotipy. I can see a field called added_at in https://developer.spotify.com/documentation/web-api/reference-beta/#category-playlists

Feel free to close the issue if that answers your question!

<!-- gh-comment-id:613501791 --> @stephanebruckert commented on GitHub (Apr 14, 2020): Hi @eliasbenb, Any field you see in the official Spotify web-api doc should be returned by spotipy. I can see a field called `added_at` in https://developer.spotify.com/documentation/web-api/reference-beta/#category-playlists Feel free to close the issue if that answers your question!
Author
Owner

@eliasbenb commented on GitHub (Apr 14, 2020):

Ok, sorry, but I've been trying to use the added_at tag but I can't get it to work.

This is the code I've been using: print(playlist_tracks['items']['added_at']) I'm trying to print the time a track in a playlist was added, I get this output: print(playlist_tracks['items']['added_at']) TypeError: list indices must be integers or slices, not str

This is my full code:

import json, os, spotipy, sys
import spotipy.util as util

spotipy_client_id = '...'
spotipy_client_secret = '...'
spotipy_redirect_uri = 'http://example.org/callback'
scope = 'user-library-read playlist-modify-public user-read-private user-read-email'

username = input("Enter yout spotify login: ")
token = util.prompt_for_user_token(username, scope, spotipy_client_id, spotipy_client_secret, spotipy_redirect_uri)

def get_playlist_tracks(_playlist_owner_id, _playlist_id, _playlist_total_tracks):
    tracks = []
    tracks.append(sp.user_playlist_tracks(_playlist_owner_id, _playlist_id, offset = 100, limit = 100))
    return tracks

if token:
    sp = spotipy.Spotify(auth=token)
else:
    print("Error")

playlists = sp.current_user_playlists()
n = 1
for playlist in playlists['items']:
    print(str(n) + ". " + playlist['name'])
    n = n + 1
playlist_choice = int(input("Which playlist would you like to sort?: "))
playlist_id = playlists['items'][playlist_choice]['name']
if playlists['items'][playlist_choice]['owner']['id'] != sp.current_user()['id']:
    print(playlists['items'][playlist_choice]['owner']['id'])
    print(sp.current_user()['id'])
    print("You do not have permission to edit this playlist")
else:
    print("You chose:", playlist_id)
    playlist_tracks = get_playlist_tracks(playlists['items'][playlist_choice]['owner']['id'], playlists['items'][playlist_choice]['id'], playlists['items'][playlist_choice]['tracks']['total'])
    print(playlist_tracks['items']['added_at'])
<!-- gh-comment-id:613564201 --> @eliasbenb commented on GitHub (Apr 14, 2020): Ok, sorry, but I've been trying to use the `added_at` tag but I can't get it to work. This is the code I've been using: `print(playlist_tracks['items']['added_at'])` I'm trying to print the time a track in a playlist was added, I get this output: `print(playlist_tracks['items']['added_at']) TypeError: list indices must be integers or slices, not str` This is my full code: ``` import json, os, spotipy, sys import spotipy.util as util spotipy_client_id = '...' spotipy_client_secret = '...' spotipy_redirect_uri = 'http://example.org/callback' scope = 'user-library-read playlist-modify-public user-read-private user-read-email' username = input("Enter yout spotify login: ") token = util.prompt_for_user_token(username, scope, spotipy_client_id, spotipy_client_secret, spotipy_redirect_uri) def get_playlist_tracks(_playlist_owner_id, _playlist_id, _playlist_total_tracks): tracks = [] tracks.append(sp.user_playlist_tracks(_playlist_owner_id, _playlist_id, offset = 100, limit = 100)) return tracks if token: sp = spotipy.Spotify(auth=token) else: print("Error") playlists = sp.current_user_playlists() n = 1 for playlist in playlists['items']: print(str(n) + ". " + playlist['name']) n = n + 1 playlist_choice = int(input("Which playlist would you like to sort?: ")) playlist_id = playlists['items'][playlist_choice]['name'] if playlists['items'][playlist_choice]['owner']['id'] != sp.current_user()['id']: print(playlists['items'][playlist_choice]['owner']['id']) print(sp.current_user()['id']) print("You do not have permission to edit this playlist") else: print("You chose:", playlist_id) playlist_tracks = get_playlist_tracks(playlists['items'][playlist_choice]['owner']['id'], playlists['items'][playlist_choice]['id'], playlists['items'][playlist_choice]['tracks']['total']) print(playlist_tracks['items']['added_at']) ```
Author
Owner

@stephanebruckert commented on GitHub (Apr 14, 2020):

Use pprint to print the content of your variables:

from pprint import pprint
pprint(playlist_tracks['items'])

Is added_at in there?

<!-- gh-comment-id:613593131 --> @stephanebruckert commented on GitHub (Apr 14, 2020): Use `pprint` to print the content of your variables: from pprint import pprint pprint(playlist_tracks['items']) Is `added_at` in there?
Author
Owner

@eliasbenb commented on GitHub (Apr 16, 2020):

OK, I was able to get this working by using this. Thank you for your help.

offset = 0
while playlists['items'][playlist_choice]['tracks']['total'] > offset:
    added_ats = sp.playlist_tracks(playlist_id,
                                offset=offset,
                                fields='items.added_at')
<!-- gh-comment-id:614416407 --> @eliasbenb commented on GitHub (Apr 16, 2020): OK, I was able to get this working by using this. Thank you for your help. ``` offset = 0 while playlists['items'][playlist_choice]['tracks']['total'] > offset: added_ats = sp.playlist_tracks(playlist_id, offset=offset, fields='items.added_at') ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#277
No description provided.