This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.rb
324 lines (281 loc) · 8.06 KB
/
crawler.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'data_mapper'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'dm-timestamps'
require 'ostruct'
DataMapper::setup(:default, File.join('sqlite3://', Dir.pwd, 'news.db'))
class Article
include DataMapper::Resource
property :id, Serial
property :guid, String, :length => 300
property :link, String, :length => 300
property :title, Text
property :pubDate, DateTime
property :publisher, String
property :source, String, :length => 100
property :category, String, :length => 40
end
DataMapper::Model.raise_on_save_failure = true
DataMapper.finalize()
DataMapper.auto_upgrade!
# <div class="description">
# text
# <br />
# text
# <br />
# text
# <br />
# text
# <br />
# text
# <br />
# Πηγή: ΑΠΕ
# </div>
# articlePage = Nokogiri::HTML(open("http://www.zougla.gr/money/article/eksagoges-me-pliromi-se-rouvlia-eksetazi-i-mosxa"))
# Since we'll iterate in order, stick the colon there to trim it if present after the word prefix is gone
def stripPrefix( stringToStrip )
sourcePrefix = ["Πηγή", "Πηγές", "πηγή", "πηγές", ":" ]
sourcePrefix.each { |prefix|
if stringToStrip.start_with?(prefix)
stringToStrip = stringToStrip[prefix.length..-1].strip
end
}
return stringToStrip
end
class ZouglaRssParser
def parseRss( rss )
# Does not work, returns no results
rss.xpath("//feed").each { |item|
article = Article.new
puts "id ->" + item.xpath("id").text
puts "link ->" + item.xpath("link[@href]/@href").text
puts "title -> " + item.xpath("title").text
puts "date ->" + item.xpath("published").text
yield article
}
end
end
class ParserForZougla
def initialize
@rssLocation = "http://www.zougla.gr/articlerss.xml"
end
def getRss
return Nokogiri::XML(open(@rssLocation))
end
def fill ( article )
articlePage = Nokogiri::HTML(open(article.link))
# Zougla does not put references in tags, it's part of the article text.
# It may or many not have a source, have to do a bit of guesswork. It's probably the last line of text
candidateSource = articlePage.css("div.description").text.lines[-1]
candidateSource = candidateSource.length > 50 ? "" : stripPrefix( candidateSource )
article.source = candidateSource
article.publisher = "Enet"
end
end
class ParserForEnet
def initialize
@rssLocationPolitics = "http://www.enet.gr/rss?i=news.el.categories&c=politikh"
@rssLocationGreece = "http://www.enet.gr/rss?i=news.el.categories&c=ellada"
@rssLocationEconomy = "http://www.enet.gr/rss?i=news.el.categories&c=oikonomia"
end
def getRssPolitics
return Nokogiri::XML(open(@rssLocationPolitics))
end
def getRssGreece
return Nokogiri::XML(open(@rssLocationGreece))
end
def getRssEconomy
return Nokogiri::XML(open(@rssLocationEconomy))
end
def fill ( article )
articlePage = Nokogiri::HTML(open(article.link))
# Enet is a PITA. It may or many not have a source, have to do a bit of guesswork
candidateSource = articlePage.css("div#post-content > p").last.text.strip!
article.source = candidateSource.length > 50 ? "" : stripPrefix( candidateSource )
article.publisher = "Enet"
end
end
class ParserForKathimerini
def initialize
@rssLocationGreece = "http://www.kathimerini.gr/rss?i=news.el.politikh"
@rssLocationEconomy = "http://www.kathimerini.gr/rss?i=news.el.ellhnikh-oikonomia"
end
def getRssEconomy
return Nokogiri::XML(open(@rssLocationEconomy))
end
def getRssGreece
return Nokogiri::XML(open(@rssLocationGreece))
end
def fill ( article )
articlePage = Nokogiri::HTML(open(article.link))
article.source = articlePage.css("article#item-article > header > span.item-source").text.strip
article.publisher = "kathimerini"
end
end
class ParserForInGr
def initialize
@rssLocationEconomy = "http://rss.in.gr/feed/news/economy/"
@rssLocationGreece = "http://rss.in.gr/feed/news/greece/"
end
def getRssEconomy
return Nokogiri::XML(open(@rssLocationEconomy))
end
def getRssGreece
return Nokogiri::XML(open(@rssLocationGreece))
end
def fill ( article )
articlePage = Nokogiri::HTML(open(article.link))
article.source = articlePage.css("p.credits").text.strip
article.publisher = "in.gr"
end
end
class RssParser
def parseRss( rss )
rss.xpath("//item").each { |item|
article = Article.new
article.guid = item.xpath("guid").text
article.link = item.xpath("link").text
article.title = item.xpath("title").text
article.pubDate = item.xpath("pubDate").text
yield article
}
end
end
def doTheThing()
rssParser = RssParser.new
ingrParser = ParserForInGr.new
kathimeriniParser = ParserForKathimerini.new
enetParser = ParserForEnet.new
begin
inRssEconomy = ingrParser.getRssEconomy
inRssGreece = ingrParser.getRssGreece
rescue
puts "Failed to open rss for in.gr"
else
rssParser.parseRss( inRssGreece ) { |article|
if ( Article.first( :guid => article.guid) == nil )
puts "#{article.link} was not there, filling"
begin
ingrParser.fill ( article )
article.category = "greece"
rescue
puts "Failed to open article #{article.link}"
else
article.save
end
elsif
puts "#{article.link} was already there, skipping"
end
}
rssParser.parseRss( inRssEconomy ) { |article|
if ( Article.first( :guid => article.guid) == nil )
puts "#{article.link} was not there, filling"
begin
ingrParser.fill ( article )
article.category = "economy"
rescue
puts "Failed to open article #{article.link}"
else
article.save
end
elsif
puts "#{article.link} was already there, skipping"
end
}
end
begin
kathimeriniRssEconomy = kathimeriniParser.getRssEconomy
kathimeriniRssGreece = kathimeriniParser.getRssGreece
rescue
puts "Failed to open rss for kathimerini"
else
rssParser.parseRss( kathimeriniRssEconomy ) { |article|
if ( Article.first( :guid => article.guid) == nil )
puts "#{article.link} was not there, filling"
begin
kathimeriniParser.fill ( article )
article.category = "economy"
rescue
puts "Failed to open article #{article.link}"
else
article.save
end
elsif
puts "#{article.link} was already there, skipping"
end
}
rssParser.parseRss( kathimeriniRssGreece ) { |article|
if ( Article.first( :guid => article.guid) == nil )
puts "#{article.link} was not there, filling"
begin
kathimeriniParser.fill ( article )
article.category = "greece"
rescue
puts "Failed to open article #{article.link}"
else
article.save
end
elsif
puts "#{article.link} was already there, skipping"
end
}
end
begin
enetRssEconomy = enetParser.getRssEconomy
enetRssGreece = enetParser.getRssGreece
enetRssPolitics = enetParser.getRssPolitics
rescue
puts "Failed to open rss for kathimerini"
else
rssParser.parseRss( enetRssEconomy ) { |article|
if ( Article.first( :guid => article.guid) == nil )
puts "#{article.link} was not there, filling"
begin
enetParser.fill ( article )
article.category = "economy"
rescue
puts "Failed to open article #{article.link}"
else
article.save
end
elsif
puts "#{article.link} was already there, skipping"
end
}
rssParser.parseRss( enetRssGreece ) { |article|
if ( Article.first( :guid => article.guid) == nil )
puts "#{article.link} was not there, filling"
begin
enetParser.fill ( article )
article.category = "greece"
rescue
puts "Failed to open article #{article.link}"
else
article.save
end
elsif
puts "#{article.link} was already there, skipping"
end
}
rssParser.parseRss( enetRssPolitics ) { |article|
if ( Article.first( :guid => article.guid) == nil )
puts "#{article.link} was not there, filling"
begin
enetParser.fill ( article )
article.category = "greece"
rescue
puts "Failed to open article #{article.link}"
else
article.save
end
elsif
puts "#{article.link} was already there, skipping"
end
}
end
end
doTheThing