Class: NHKore::ArticleScraper

Inherits:
Scraper
  • Object
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

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.

Parameters:

  • dict (Dict, :scrape, nil) (defaults to: :scrape)

    the Dict (dictionary) to use for Word#defn (definitions)

    :scrape

    auto-scrape it using DictScraper

    nil

    don’t scrape/use it

  • missingno (Missingno) (defaults to: nil)

    data to use as a fallback for Ruby words without kana/kanji, instead of raising an error

  • strict (true, false) (defaults to: true)


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

#cleanersObject (readonly)

Returns the value of attribute cleaners.



32
33
34
# File 'lib/nhkore/article_scraper.rb', line 32

def cleaners
  @cleaners
end

#datetimeObject

Returns the value of attribute datetime.



33
34
35
# File 'lib/nhkore/article_scraper.rb', line 33

def datetime
  @datetime
end

#dictObject

Returns the value of attribute dict.



34
35
36
# File 'lib/nhkore/article_scraper.rb', line 34

def dict
  @dict
end

#kargsObject (readonly)

Returns the value of attribute kargs.



35
36
37
# File 'lib/nhkore/article_scraper.rb', line 35

def kargs
  @kargs
end

#missingnoObject

Returns the value of attribute missingno.



36
37
38
# File 'lib/nhkore/article_scraper.rb', line 36

def missingno
  @missingno
end

#polishersObject (readonly)

Returns the value of attribute polishers.



37
38
39
# File 'lib/nhkore/article_scraper.rb', line 37

def polishers
  @polishers
end

#splitterObject

Returns the value of attribute splitter.



38
39
40
# File 'lib/nhkore/article_scraper.rb', line 38

def splitter
  @splitter
end

#variatorsObject (readonly)

Returns the value of attribute variators.



40
41
42
# File 'lib/nhkore/article_scraper.rb', line 40

def variators
  @variators
end

#yearObject

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|
    # Words should have already been cleaned.
    # If we don't check this, Word.new() could raise an error in polish().
    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?

      # Do not pass in "word: word". We only want defn & eng.
      # If we pass in kanji/kana & unknown, it will raise an error.
      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_htmlObject



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
  # Fixes:
  # - '<「<' without escaping '<' as '&lt;'
  #   - https://www3.nhk.or.jp/news/easy/k10012118911000/k10012118911000.html
  #   - '</p><br><「<ruby>台風<rt>たいふう</rt></ruby>'

  read

  # To add a new one, simply add '|(...)' on a newline and test Regexp.last_match().
  @str_or_io = @str_or_io.gsub(/
    (?<cane><「<)
  /x) do |match|
    if !Regexp.last_match(:cane).nil?
      match = match.sub('<','&lt;')
    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:]]]+/,'') # Remove: [ ] \s
  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-') # 'id-0000'
    str = str.gsub(/\D+/,'')
  else # 'RSHOK-K-003806'
    # Same.
  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

#scrapeObject



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

Raises:



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

  # - https://www3.nhk.or.jp/news/easy/tsunamikeihou/index.html
  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? # For scrape_sha256_only()

      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

Raises:



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)

  # First, try with the id.
  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
      # Ignore; try again below.
      Util.warn("could not parse date time[#{tag_text}] from tag[#{tag_name}] at URL[#{@url}]: #{e}")
    end
  end

  # Second, try with the class.
  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
      # Ignore; try again below.
      Util.warn("could not parse date time[#{tag_text}] from tag[#{tag_name}] at URL[#{@url}]: #{e}")
    end
  end

  # Third, try body's id.
  # - https://www3.nhk.or.jp/news/easy/article/disaster_earthquake_02.html
  # - 'news20170331_k10010922481000'
  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

  # As a last resort, use our user-defined fallback (if specified).
  return @datetime unless @datetime.nil?

  raise ScrapeError,"could not scrape date time at URL[#{@url}]"
end

#scrape_dictObject



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_onlyObject

Raises:



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

  # - https://www3.nhk.or.jp/news/easy/article/disaster_earthquake_02.html
  # - 'news20170331_k10010922481000'
  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

Raises:



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 # Add trailing kana (or kanji) to kanji
    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) # Don't call dicwin_result.polish!()
  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)
  # First, try with the id.
  tag = doc.css('div#js-regular-news-wrapper')

  if tag.length > 0
    link = scrape_link(tag[0])

    return link unless link.nil?
  end

  # Second, try with the class.
  tag = doc.css('div.link-to-normal')

  if tag.length > 0
    link = scrape_link(tag[0])

    return link unless link.nil?
  end

  # Some sites don't have a futsuurl and need a lenient mode.
  # - https://www3.nhk.or.jp/news/easy/article/disaster_earthquake_02.html
  warn_or_error(ScrapeError,"could not scrape futsuurl at URL[#{@url}]")

  return nil
end


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

Raises:



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)
  # No cleaning; raw text.
  # Do not add kana to the text.
  result.add_text(word.kanji)

  kanji = clean(word.kanji)
  kana = clean(word.kana)

  # Even though Word.scrape_ruby_tag() also does this,
  #   check it again after cleaning above.
  if !@missingno.nil?
    # Check kana first, since this is the typical scenario.
    # - https://www3.nhk.or.jp/news/easy/k10012331311000/k10012331311000.html
    # - '窓' in '(8)窓を開けて外の空気を入れましょう'
    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_onlyObject



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) # Raw spaces for output

    return nil
  end

  # Kanji only for:
  # - https://www3.nhk.or.jp/news/easy/k10012639271000/k10012639271000.html
  #   - '第3のビール'
  text = word.word # Should usually be kana only

  result.add_text(text) # No cleaning; raw text

  text = clean(text)

  return nil if text.empty? # No error; empty text is fine here

  word = Word.new(
    kana: clean(word.kana),
    kanji: clean(word.kanji),
    word: word,
  )

  return word
end

#scrape_title(doc, article) ⇒ Object

Raises:



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)
  # Not grabbing `<head><title>` because it doesn't have `<ruby>` tags.

  tag = doc.css('h1.article-main__title')
  tag_name = nil

  if tag.length < 1
    # - https://www3.nhk.or.jp/news/easy/em2024081312029/em2024081312029.html
    tag = doc.css('h1.article-title') # No warning.
  end

  if tag.length < 1
    # - https://www3.nhk.or.jp/news/easy/article/disaster_earthquake_illust.html
    tag_name = 'h1.article-eq__title'
    tag = doc.css(tag_name)
  end
  if tag.length < 1 && !@strict
    # This shouldn't be used except for select sites.
    # - https://www3.nhk.or.jp/news/easy/tsunamikeihou/index.html
    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 # A faster stack?

  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'
      # Returns an array.
      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?
        # I originally didn't use a stack-like Array and did a constant insert,
        #   but I think this is slower (moving all elements down every time).
        # However, if it's using C-like code for moving memory, then maybe it
        #   is faster?
        # Old code:
        #   children.insert(i + 1,*child.children.to_a())
        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|
      # All word-scraping methods can return nil.
      result.add_word(word) unless word.nil?
    end
  end

  return result
end

#scrape_year(doc, futsuurl = nil) ⇒ Object

Raises:



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)
  # First, try body's id.
  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

  # Second, try futsuurl.
  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

  # As a last resort, use our user-defined fallbacks (if specified).
  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