Objective: Looks up the Spotify HTTP link for each song in a text file, and outputs a CSV text file containing each song’s respective title, artist, and album.
Dependencies : Beautiful Soup 4
Limitations
- Does not accept Unicode characters at all. This appears to be a limitation with urllib2.
- I will try some other URL fetcher with Unicode support in the future.
- This means that currently the input file (e.g. “spotify_http_list.txt” below) must be formatted in ANSI or ASCII, or urllib2 will throw the error:
urllib2.URLError: <urlopen error unknown url type: http>
This shows up as
URLError: <urlopen error unknown url type: http>
in the debugger. - The song title(s), artist(s), and album name(s) must also not have Unicode characters in them either, or you get an error like
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 96: ordinal not in range(128)
.
- Spotify’s song info page via its HTTP link does not show more than two artists in a list of artists.
Notes
Copying and pasting the Spotify HTTP links directly into Microsoft Word result in hyperlinks showing up as {artist} – {title}, which is a much easier (and more accurate) way to export a Spotify playlist than the code here. The code here is thus mostly for educational purposes. (The code below does report album data, though!)
References : The snippet here is forked from another snippet by jclassi on GitHub, that exports Spotify playlists to Youtube.
Code
import urllib2 import argparse from bs4 import BeautifulSoup import csv csvheader = ("title", "artist", "album") def get_soup(url): page = urllib2.urlopen(url) return BeautifulSoup(page) def spotify_parse(url): soup = get_soup(url) title = soup.find("a", id="title").get_text() artist = soup.find("div", id="artist").get_text().strip() album = soup.find("div", id="album").get_text().strip() return title, artist, album def main(): parser = argparse.ArgumentParser(description = 'Export Spotify playlist to CSV.') parser.add_argument('-f', '--filename', help='File holding list of HTTP spotify URIs.', required = True) parser.add_argument('-o', '--output', help='CSV file containing title, artist, album.', required = True) args = vars(parser.parse_args()) f = open( args['output'], 'wb') c = csv.writer(f, delimiter = ',', quotechar = '"') with open( args['filename'], 'rb') as input: c.writerow(csvheader) for line in input: meta = spotify_parse(line) c.writerow(meta) if __name__ == "__main__": main()
Sample Input File
http://open.spotify.com/track/4FXj4ZKMO2dSkqiAhV7L8t http://open.spotify.com/track/411rcI6KFOx1BYw8HY4shd
Example Usage : python export-spotify-playlist-to-csv-with-csv-library.py -f spotify_http_list.txt -o playlist_metadata.csv
Sample Output
title,artist,album Evil Beauty,Blackmill,Reach For Glory Starscapes,"TwoThirds, Veela and 1 more.Feint",Starscapes Reprise - Original Mix,Feint,Feint EP1
Notes
- Without the CSV library, a single field containing commas (such as a list of artists) will be output to multiple fields.