-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
35 lines (32 loc) · 841 Bytes
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'sinatra'
require 'mechanize'
get '/' do
@lyrics = {}
haml :index
end
post '/' do
query = "lyrics site:metrolyrics.com #{params[:query]}"
agent = Mechanize::new
page = agent.get("http://www.google.com")
google_form = page.form('f')
google_form.q = query
page = agent.submit(google_form, google_form.buttons.first)
page = agent.get(page.at('#ires ol a').attribute('href'))
@title = page.title
chunks = page.at('#lyrics-body').to_html.split '<br>'
@lyrics = Hash.new { |l, k| l[k] = Hash.new(&l.default_proc) }
i = 0
chunks.each do |chunk|
l = 0
n = Nokogiri::HTML(chunk)
n.content.lines.each do |line|
@lyrics[i][l] = line.strip unless line.include? 'metrolyrics' || line.strip.empty?
l += 1
end
i += 1
end
haml :index
end
get '/views/style.css' do
sass :style
end