Class: NHKore::ArticleScraper
- Inherits:
-
Scraper
- Object
- Scraper
- NHKore::ArticleScraper
show all
- Extended by:
- AttrBool::Ext
- Defined in:
- lib/nhkore/article_scraper.rb
Constant Summary
Constants inherited
from Scraper
Scraper::DEFAULT_HEADER
Instance Attribute Summary collapse
Attributes inherited from Scraper
#max_redirects, #max_retries, #redirect_rule, #str_or_io, #url
Instance Method Summary
collapse
-
#add_words(article, words, text) ⇒ Object
-
#clean(obj) ⇒ Object
-
#fix_bad_html ⇒ Object
-
#initialize(url, cleaners: [BestCleaner.new], datetime: nil, dict: :scrape, missingno: nil, polishers: [BestPolisher.new], splitter: BestSplitter.new, strict: true, variators: [BestVariator.new], year: nil, **kargs) ⇒ ArticleScraper
constructor
A new instance of ArticleScraper.
-
#parse_datetime(str, year) ⇒ Object
-
#parse_dicwin_id(str) ⇒ Object
-
#polish(obj) ⇒ Object
-
#scrape ⇒ Object
-
#scrape_and_add_words(tag, article, result: ScrapeWordsResult.new) ⇒ Object
-
#scrape_content(doc, article) ⇒ Object
-
#scrape_datetime(doc, futsuurl = nil) ⇒ Object
-
#scrape_dict ⇒ Object
-
#scrape_dict_url_only ⇒ Object
-
#scrape_dicwin_word(tag, id, result: ScrapeWordsResult.new) ⇒ Object
-
#scrape_futsuurl(doc) ⇒ Object
-
#scrape_link(tag) ⇒ Object
-
#scrape_ruby_word(word, result: ScrapeWordsResult.new) ⇒ Object
-
#scrape_ruby_words(tag, result: ScrapeWordsResult.new) ⇒ Object
-
#scrape_sha256_only ⇒ Object
-
#scrape_text_word(tag, result: ScrapeWordsResult.new) ⇒ Object
-
#scrape_title(doc, article) ⇒ Object
-
#scrape_words(tag, dicwin: false, result: ScrapeWordsResult.new) ⇒ Object
-
#scrape_year(doc, futsuurl = nil) ⇒ Object
-
#split(str) ⇒ Object
-
#variate(str) ⇒ Object
-
#warn_or_error(klass, msg) ⇒ Object
Methods inherited from Scraper
#fetch_cookie, #html_doc, #join_url, #open, #open_file, #open_url, #read, #reopen, #rss_doc
Constructor Details
#initialize(url, cleaners: [BestCleaner.new], datetime: nil, dict: :scrape, missingno: nil, polishers: [BestPolisher.new], splitter: BestSplitter.new, strict: true, variators: [BestVariator.new], year: nil, **kargs) ⇒ ArticleScraper
Returns a new instance of ArticleScraper.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/nhkore/article_scraper.rb', line 49
def initialize(url,cleaners: [BestCleaner.new],datetime: nil,dict: :scrape,missingno: nil,
polishers: [BestPolisher.new],splitter: BestSplitter.new,strict: true,
variators: [BestVariator.new],year: nil,**kargs)
super(url,**kargs)
@cleaners = Array(cleaners)
@datetime = datetime.nil? ? nil : Util.jst_time(datetime)
@dict = dict
@kargs = kargs
@missingno = missingno
@polishers = Array(polishers)
@splitter = splitter
@strict = strict
@variators = Array(variators)
@year = year
end
|
Instance Attribute Details
#cleaners ⇒ Object
Returns the value of attribute cleaners.
32
33
34
|
# File 'lib/nhkore/article_scraper.rb', line 32
def cleaners
@cleaners
end
|
#datetime ⇒ Object
Returns the value of attribute datetime.
33
34
35
|
# File 'lib/nhkore/article_scraper.rb', line 33
def datetime
@datetime
end
|
#dict ⇒ Object
Returns the value of attribute dict.
34
35
36
|
# File 'lib/nhkore/article_scraper.rb', line 34
def dict
@dict
end
|
#kargs ⇒ Object
Returns the value of attribute kargs.
35
36
37
|
# File 'lib/nhkore/article_scraper.rb', line 35
def kargs
@kargs
end
|
#missingno ⇒ Object
Returns the value of attribute missingno.
36
37
38
|
# File 'lib/nhkore/article_scraper.rb', line 36
def missingno
@missingno
end
|
#polishers ⇒ Object
Returns the value of attribute polishers.
37
38
39
|
# File 'lib/nhkore/article_scraper.rb', line 37
def polishers
@polishers
end
|
#splitter ⇒ Object
Returns the value of attribute splitter.
38
39
40
|
# File 'lib/nhkore/article_scraper.rb', line 38
def splitter
@splitter
end
|
#variators ⇒ Object
Returns the value of attribute variators.
40
41
42
|
# File 'lib/nhkore/article_scraper.rb', line 40
def variators
@variators
end
|
#year ⇒ Object
Returns the value of attribute year.
41
42
43
|
# File 'lib/nhkore/article_scraper.rb', line 41
def year
@year
end
|
Instance Method Details
#add_words(article, words, text) ⇒ Object
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
|
# File 'lib/nhkore/article_scraper.rb', line 66
def add_words(article,words,text)
words.each do |word|
next if polish(word.word).empty?
article.add_word(polish(word))
variate(word.word).each do |v|
v = polish(clean(v))
next if v.empty?
article.add_word(Word.new(
defn: word.defn,
eng: word.eng,
unknown: v
))
end
end
split(text).each do |t|
t = polish(clean(t))
next if t.empty?
article.add_word(Word.new(unknown: t))
variate(t).each do |v|
v = polish(clean(v))
next if v.empty?
article.add_word(Word.new(unknown: v))
end
end
end
|
#clean(obj) ⇒ Object
106
107
108
|
# File 'lib/nhkore/article_scraper.rb', line 106
def clean(obj)
return Cleaner.clean_any(obj,@cleaners)
end
|
#fix_bad_html ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/nhkore/article_scraper.rb', line 110
def fix_bad_html
read
@str_or_io = @str_or_io.gsub(/
(?<cane><「<)
/x) do |match|
if !Regexp.last_match(:cane).nil?
match = match.sub('<','<')
end
match
end
end
|
#parse_datetime(str, year) ⇒ Object
130
131
132
133
134
135
|
# File 'lib/nhkore/article_scraper.rb', line 130
def parse_datetime(str,year)
str = str.gsub(/[\[\][[:space:]]]+/,'') str = "#{year}年 #{str} #{Util::JST_OFFSET}"
return Time.strptime(str,'%Y年 %m月%d日%H時%M分 %:z')
end
|
#parse_dicwin_id(str) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/nhkore/article_scraper.rb', line 137
def parse_dicwin_id(str)
str = str.to_s.strip.downcase
if str.start_with?('id-') str = str.gsub(/\D+/,'')
else end
return nil if str.empty?
return str
end
|
#polish(obj) ⇒ Object
150
151
152
|
# File 'lib/nhkore/article_scraper.rb', line 150
def polish(obj)
return Polisher.polish_any(obj,@polishers)
end
|
#scrape ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/nhkore/article_scraper.rb', line 154
def scrape
scrape_dict
fix_bad_html
article = Article.new
doc = html_doc
article.futsuurl = scrape_futsuurl(doc)
article.datetime = scrape_datetime(doc,article.futsuurl)
article.sha256 = scrape_content(doc,article)
article.title = scrape_title(doc,article)
article.url = @url
return article
end
|
#scrape_and_add_words(tag, article, result: ScrapeWordsResult.new) ⇒ Object
171
172
173
174
175
176
177
178
|
# File 'lib/nhkore/article_scraper.rb', line 171
def scrape_and_add_words(tag,article,result: ScrapeWordsResult.new)
result = scrape_words(tag,result: result)
result.polish!
add_words(article,result.words,result.text)
return result
end
|
#scrape_content(doc, article) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/nhkore/article_scraper.rb', line 180
def scrape_content(doc,article)
tag = doc.css('div#js-article-body')
tag = doc.css('div.article-main__body') if tag.length < 1
tag = doc.css('div.article-body') if tag.length < 1
tag = doc.css('div#main') if tag.length < 1 && !@strict
if tag.length > 0
text = Util.unspace_web_str(tag.text.to_s)
if !text.empty?
hexdigest = Digest::SHA256.hexdigest(text)
return hexdigest if article.nil?
result = scrape_and_add_words(tag,article)
return hexdigest if result.words?
end
end
raise ScrapeError,"could not scrape content at URL[#{@url}]"
end
|
#scrape_datetime(doc, futsuurl = nil) ⇒ Object
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
|
# File 'lib/nhkore/article_scraper.rb', line 205
def scrape_datetime(doc,futsuurl=nil)
year = scrape_year(doc,futsuurl)
tag_name = 'p#js-article-date'
tag = doc.css(tag_name)
if tag.length > 0
tag_text = tag[0].text
begin
datetime = parse_datetime(tag_text,year)
return datetime
rescue ArgumentError => e
Util.warn("could not parse date time[#{tag_text}] from tag[#{tag_name}] at URL[#{@url}]: #{e}")
end
end
tag_name = 'p.article-main__date'
tag = doc.css(tag_name)
if tag.length > 0
tag_text = tag[0].text
begin
datetime = parse_datetime(tag_text,year)
return datetime
rescue ArgumentError => e
Util.warn("could not parse date time[#{tag_text}] from tag[#{tag_name}] at URL[#{@url}]: #{e}")
end
end
tag = doc.css('body')
if tag.length > 0
tag_id = tag[0]['id'].to_s.split('_',2)
if tag_id.length > 0
tag_id = tag_id[0].gsub(/[^[[:digit:]]]+/,'')
if tag_id.length == 8
datetime = Time.strptime(tag_id,'%Y%m%d')
return datetime
end
end
end
return @datetime unless @datetime.nil?
raise ScrapeError,"could not scrape date time at URL[#{@url}]"
end
|
#scrape_dict ⇒ Object
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
|
# File 'lib/nhkore/article_scraper.rb', line 267
def scrape_dict
return if @dict != :scrape
dict_url = DictScraper.parse_url(@url)
retries = 0
begin
scraper = DictScraper.new(dict_url,missingno: @missingno,parse_url: false,**@kargs)
rescue OpenURI::HTTPError => e
if retries == 0 && e.to_s.include?('404')
read
scraper = ArticleScraper.new(@url,str_or_io: @str_or_io,**@kargs)
dict_url = scraper.scrape_dict_url_only
retries += 1
retry
else
raise e.exception("could not scrape dictionary URL[#{dict_url}] at URL[#{@url}]: #{e}")
end
end
@dict = scraper.scrape
end
|
#scrape_dict_url_only ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/nhkore/article_scraper.rb', line 293
def scrape_dict_url_only
doc = html_doc
tag = doc.css('body')
if tag.length > 0
tag_id = tag[0]['id'].to_s.split('_',2)
if tag_id.length == 2
dict_url = Util.strip_web_str(tag_id[1])
if !dict_url.empty?
return DictScraper.parse_url(@url,basename: dict_url)
end
end
end
raise ScrapeError,"could not scrape dictionary URL at URL[#{@url}]"
end
|
#scrape_dicwin_word(tag, id, result: ScrapeWordsResult.new) ⇒ Object
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/nhkore/article_scraper.rb', line 315
def scrape_dicwin_word(tag,id,result: ScrapeWordsResult.new)
dicwin_result = scrape_words(tag,dicwin: true)
return nil unless dicwin_result.words?
kana = ''.dup
kanji = ''.dup
dicwin_result.words.each do |word|
kana << word.kana unless word.kana.nil?
if kanji.empty?
kanji << word.kanji unless word.kanji.nil?
else
kanji << word.word end
end
entry = nil
kana = clean(kana)
kanji = clean(kanji)
raise ScrapeError,"empty dicWin word at URL[#{@url}] in tag[#{tag}]" if kana.empty? && kanji.empty?
if !@dict.nil?
entry = @dict[id]
raise ScrapeError,"no dicWin ID[#{id}] at URL[#{@url}] in dictionary[#{@dict}]" if entry.nil?
entry = entry.to_s
end
word = Word.new(
defn: entry,
kana: kana,
kanji: kanji
)
result.add_text(dicwin_result.text) result.add_word(word)
return word
end
|
#scrape_futsuurl(doc) ⇒ Object
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# File 'lib/nhkore/article_scraper.rb', line 359
def scrape_futsuurl(doc)
tag = doc.css('div#js-regular-news-wrapper')
if tag.length > 0
link = scrape_link(tag[0])
return link unless link.nil?
end
tag = doc.css('div.link-to-normal')
if tag.length > 0
link = scrape_link(tag[0])
return link unless link.nil?
end
warn_or_error(ScrapeError,"could not scrape futsuurl at URL[#{@url}]")
return nil
end
|
#scrape_link(tag) ⇒ Object
385
386
387
388
389
390
391
392
393
394
|
# File 'lib/nhkore/article_scraper.rb', line 385
def scrape_link(tag)
link = tag.css('a')
return nil if link.length < 1
link = Util.unspace_web_str(link[0]['href'].to_s)
return nil if link.empty?
return link
end
|
#scrape_ruby_word(word, result: ScrapeWordsResult.new) ⇒ Object
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
# File 'lib/nhkore/article_scraper.rb', line 410
def scrape_ruby_word(word,result: ScrapeWordsResult.new)
result.add_text(word.kanji)
kanji = clean(word.kanji)
kana = clean(word.kana)
if !@missingno.nil?
if kana.empty?
kana = @missingno.kana_from_kanji(kanji)
kana = kana.nil? ? '' : clean(kana)
if !kana.empty?
Util.warn("using missingno for kana[#{kana}] from kanji[#{kanji}]")
end
elsif kanji.empty?
kanji = @missingno.kanji_from_kana(kana)
kanji = kanji.nil? ? '' : clean(kanji)
if !kanji.empty?
Util.warn("using missingno for kanji[#{kanji}] from kana[#{kana}]")
end
end
end
raise ScrapeError,"empty kanji at URL[#{@url}] in tag[#{tag}]" if kanji.empty?
raise ScrapeError,"empty kana at URL[#{@url}] in tag[#{tag}]" if kana.empty?
word = Word.new(
kana: kana,
kanji: kanji,
word: word
)
return word
end
|
#scrape_ruby_words(tag, result: ScrapeWordsResult.new) ⇒ Object
397
398
399
400
401
402
403
404
405
406
407
408
|
# File 'lib/nhkore/article_scraper.rb', line 397
def scrape_ruby_words(tag,result: ScrapeWordsResult.new)
words = Word.scrape_ruby_tag(tag,missingno: @missingno,url: @url)
final_words = []
return final_words if words.nil?
words.each do |word|
final_words << scrape_ruby_word(word,result: result)
end
return final_words
end
|
#scrape_sha256_only ⇒ Object
453
454
455
456
457
458
459
|
# File 'lib/nhkore/article_scraper.rb', line 453
def scrape_sha256_only
doc = html_doc
sha256 = scrape_content(doc,nil)
return sha256
end
|
#scrape_text_word(tag, result: ScrapeWordsResult.new) ⇒ Object
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
# File 'lib/nhkore/article_scraper.rb', line 461
def scrape_text_word(tag,result: ScrapeWordsResult.new)
word = Word.scrape_text_node(tag,url: @url)
if word.nil?
result.add_text(tag.text.to_s)
return nil
end
text = word.word
result.add_text(text)
text = clean(text)
return nil if text.empty?
word = Word.new(
kana: clean(word.kana),
kanji: clean(word.kanji),
word: word,
)
return word
end
|
#scrape_title(doc, article) ⇒ Object
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
# File 'lib/nhkore/article_scraper.rb', line 490
def scrape_title(doc,article)
tag = doc.css('h1.article-main__title')
tag_name = nil
if tag.length < 1
tag = doc.css('h1.article-title') end
if tag.length < 1
tag_name = 'h1.article-eq__title'
tag = doc.css(tag_name)
end
if tag.length < 1 && !@strict
tag_name = 'div#main h2'
tag = doc.css(tag_name)
end
if tag.length > 0
Util.warn("using [#{tag_name}] for title at URL[#{@url}]") unless tag_name.nil?
result = scrape_and_add_words(tag,article)
title = result.text
return title unless title.empty?
end
raise ScrapeError,"could not scrape title at URL[#{@url}]"
end
|
#scrape_words(tag, dicwin: false, result: ScrapeWordsResult.new) ⇒ Object
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
# File 'lib/nhkore/article_scraper.rb', line 525
def scrape_words(tag,dicwin: false,result: ScrapeWordsResult.new)
children = tag.children.to_a.reverse
while !children.empty?
child = children.pop
name = nil
words = []
name = Util.unspace_web_str(child.name.to_s).downcase if child.respond_to?(:name)
if name == 'ruby'
words = scrape_ruby_words(child,result: result)
elsif child.text?
words << scrape_text_word(child,result: result)
elsif name == 'rt'
raise ScrapeError,"invalid rt tag[#{child}] without a ruby tag at URL[#{@url}]"
else
dicwin_id = nil
if name == 'a'
id = parse_dicwin_id(child['id'].to_s)
klass = Util.unspace_web_str(child['class'].to_s).downcase
if klass == 'dicwin' && !id.nil?
if dicwin
raise ScrapeError,"invalid dicWin class[#{child}] nested inside another dicWin class at" \
" URL[#{@url}]"
end
dicwin_id = id
end
end
if dicwin_id.nil?
grand_children = child.children.to_a
(grand_children.length - 1).downto(0).each do |i|
children.push(grand_children[i])
end
else
words << scrape_dicwin_word(child,dicwin_id,result: result)
end
end
words&.each do |word|
result.add_word(word) unless word.nil?
end
end
return result
end
|
#scrape_year(doc, futsuurl = nil) ⇒ Object
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
|
# File 'lib/nhkore/article_scraper.rb', line 585
def scrape_year(doc,futsuurl=nil)
tag = doc.css('body')
if tag.length > 0
tag_id = tag[0]['id'].to_s.gsub(/[^[[:digit:]]]+/,'')
if tag_id.length >= 4
year = tag_id[0..3].to_i
return year if Util.sane_year?(year)
end
end
if !futsuurl.nil?
m = futsuurl.match(/([[:digit:]]{4,})/)
if !m.nil? && (m = m[0].to_s).length >= 4
year = m[0..3].to_i
return year if Util.sane_year?(year)
end
end
return @year.to_i unless @year.nil?
return @datetime.year if !@datetime.nil? && Util.sane_year?(@datetime.year)
raise ScrapeError,"could not scrape year at URL[#{@url}]"
end
|
#split(str) ⇒ Object
617
618
619
|
# File 'lib/nhkore/article_scraper.rb', line 617
def split(str)
return @splitter.split(str)
end
|
#variate(str) ⇒ Object
621
622
623
624
625
626
627
628
629
|
# File 'lib/nhkore/article_scraper.rb', line 621
def variate(str)
variations = []
@variators.each do |variator|
variations.push(*variator.variate(str))
end
return variations
end
|
#warn_or_error(klass, msg) ⇒ Object
631
632
633
634
635
636
637
|
# File 'lib/nhkore/article_scraper.rb', line 631
def warn_or_error(klass,msg)
if @strict
raise klass,msg
else
Util.warn(msg)
end
end
|