Class: AwsWhitepaperDownloader::Crawler

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_whitepaper_downloader/crawler.rb

Instance Method Summary collapse

Constructor Details

#initializeCrawler

Returns a new instance of Crawler.



7
8
# File 'lib/aws_whitepaper_downloader/crawler.rb', line 7

def initialize
end

Instance Method Details

#runObject



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
# File 'lib/aws_whitepaper_downloader/crawler.rb', line 10

def run
  count = 0
  start = nil
  output = {}
  flag = 0
  current_hash = nil
  pdfs = nil
  title = nil
  doc = Nokogiri::HTML( open("https://aws.amazon.com/whitepapers/"))
  doc.css(".content > .title-wrapper, .content > .row-builder").each do |div|

    # div == title-wrapper
    if div['class'].match("title-wrapper")
      title = parse_title( div )
      output[title] ||= {}
      current_hash = output[title]
    end

    # div == row-builder
    next unless current_hash
    next unless div['class'].match("row-builder")
    # nested
    if div.css('h3').size > 0
      current_hash['type'] = 'nested'
      sub_divs = div.css(
        ".wrapper > .columns > .parsys > .columnbuilder,
        .wrapper > .columns > .parsys > .title-wrapper"
      )
      sub_divs.each do |sub_div|
        if subtitle = parse_subtitle( sub_div )
          current_hash[subtitle] ||= {}
          pdfs = current_hash[subtitle]
        end
        pdfs = parse_pdfs(sub_div, pdfs)
      end
    # normal
    else
      current_hash['type'] = 'normal'
      pdfs = output[title]
      pdfs = parse_pdfs(div, pdfs)
    end

  end
  return output
end