Created
September 11, 2011 21:14
-
-
Save brianstorti/1210137 to your computer and use it in GitHub Desktop.
Lyrics
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
USAGE | |
ruby lyrics.rb "Bob Dylan" "Tangled Up In Blue" |
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
# original author: https://github.com/fnando | |
require 'rubygems' | |
require "nokogiri" | |
require "cgi" | |
require "open-uri" | |
class Lyrics | |
def self.fetch(artist, song) | |
lyric = Lyrics.new(artist, song) | |
lyric.body | |
end | |
attr_accessor :artist, :song, :xml, :html | |
def initialize(artist, song) | |
@artist = artist | |
@song = song | |
@data = nil | |
end | |
def body | |
@fetch = begin | |
url = "http://lyrics.wikia.com/api.php?artist=#{CGI.escape(artist)}&song=#{CGI.escape(song)}&fmt=xml" | |
@xml = Nokogiri::XML(open(url).read) | |
url = xml.css("LyricsResult > url").inner_text | |
@html = Nokogiri::HTML(open(url).read) | |
div = html.css("div.lyricbox") | |
div.css("div.rtMatcher").remove | |
content = div.inner_html | |
content.gsub!(/<!--.*?-->/xim, "") | |
content.gsub!(/<\/?([a-z]+).*?>/xim, "\n") | |
content.gsub!(/\n{2,}/, "\n\n") | |
content.match(/we are not licensed to display the full lyrics/) ? nil : content | |
end | |
rescue Exception | |
nil | |
end | |
end | |
artist, song = ARGV | |
lyrics = Lyrics.fetch(artist, song) | |
puts lyrics ? lyrics : "No lyrics found" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment