local http = require("socket.http")

function makeRequest(artist, song)
       local webPage, statusCode =
http.request("http://www.metrolyrics.com/" .. song .. "-lyrics-" .. artist
. ".html")
       if statusCode == 200 then return webPage else print("Música não
encontrada") os.exit() end
end

function concat(str)
       local fstr = ''
       for ech in string.gmatch(str,"%Z") do
               if ech == " " then
                       fstr = fstr .. "-"
               else
                       fstr = fstr .. ech
               end
       end
       return string.lower(fstr)
end

function textOnly(verses)
       local txt = ''
       local ftxt = ''
       for initP in string.gmatch(verses, ">%Z-<") do
               txt = txt .. initP
       end
       for tag in string.gmatch(txt, "%Z") do
               if tag == "<" then
                       ftxt = ftxt .. "\n"
               elseif tag == ">" then
                       ftxt = ftxt .. ""
               else
                       ftxt = ftxt .. tag
               end
       end
       return ftxt
end

function noEnter(text)
       local ans = ''
       for n in string.gmatch(text, "%Z%Z") do
               if n == "\n\n" then
                       ans = ans .. "\n"
               else
                       ans = ans .. n
               end
       end
       return ans
end

function parseLyrics(entPage)
       local verses = ''
       for verse in string.gmatch(entPage, "<p class='verse'>%Z-</p>") do
               verses = verses .. verse
       end
       return noEnter(textOnly(verses))
end

io.write("Insira o artista: ")
local artist = io.read()
io.write("Insira a música: ")
local song = io.read()
if artist and song then
       print(parseLyrics(makeRequest(concat(artist), concat(song))))
end