Skip to content

Instantly share code, notes, and snippets.

@alobato
Forked from clarkware/lyrics.rb
Created September 14, 2012 00:47
Show Gist options
  • Save alobato/3719053 to your computer and use it in GitHub Desktop.
Save alobato/3719053 to your computer and use it in GitHub Desktop.
Random Lyrics
#!/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