-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedstore.rb
307 lines (276 loc) · 6.93 KB
/
feedstore.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/ruby
require 'net/http'
require 'openssl'
require 'uri'
require 'gdbm'
require 'time'
require 'syslog'
require 'rexml/parsers/baseparser'
require 'rexml/parsers/streamparser'
require 'rexml/streamlistener'
require 'rubygems'
require 'tarwriter'
class WGet
def initialize
@conn = nil
@resp = nil
@ca = nil
$logger = Syslog.open('feedstore', Syslog::LOG_PID, Syslog::LOG_NEWS)
$onset = Time.now
@n = Hash.new(0)
@maxage = nil
end
attr_reader :maxage
def ca= val
@ca = val
end
def connect(uri)
if @conn then
STDERR.puts "now #{@conn.address}:#{@conn.port}" if $VERBOSE
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 unless uri.port == 80
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)
connect(uri)
hdr = {}
path = uri.request_uri
STDERR.puts "GET #{path}" if $VERBOSE
if lmt then
STDERR.puts "If-Modified-Since: #{lmt}" if $VERBOSE
hdr['if-modified-since'] = lmt
end
@resp = @conn.request_get(path, hdr)
if /max-age=(\d+)/ === @resp['cache-control'] then
@maxage = $1.to_i
else
@maxage = nil
end
rc = @resp.code
STDERR.puts "--> #{rc}" if $VERBOSE
@n[rc] += 1
rc
end
def body
@resp.body
end
def lmt
@resp['last-modified']
end
def close
@conn.finish if @conn and @conn.started?
$logger.info('elapsed %g wget %s', Time.now - $onset, @n.inspect)
$logger.close
end
def status
return 11 if @n.empty?
if @n.include?('200') then 0
elsif @n.include?('304') then 3
else 4
end
end
end
class AtomParse
include REXML::StreamListener
def initialize
@tag = nil
@rec = {}
@cb = proc
end
TAGS = /^(name|author|id|title|updated)$/
def text(text)
return unless @tag
# return if text.strip.empty?
@rec[@tag] = text
@tag = nil
end
def tag_start(name, attrs)
case name
when 'entry' then @rec = {}
when TAGS then @tag = name
when 'link' then
@rec['link/@href'] = attrs['href']
end
end
def tag_end(name)
return unless 'entry' == name
@cb.call(@rec)
end
end
class FeedStore
def help
puts "#$0 rtdb outfnam ca feedurl ..."
exit 1
end
def initialize argv
@rtdb = argv.shift
@outfnam = argv.shift
ca = argv.shift
@feeds = argv
help if @feeds.empty?
@wget = WGet.new
@wget.ca = ca
@dfilter = nil
@feedtar = nil
@acheck = nil
@xfilter = nil
@lfilter = nil
end
def getlmt(feed)
lmt = nil
mode = GDBM::NOLOCK | GDBM::READER
GDBM.open(@rtdb, 0644, mode) {|rtdb|
key = "lmt/#{feed}"
lmt = rtdb[key]
}
return lmt
rescue Errno::ENOENT
nil
end
def setlmt(feed, lmt)
key = "lmt/#{feed}"
mode = GDBM::WRCREAT
GDBM.open(@rtdb, 0644, mode) {|rtdb|
rtdb[key] = lmt
}
end
def tmpnam(feed)
File.basename(feed) + Time.now.utc.strftime('-%Y-%m-%dT%H%M%S') + "-#{$$}.xml"
end
def getfeed(idb, tar, feed)
lmt = getlmt(feed)
ufeed = URI.parse(feed)
code = @wget.get(ufeed, lmt)
case code
when '304' then return 0
when '200' then :do_nothing
else raise Errno::EIO, "HTTP #{code}"
end
fbdy = @wget.body
lmt2 = @wget.lmt
@feedtar.add(tn = tmpnam(feed), fbdy)
if @acheck and @wget.maxage then
if @wget.maxage > @acheck then
msg = "Max-Age: #{@wget.maxage} exceeds #{@acheck} for #{tn}"
$logger.err(msg)
end
end
# @wget can be reused now
li = AtomParse.new { |rec|
STDERR.puts rec.inspect if $VERBOSE
ft = Time.parse(rec['updated']).utc
id = rec['id']
if @dfilter then
unless @dfilter === ft then
STDERR.puts "skip -d #{ft} #{id}" if $VERBOSE
next
end
end
if @lfilter and not @lfilter === id then
STDERR.puts "limit #{id}" if $VERBOSE
next
end
if @xfilter and @xfilter === id then
STDERR.puts "exclude #{id}" if $VERBOSE
next
end
if idb.has_key?(id) then
STDERR.puts "skip dup #{id}" if $VERBOSE
next
end
begin
umsg = ufeed.merge(rec['link/@href'])
code2 = @wget.get(umsg)
case code2
when '200' then :do_nothing
when '404' then
$logger.err('rescue=ENOENT %s', umsg)
raise Errno::ENOENT, umsg
else
$logger.err('rescue=EIO %s', umsg)
raise Errno::EIO, "HTTP #{code2}"
end
body = @wget.body
STDERR.puts "size #{body.size}" if $VERBOSE
idb["lmt/#{id}"] = lmt2
t = Time.now.utc
mid = File.basename(id)
if idb.has_key?(mid) then
STDERR.puts "skip dup2 #{mid}" if $VERBOSE
else
pos = tar.add(mid, body, t)
idb[mid] = idb[id] = pos.to_s
m = t.strftime('m/%Y-%m-%dT%H%M')
idb[m] = [String(idb[m]), mid, " "].join
end
rescue Errno::ENOENT
nil
end
}
begin
REXML::Parsers::StreamParser.new(fbdy, li).parse
setlmt(feed, lmt2)
rescue REXML::ParseException => e
STDERR.puts("feed #{feed} - #{e.message}")
$logger.err('feed %s - %s', feed, e.message)
end
end
def run2
idx = "#{@outfnam}.idx1"
GDBM.open(idx, 0644, GDBM::WRCREAT) {|idb|
@feedtar = TarWriter.open("feed-#{@outfnam}.tar", "a");
TarWriter.open("#{@outfnam}.tar", 'a') {|tar|
@feeds.each {|feed|
case feed
when /^-d(\d\d\d\d)-?(\d\d)-?(\d\d)/
base = Time.gm($1.to_i, $2.to_i, $3.to_i)
@dfilter = base...(base + 86400)
STDERR.puts @dfilter.inspect if $VERBOSE
when /^-a(\d+)/
@acheck = $1.to_i
STDERR.puts "set max-age-check #{@acheck}" if $VERBOSE
when /^-l/
@lfilter = Regexp.new($')
STDERR.puts "limit #{@lfilter.inspect}" if $VERBOSE
when /^-x/
@xfilter = Regexp.new($')
# 暫定対処:x オプションは 40% の確率で無効化する
if rand < 0.1 then
$VERBOSE = true
STDERR.puts "skip (exclude #{@xfilter.inspect})" if $VERBOSE
@xfilter = nil
else
STDERR.puts "exclude #{@xfilter.inspect}" if $VERBOSE
end
else
getfeed(idb, tar, feed)
end
}
}
}
rescue Errno::EAGAIN
$logger.err('rescue=EAGAIN idx=%s', @outfnam)
ensure
@feedtar.close if @feedtar
@wget.close
end
def run
run2
exit @wget.status
end
end
FeedStore.new(ARGV).run