-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwget.rb
45 lines (40 loc) · 816 Bytes
/
wget.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
#!/usr/bin/env ruby
# coding: utf-8
require 'open-uri'
class MyWGet
def initialize(time_stride, retry_times = 0)
@last_time = Time.now
@time_stride = time_stride
@retry_times = retry_times
end
def check_sleep
return if not @time_stride
dif = Time.now - @last_time
if dif < @time_stride
if sleep(@time_stride - dif) == 0
return
end
end
@last_time = Time.now
end
def get(from, to)
nr_retry = 0
begin
URI.open(from) do |fin|
fout = open(to, 'w')
fout.write(fin.read)
end
check_sleep
return true
#rescue OpenURI::HTTPError
rescue
if nr_retry >= @retry_times
return false
else
nr_retry += 1
sleep 1 # sleep before retry
retry
end
end
end
end