-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomics.rb
More file actions
303 lines (258 loc) · 5.39 KB
/
comics.rb
File metadata and controls
303 lines (258 loc) · 5.39 KB
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
=begin
Base utility to create a comics file
=end
require 'rss'
require 'open-uri'
require 'htmlentities'
require 'digest/sha1'
require 'thread'
require 'mail'
class Grabber
attr_accessor :content, :time, :page, :page_title, :guid
# Grabber is a @content stack, a @title and some downladed @page.
def initialize
@title = ""
@content = []
@page = ""
@time = nil
@guid = nil
end
def to_s
"< #{@title} : \n#{@content * '\n'} >"
end
def get_link
(get_content.scan /(\/\/[^"'>]*)/)[0][0]
end
def get_guid
if not @guid then
name = (get_link.scan /\/\/.*?\/(.*)$/)[0][0]
@guid = "//comic/#{Digest::SHA1.hexdigest (get_title + '/' + name)}"
end
@guid
end
def get_title
@title.strip
end
def get_content
@content * ''
end
def url(url)
open url do |f|
@page = f.read
end
end
def title(title)
@title = title
end
def feed(u)
url u
f = RSS::Parser.parse @page, false
i = f.items[0]
for m in [ :content_encoded, :description, :content, :summary ] do
if i.respond_to? m and i.send(m) != nil then
content = i.send(m)
content = content.content if content.respond_to? :content
content.strip!
if content.length > 0 then
@page = content
return
end
end
end
raise "Not found"
end
def re(re)
match = (@page.scan re)[0][0]
@content.push match
match
end
def add_or_replace(s)
if s.include? '%s'
@content.push(s % @content.pop)
else
@content.push s
end
end
def img(s="%s")
@content.pop if @content.last == s
add_or_replace "<img src='#{s}'>"
end
def comment(s="%s")
@content.pop if @content.last == s
add_or_replace "<p>#{s}"
end
def post
@content.push HTMLEntities.new.decode @page
end
end
@feedname = ''
@backfeedname = ''
@backmail = ''
@data = []
@threads = []
@putsemaphore = Mutex.new
def comic(&name)
@threads << Thread.new {
cr = Grabber.new
begin
cr.instance_eval &name
@data.push cr
@putsemaphore.synchronize {
puts "Done: " + cr.get_title
}
rescue
@putsemaphore.synchronize {
puts "Exception for: " + cr.get_title
}
end
}
end
def load_old_file(filename)
# Load old file
out = []
if FileTest.exists? filename then
open filename do |rss|
feed = RSS::Parser.parse rss
return out if not feed
feed.items.each do |item|
cr = Grabber.new
cr.title item.title
cr.content.push item.description
cr.time = item.date
out.push cr
end
end
end
return out
end
def cleanup
# Sort by time
now = Time.now
@data.sort_by! { |i| [i.time ? i.time : now, i.get_title] }
@data.reverse!
# Remove duplicates
tc = {}
@data.each do |i|
tc[i.get_guid] = [] if not tc.key? i.get_guid
tc[i.get_guid].push i
end
tc.each do |k, v|
v[0..-2].each do |i|
@data.delete i
end
end
end
def compute_delta olddata
now = Time.now
@data.sort_by! { |i| [i.time ? i.time : now, i.get_title] }
@data.reverse!
tc = {}
olddata.each do |i|
tc[i.get_guid] = true
end
out = []
@data.each do |i|
if not tc.key? i.get_guid then
out.push i
end
end
return out
end
def cleanup_mail
# Sort by time
now = Time.now
@data.sort_by! { |i| [i.time ? i.time : now, i.get_title] }
@data.reverse!
# Remove duplicates
tc = {}
@data.each do |i|
tc[i.get_title] = [] if not tc.key? i.get_title
tc[i.get_title].push i
end
tc.each do |k, v|
v[1..-1].each do |i|
@data.delete i
end
end
end
def save filename
now = Time.now
puts "Saving: #{filename} with #{@data.length} entries"
rss = RSS::Maker.make "rss2.0" do |maker|
maker.channel.author = 'Fernando Serboncini'
maker.channel.updated = now
maker.channel.title = 'Comics'
maker.channel.description = 'Comics feeds for the masses'
maker.channel.link = filename
@data.each do |cr|
maker.items.new_item do |item|
item.title = cr.get_title
item.date = if cr.time then cr.time else now.to_s end
item.description = cr.get_content
item.link = cr.get_link
end
end
end
open(filename, "w") do |f|
f.write(rss)
end
end
def feed(filename)
@feedname = filename
end
def backfeed filename
@backfeedname = filename
end
def backmail filename
@backmail = filename
end
def run_mail
@threads.each { |t| t.join }
newdata = @data.clone
current = load_old_file @backmail
newdata = compute_delta current
@data.push(*current)
cleanup_mail
save @backmail
return if newdata.size == 0
b = ""
newdata.each do |c|
b += "<h2><a href='" + c.get_link + "'>" + c.get_title + "</a></h2>"
b += c.get_content
b += "\n\n"
end
mail = Mail.new do
from "fernando.serboncini@gmail.com"
to "fernando.serboncini@gmail.com"
subject "Comics"
html_part do
content_type 'text/html; charset=UTF-8'
body b
end
end
mail.delivery_method :sendmail
mail.deliver
end
def run_rss
@threads.each { |t| t.join }
puts "Saving..."
d = load_old_file @backfeedname
@data.push(*d)
cleanup
@data = @data.take 10000
save @backfeedname
# Limit to 75 entries
@data = @data.take 75
save @feedname
end
def run
ARGV.each do |a|
case a
when "mail"
run_mail
when "rss"
run_rss
end
end
end
at_exit { run }