Created
September 10, 2012 20:05
-
-
Save clarkware/3693483 to your computer and use it in GitHub Desktop.
Random Lyrics
This file contains 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 ruby | |
# API Documentation at http://api.wikia.com/wiki/LyricWiki_API | |
require 'open-uri' | |
require 'json' | |
require 'tmpdir' | |
ARTIST = "Johnny Cash" | |
def json_for(name, url) | |
temp_file = File.join(Dir.tmpdir, name) | |
if File.exists?(temp_file) | |
data = File.read(temp_file) | |
else | |
data = open(URI.escape(url)).read | |
File.open(temp_file, "w") { |f| f.puts data } | |
end | |
JSON.parse(data) | |
end | |
artist_json = json_for(ARTIST, "http://lyrics.wikia.com/api.php?artist=#{ARTIST}&fmt=realjson") | |
songs = artist_json["albums"].map { |a| a["songs"] } | |
song = songs.flatten.sample | |
song_json = json_for(song, "http://lyrics.wikia.com/api.php?artist=#{ARTIST}&song=#{song}&fmt=realjson") | |
puts song_json["lyrics"].gsub("\n", ", ").squeeze |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment