Skip to content

Instantly share code, notes, and snippets.

@acdha
Forked from clarkware/lyrics.rb
Created September 10, 2012 21:39
Show Gist options
  • Save acdha/3694087 to your computer and use it in GitHub Desktop.
Save acdha/3694087 to your computer and use it in GitHub Desktop.
Random Lyrics (Python3 port)
#!/usr/bin/env python3
# Based on https://gist.github.com/3693483, converted to Python 3 for a zero-install experience
# API Documentation at http://api.wikia.com/wiki/LyricWiki_API
from urllib.parse import urlencode
from urllib.request import urlopen
import json
import random
import sys
ARTISTS = [
'Césaria Évora',
'Iron & Wine',
'Johnny Cash',
'mûm',
'Over The Rhine',
'Radiohead',
'sigur rós',
'The Decemberists',
'Tom McRae',
]
random.shuffle(ARTISTS)
# We'll try in order until we find some lyrics
for artist in ARTISTS:
artist_data = urlopen("http://lyrics.wikia.com/api.php?" +
urlencode({'artist': artist,
'fmt': 'realjson'}))
artist_json = json.loads(artist_data.read().decode("utf-8"))
songs = []
for album in artist_json['albums']:
songs.extend(album['songs'])
if not songs:
continue
song = random.choice(songs)
song_data = urlopen("http://lyrics.wikia.com/api.php?" +
urlencode({'artist': artist,
'song': song,
'fmt': 'text'}))
print(song_data.read().decode("utf-8"))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment