Created
August 31, 2012 17:50
-
-
Save siavashs/3556469 to your computer and use it in GitHub Desktop.
Ruby script to fetch lyrics from Lyric Wiki
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
# lyrics.rb: Ruby script to fetch lyrics from Lyric Wiki | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# Copyright 2012 Siavash Safi | |
# | |
# Important Notes: | |
# * This is my "Hello World!" in ruby! | |
# * I did it just for fun and because I hate to visit wiki pages to read lyrics. | |
# * I'm not responsible for what you do with this script! | |
# * Enjoy! :) | |
require 'optparse' | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'uri' | |
options = {} | |
optparser = OptionParser.new do |opts| | |
opts.banner = "Usage: lyrics.rb --artist=ARTIST --song=SONG" | |
options[:artist] = nil | |
opts.on( '-a', '--artist ARTIST', 'Set the artist' ) do |artist| | |
options[:artist] = artist | |
end | |
options[:song] = nil | |
opts.on( '-s', '--song SONG', 'Set the song' ) do |song| | |
options[:song] = song | |
end | |
opts.on( '-h', '--help', 'Display this screen' ) do | |
puts opts | |
exit | |
end | |
end | |
optparser.parse! | |
if options[:artist] == nil || options[:song] == nil | |
puts optparser.help | |
exit 1 | |
end | |
def download(url) | |
begin | |
return open(url) | |
rescue OpenURI::HTTPError => e | |
puts 'Failed to open: ' + url | |
puts 'HTTP Error: ' + e.message | |
exit 1 | |
end | |
end | |
# Generate API URL | |
url = URI::escape('http://lyrics.wikia.com/api.php?artist=' + options[:artist] + '&song=' + options[:song] + '&fmt=xml') | |
# Call the API to get the lyrics url | |
xml = Nokogiri::XML(download(url)) | |
# Extract lyrics url from XML document | |
url = xml.root.at('//LyricsResult//url').content | |
# Fetch the wiki page | |
wiki = Nokogiri::HTML(download(url)) | |
# Remove comments | |
wiki.xpath('//comment()').remove | |
# Extract lyricbox | |
lyricbox = wiki.root.at('//body').at_css('div.lyricbox') | |
# Find and remove ringtone matcher | |
lyricbox.css('div.rtMatcher').remove | |
# Print each line separatly so the lyrics are more readable | |
lyricbox.children.each do |child| | |
puts child.text | |
end | |
puts 'Lyrics from Lyric Wiki: ' + url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment