Class: RumbleBundle::Scraper

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

Instance Method Summary collapse

Constructor Details

#initializeScraper

Returns a new instance of Scraper.



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

def initialize
  @base_url = 'https://www.humblebundle.com'
  @main_pages = [
    'https://www.humblebundle.com/',
    'https://www.humblebundle.com/books/',
    'https://www.humblebundle.com/mobile/']
end

Instance Method Details

#crawl_siteObject



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

def crawl_site

  @main_pages.each do |page|
    first_page = Nokogiri::HTML(open(page))
    scrape_bundle(first_page, page)

    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") }

      links.each do |link|
        scrape_bundle(Nokogiri::HTML(open(link)), link)
      end
    end
  end

end

#scrape_bundle(html, url) ⇒ Object



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

def scrape_bundle(html, url)

  bundle = {
    'name' => '',
    'tiers' => [],
    'products' => [],
    'charities' => [],
    'total_msrp' => '',
    'url' => url
  }

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

  bundle['charities'] = html.css(".charity-image-wrapper img").collect{|img| img.attr("alt")}

  #for each tier in bundle
  html.css(".main-content-row").each do |tier|

    #add tier to Bundle @tiers array
    tier_name = tier.css(".dd-header-headline").text.strip
    if tier_name.downcase.include?("charity")
      break
    end

    bundle['tiers'] << tier_name

    #and instantiate products from tier
    tier.css(".game-boxes").each do |box|
      scrape_product(box, tier_name).tap do |product|
        bundle['products'] << product
      end
    end

  end

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

  RumbleBundle::Bundle.new(bundle)

end

#scrape_product(box, tier) ⇒ Object



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

def scrape_product(box, tier)

  product = {
    'name' => '',
    'subtitle' => '',
    'bundle' => '',
    'tier' => '',
    'platforms' => [],
    'drm_free' => nil,
    'steam_key' => nil
  }

  product['name'] = box.css(".dd-image-box-caption").text.strip
  product['tier'] = tier
  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

  RumbleBundle::Product.new(product)

end