« Back

Spotify Track IDs

Find out the artist and title from Spotify's IDs over the API.

A few days ago I got a list of songs from Spotify, in this weird format:

spotify:track:1AhDOtG9vPSOmsWgNW0BEY

The list was very long and I didn’t want to enter every single ID in the Spotify search to find out which artist and title is behind it. The first thing that came to my mind was to have a look at the Spotify API. I was surprised, because I don’t even need a developer account, token or anything else. So I wrote a little python script.

Example: https://api.spotify.com/v1/tracks/1AhDOtG9vPSOmsWgNW0BEY

#!/usr/bin/env python3
# -*- coding: iso-8859-1 -*-

import sys, requests, os

URL = 'https://api.spotify.com/v1/tracks/{trackId}'

def main(filename):
    if os.path.isfile(filename) and os.access(filename, os.R_OK):
        f = open(filename, 'r')
        for line in f:
            line = line.strip()
            split = line.split(':', 2)

            r = requests.get(URL.format(trackId=split[2]))
            json = r.json()

            for artist in json['artists']:
                name = artist

            print('{artist} - {track}'.format(
                artist=name['name'],
                track=json['name'])
            )

        f.close()
    else:
        print("Filename: {filename} doesn't exists or not readable".format(filename=filename));
if __name__=="__main__":
    if len(sys.argv) > 1:
        main(sys.argv[1])
    else:
         print("Usage: \n\t ./spotify-track-id /path/to/file")

Example:

$ ./spotify-track-id /path/to/file
ARTIST1 - TRACK1
ARTIST2 - TRACK2
...

File format

spotify:track:1AhDOtG9vPSOmsWgNW0BEY
spotify:track:51pQ7vY7WXzxskwloaeqyj
spotify:track:21cp8L9Pei4AgysZVihjSv
spotify:track:1ZA8L9IOkRhmmz4nPXXx7h
spotify:track:3UOrKWXdwUVtDglahh3OQj
spotify:track:1wHSxWBfPr3GO31y8KGrWe

Output

Queen - Bohemian Rhapsody - Remastered 2011
Led Zeppelin - Stairway To Heaven
Deep Purple - Child In Time
Eagles - Hotel California
Dire Straits - Brothers In Arms
Pink Floyd - Wish You Were Here - 2011 Remastered Version