Skip to content

Instantly share code, notes, and snippets.

@dfm
Created May 11, 2014 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfm/035b00a4f559ea2834af to your computer and use it in GitHub Desktop.
Save dfm/035b00a4f559ea2834af to your computer and use it in GitHub Desktop.
Make a spotify playlist out of a Wikipedia list
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import spotify
import requests
from bs4 import BeautifulSoup
p = re.compile(r"\"(.*?)\"(.*?)(?:(?: by (.*))|$)", re.I)
# Log into Spotify.
session = spotify.Session()
session.login("YOUR_SPOTIFY_USERNAME", "YOUR_SPOTIFY_PASSWORD")
while True:
session.process_events()
if session.connection.state is spotify.ConnectionState.LOGGED_IN:
break
# Set up the playlist.
name = "List of songs about New York City"
playlist = None
for pl in session.playlist_container.load():
if pl.load().name == name:
playlist = pl
break
if playlist is None:
print("Creating new playlist")
playlist = session.playlist_container.add_new_playlist(name)
# Download the list.
url = "http://en.wikipedia.org/wiki/{0}".format(name.replace(" ", "_"))
r = requests.get(url)
if r.status_code != requests.codes.ok:
r.raise_for_status()
# Parse the HTML and loop over list items.
txt = r.content
tree = BeautifulSoup(txt, "html5lib")
fn = "failures.txt"
open(fn, "w").close()
count = 0
for i, el in enumerate(tree.find(id="mw-content-text").find_all("li")):
# Parse the element and skip if it doesn't match.
results = p.findall(el.text)
if len(results) != 1:
continue
# Skip if there is no title result.
result = results[0]
if len(result[0]) == 0:
continue
# Build the query.
result = map(lambda s: s.strip(), result)
q = u" ".join(result[:2]).strip().replace(u": ", u" ")
if len(result[2]):
q += u" artist:\"{0}\"".format(result[2])
# Run the spotify query.
search = session.search(q).load()
if not len(search.tracks):
print(u"No results for: {0}".format(q))
with open(fn, "a") as f:
f.write(u"{0}\n".format(q).encode("utf-8"))
continue
# Add the track to the playlist.
track = search.tracks[0].load()
playlist.add_tracks(track)
session.process_events()
count += 1
while playlist.has_pending_changes:
session.process_events()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment