#!/usr/bin/ruby
# Copyright (c) 2007, Mirco Macrelli
# All rights reserved.
require 'net/http'
def clean_string(s)
s.chop.downcase.gsub(/[\(\)\'\^\/']/, '').gsub(/ /, '-')
end
def prepare_text_simple(text)
text.gsub!(/\[.+ on http\:\/\/www\.metrolyrics\.com\]/, '')
text.gsub!(/\
/, '
')
text.gsub!(/^[\t]+/, '')
text.gsub!(/^[\ ]+/, '')
return text.slice(0, text.length - 3)
end
def prepare_text_complex(text)
p = false
count_down = 7
t = ""
text.each_line do |line|
p = true if line.include? '
'
p = false if line.include? '
'
count_down -= 1 if p and count_down > 0
t += line.gsub(/\
/, '') if p and count_down == 0
end
t.gsub!(/&/, '&')
return t.slice(0, t.length - 3)
end
artist = ''
song = ''
second_arg = false
ARGV.each do |arg|
second_arg = true if arg.include? '^'
if second_arg
song += "#{arg} "
else
artist += "#{arg} "
end
end
artist = clean_string artist
song = clean_string song
file_name = "/#{song}-lyrics-#{artist}.html"
Net::HTTP.start 'www.metrolyrics.com' do |http|
response = http.head file_name
if response.code == '200'
response = http.get file_name
text = response.body.match(/\t\t[\w].+\
/).to_s
if text.include? 'All lyrics provided for educational'
puts prepare_text_complex(response.body)
else
puts prepare_text_simple(text)
end
end
end