This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.rb
53 lines (43 loc) · 1.45 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'sinatra'
require 'sinatra/reloader'
require 'metainspector'
require 'open-uri'
set :show_exceptions, :after_handler
helpers do
def show_if_present(title, value)
"<h3>#{title}</h3><p>#{value}</p>" unless value.nil? || value.length == 0
end
def link_if_present(title, value)
show_if_present(title, "<a href='#{value}'>#{value}</a>") unless value.nil? || value.length == 0
end
def paint_images(images)
images.map { |image| "<a href='#{image}'><img src='#{image}' height='75' hspace='5' vspace='5' style='border: 1px solid #ddd;'/></a>" }.join
end
def links_to_li(links)
links.map { |link| "<li><a href='#{link}'>#{link}</a></li>" }.join("\n")
end
end
get '/' do
erb :home
end
get '/scrape' do
if params[:url]
@page = MetaInspector.new(params[:url],
:connection_timeout => 5, :read_timeout => 5,
:headers => { 'User-Agent' => user_agent, 'Accept-Encoding' => 'identity' },
:faraday_options => { :ssl => { :verify => false } },
:html_content_only => true)
erb :scrape
else
redirect "/"
end
end
error MetaInspector::Error do
@exception_title = env['sinatra.error'].class.to_s
@exception_msg = env['sinatra.error'].message
erb :error
end
private
def user_agent
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
end