-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget.rb
40 lines (37 loc) · 1005 Bytes
/
get.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
require "rest-client"
require "timeout"
require "charlock_holmes/string"
class Get
def initialize
@urls = {}
end
def get(config)
url = config.source.fetch(:url)
Timeout::timeout(3) do
@urls[url] ||= RestClient::Request.execute({
method: "get",
url: url,
headers: {
user_agent: "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
},
cookies: config.source[:cookies],
timeout: 3,
open_timeout: 3
})
detection = CharlockHolmes::EncodingDetector.detect(@urls[url])
if detection[:encoding]
CharlockHolmes::Converter.convert @urls[url], detection[:encoding], 'UTF-8'
else
@urls[url]
end
end
rescue RestClient::Exception, SocketError
return false
rescue ArgumentError
return false
rescue Timeout::Error
return false
rescue Net::OpenTimeout
return false
end
end