-
Notifications
You must be signed in to change notification settings - Fork 0
/
syndl.rb
265 lines (242 loc) · 5.67 KB
/
syndl.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/local/bin/ruby
require 'net/http'
require 'openssl'
require 'uri'
require 'gdbm'
require 'syslog'
require 'rubygems'
require 'tarwriter'
class WGet
def initialize
@conn = nil
@resp = nil
@ca = nil
$logger = Syslog.open('syndl', Syslog::LOG_PID, Syslog::LOG_NEWS)
$onset = Time.now
@n = Hash.new(0)
@n['-w'] = !!($VERBOSE)
end
def ca= val
@ca = val
end
def connect(uri)
if @conn then
return 0 if @conn.address == uri.host and @conn.port == uri.port
@conn.finish
end
STDERR.puts "#CONNECT #{uri.host}:#{uri.port}" if $VERBOSE
@conn = Net::HTTP.new(uri.host, uri.port, :ENV)
@conn.use_ssl = true
if @ca
if /\/$/ === @ca then
@conn.ca_path = @ca
else
@conn.ca_file = @ca
end
else
STDERR.puts "Warning: server certificate not verified"
@conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
@conn.start
end
def get(uri, lmt = nil, etag = nil)
begin
connect(uri)
hdr = {}
path = uri.request_uri
STDERR.puts "#GET #{path}" if $VERBOSE
if lmt then
hdr['if-modified-since'] = lmt
end
if etag then
hdr['If-None-Match'] = etag
end
STDERR.puts "# #{hdr.inspect}" if $VERBOSE
@resp = @conn.get2(path, hdr)
STDERR.puts "#--> #{@resp.code}" if $VERBOSE
rc = @resp.code
rescue Exception => e
rc = '500'
$logger.err("#{rc} rescue=#{e.class.to_s}")
end
@n[rc] += 1
rc
end
def body
@resp.body
end
def lmt
@resp['last-modified']
end
def etag
@resp['etag']
end
def tag s
@n['tag'] = s
end
def eagain
@n['EAGAIN'] = 1
end
def waitok s
@n["wait#{s}"] += 1
end
def close
@conn.finish if @conn and @conn.started?
$logger.info('elapsed %g wget %s', Time.now - $onset, @n.inspect)
$logger.close
end
end
class SynDL
def help
puts "#$0 rtdb logdb feedurl ..."
exit 1
end
def initialize argv
@rtdb = argv.shift
@logdb = argv.shift
@feeds = argv.dup
help if @feeds.empty?
# @folder and @wget must be close()-ed properly
@folder = TarWriter::Folder.new(nil, 'a')
@wget = WGet.new
@pfilter = {}
end
def getlmt(feed)
lmt = etag = nil
mode = GDBM::NOLOCK | GDBM::READER
GDBM.open(@rtdb, 0644, mode) {|rtdb|
key = "lmt/#{feed}"
lmt = rtdb[key]
key = "etag/#{feed}"
etag = rtdb[key]
}
return [lmt, etag]
rescue Errno::ENOENT
nil
end
def setlmt(feed, lmt, etag)
return unless lmt or etag
mode = GDBM::WRCREAT
GDBM.open(@rtdb, 0644, mode) {|rtdb|
if lmt then
key = "lmt/#{feed}"
rtdb[key] = lmt
end
if etag then
key = "etag/#{feed}"
rtdb[key] = etag
end
}
end
def getfeed(ldb, feed)
lmt, etag = getlmt(feed)
ufeed = URI.parse(feed)
STDERR.puts "##{ufeed.inspect}" if $VERBOSE
code = @wget.get(ufeed, lmt, etag)
case code
when '304' then
STDERR.puts "#unchanged" if $VERBOSE
errid = 'dup:' + Time.now.utc.strftime('%Y-%m-%dT%H%M%SZ')
ldb[errid] = feed
return 0
when '200' then
:do_nothing
else
errid = "err:#{code}:" + Time.now.utc.strftime('%Y-%m-%dT%H%M%SZ')
ldb[errid] = feed
exit 16
end
fbdy = @wget.body
lmt2 = @wget.lmt
etag2 = @wget.etag
STDERR.puts "#ETag: #{etag2}" if $VERBOSE
# @wget can be reused now
defer = Hash.new
fbdy.each_line { |line|
id = line.chomp
if @pfilter[:match] then
next unless @pfilter[:match] =~ id
end
if @pfilter[:reject] then
next if @pfilter[:reject] =~ id
end
if ldb[id] then
STDERR.puts "#dup skip #{id}" if $VERBOSE
next
end
umsg = URI.parse(id)
code = @wget.get(umsg)
if '404' == code then
defer[id] = true
else
body = @wget.body
STDERR.puts "#size #{body.size}" if $VERBOSE
fnam = File.basename(id).gsub(/[^A-Za-z_0-9.]/, '_')
t = Time.now.utc
@folder.add(fnam, body, t)
ldb[id] = t.strftime('%Y-%m-%dT%H%M%SZ')
end
}
5.times {|i|
break if defer.empty?
sleep(11)
defer.keys.each {|id|
umsg = URI.parse(id)
code = @wget.get(umsg)
next if '404' == code
body = @wget.body
STDERR.puts "#size #{body.size}" if $VERBOSE
fnam = File.basename(id).gsub(/[^A-Za-z_0-9.]/, '_')
t = Time.now.utc
@folder.add(fnam, body, t)
ldb[id] = t.strftime('%Y-%m-%dT%H%M%SZ')
defer.delete(id)
@wget.waitok(i * 11)
}
}
unless defer.empty?
defer.keys.each{ @wget.waitok('X') }
end
setlmt(feed, lmt2, etag2)
end
def run
GDBM.open(@logdb, 0644, GDBM::WRCREAT) {|ldb|
@feeds.each {|feed|
case feed
when /^--match=/
if $'.empty? then @pfilter.delete(:match)
else @pfilter[:match] = Regexp.new($')
end
when /^--reject=/
if $'.empty? then @pfilter.delete(:reject)
else @pfilter[:reject] = Regexp.new($')
end
when /^--ca=/
@wget.ca= $'
when /^--tar=/
tar = $'
@folder.close
@folder = TarWriter::Folder.new(tar, 'a')
when /^--chdir=/
dir = $'
@folder.close
Dir.chdir(dir)
@folder = TarWriter::Folder.new(nil, 'a')
when /^--tag=/
@wget.tag($')
else
STDERR.puts "getfeed #{feed}" if $VERBOSE
getfeed(ldb, feed)
GC.start
end
}
}
rescue Errno::EAGAIN
$logger.err("db #{@logdb} busy - possibly multiple runs")
@wget.eagain
ensure
@wget.close
@folder.close
end
end
SynDL.new(ARGV).run