views:

56

answers:

1

Anyone have a Ruby script to grab some lyrics from somewhere?

+1  A: 
require 'cgi'
require 'nokogiri'
require 'open-uri'

class Lyrics
  def search(query="")
    suggestions = open("http://lyrics.wikia.com/index.php?action=ajax&rs=getLinkSuggest&format=json&query=#{CGI.escape(query)}")
    json = ActiveSupport::JSON.decode(suggestions.string)
    lyrics_html = Nokogiri::HTML(open("http://lyrics.wikia.com/wiki/#{json["suggestions"].first.gsub(/\s/, "_")}"))
    lyricbox_div = lyrics_html.at_css('div.lyricbox')
    lyricbox_div.css(".rtMatcher").remove
    puts lyricbox_div.inner_html
  end
end
tony