-
-
Save acdha/3694087 to your computer and use it in GitHub Desktop.
Random Lyrics (Python3 port)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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