Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Created May 24, 2013 20:58
Show Gist options
  • Save RoxasShadow/5646474 to your computer and use it in GitHub Desktop.
Save RoxasShadow/5646474 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
class String
def numeric?
self.to_i.to_s == self || self.to_f.to_s == self
end
end
module Lyrics
def self.search(query, skip = 1)
i = 1
url = URI.parse(URI.encode('http://canzonimetal.altervista.org/?s="' + query + '"&x=0&y=0'))
Nokogiri::HTML(open(url)).xpath('//a[@id="featured-thumbnail"]/@href').each { |u|
if skip <= i
return u.to_s
else
i += 1
end
}
nil
end
def self.get(url)
contents = { :url => url }
contents[:who] = ''.tap { |s|
Nokogiri::HTML(open(url)).xpath('//div[@class="post-content box mark-links"]/ul/li').each { |u|
s << u.content.tr('^A-Za-z0-9 ', '').gsub(/\s\s/, ' ') + "\n"
}
}
contents[:en] = ''.tap { |s|
Nokogiri::HTML(open(url)).xpath('//div[@class="texten"]/p').each { |u|
s << u.to_s.gsub(/\<br\>/m, "").gsub(/\<p>/m, "\n").gsub(/\<\/p>/m, "\n")
}
}
contents[:it] = ''.tap { |s|
Nokogiri::HTML(open(url)).xpath('//div[@class="textit"]/p').each { |u|
s << u.to_s.gsub(/\<br\>/m, "").gsub(/\<p>/m, "\n").gsub(/\<\/p>/m, "\n")
}
}
contents
end
end
abort 'Title required' if ARGV.empty?
title = ARGV[0]
skip = (!ARGV[1].nil? && ARGV[1].numeric?) ? ARGV[1].to_i : 1
url = Lyrics.search title, skip
abort 'Not found.' if url.nil?
contents = Lyrics.get url
puts contents[:url]
puts
puts ?- * 50
puts contents[:en]
puts
puts ?- * 50
puts contents[:it]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment