Class: RumbleBundle::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/rumble_bundle/scraper.rb

Instance Method Summary collapse

Constructor Details

#initializeScraper



3
4
5
6
7
8
9
10
# File 'lib/rumble_bundle/scraper.rb', line 3

def initialize
  # Set URLs used as starting points for scraping
  @base_url = 'https://www.humblebundle.com'
  @main_pages = [
    'https://www.humblebundle.com/software-bundle/',
    'https://www.humblebundle.com/books/',
    'https://www.humblebundle.com/mobile/']
end

Instance Method Details

#crawl_siteObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rumble_bundle/scraper.rb', line 14

def crawl_site
  # For each main page
  @main_pages.each do |page|
    # scrape the bundle from that page
    first_page = Nokogiri::HTML(open(page))
    scrape_bundle(first_page, page)

    # and if there are any sub-pages
    if first_page.at_css(".js-highlight.subtab-button")
      tab_bar = first_page.css(".js-highlight.subtab-button")
      links = tab_bar.collect{|a| @base_url + a.attr("href") }

      # scrape them for bundles too
      links.each do |link|
        scrape_bundle(Nokogiri::HTML(open(link)), link)
      end
    end
  end

end

#scrape_bundle(html, url) ⇒ Object



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
# File 'lib/rumble_bundle/scraper.rb', line 37

def scrape_bundle(html, url)

  RumbleBundle::Bundle.new.tap do |bundle|

    bundle.url = url

    # Scrape Bundle metadata

    bundle.name = html.css("title").text.chomp("(pay what you want and help charity)").strip

    bundle.charities = html.css(".hb-giftbox ~ div > .hr-tagline-popup").text.strip

    bundle.total_msrp = html.css('.hr-tagline-text').detect{|e| e.text.include?("worth")}.text.strip


    bundle.tiers = Array.new.tap do |bundle_tiers|

      # For each 'main content row' on the page, instantiate a Tier with its Products and add that Tier to the Bundle's @tiers array
      html.css(".main-content-row").each do |row|

        row_title = row.css(".dd-header-headline").text.strip

        # Stop iterating if scraper has reached a row which is not a Tier
        if row_title.downcase.include?("charity")
          break
        end

        bundle_tiers << RumbleBundle::Tier.new.tap do |tier|
          tier.description = row_title
          tier.bundle = bundle
          tier.products = Array.new.tap do |tier_products|
            row.css(".game-boxes").each do |box|
              scrape_product(box, bundle, tier).tap do |product|
                tier_products << product
              end
            end
          end
        end

      end

    end

  end unless html.at('button:contains("This Bundle is Over!")')

end

#scrape_product(box, bundle, tier) ⇒ Object

Self-explanatory



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
# File 'lib/rumble_bundle/scraper.rb', line 86

def scrape_product(box, bundle, tier)

  RumbleBundle::Product.new.tap do |product|

    product.bundle = bundle
    product.tier = tier

    product.name = box.css(".dd-image-box-caption").text.strip

    product.subtitle = if box.at_css(".subtitle")
      box.css(".subtitle .callout-msrp").remove
      if box.css(".subtitle").text.strip != ""
        box.css(".subtitle").text.strip
      else
        nil
      end
    end

    product.platforms = Array.new.tap do |platforms|
      if box.at_css(".dd-availability-icon > i.hb-android")
        platforms << 'Android'
      end

      if box.at_css(".dd-availability-icon > i.hb-linux")
        platforms << 'Linux'
      end

      if box.at_css(".dd-availability-icon > i.hb-windows")
        platforms << 'Windows'
      end

      if box.at_css(".dd-availability-icon > i.hb-osx")
        platforms << 'Mac'
      end

    end

    product.drm_free = box.at_css(".dd-availability-icon > i.hb-drmfree") ?
      true : false

    product.steam_key = box.at_css(".dd-availability-icon > i.hb-steam") ?
      true : false

  end

end