Module: SteamPrices::Updater::ClassMethods

Defined in:
lib/steam_prices/steam_updater.rb

Instance Method Summary collapse

Instance Method Details

#currency(c) ⇒ Object



22
23
24
# File 'lib/steam_prices/steam_updater.rb', line 22

def currency(c)
  return (c.nil? && SteamPrices::Country.currencies) || { c => SteamPrices::Country.get_country(c) }
end

#get_exception_priceObject



46
47
# File 'lib/steam_prices/steam_updater.rb', line 46

def get_exception_price()
end

#get_original_price(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/steam_prices/steam_updater.rb', line 39

def get_original_price(node)
  price = node.search('strike').text.match(/(\d+)(\.|,)(\d\d)/i)
  return nil if price.nil?
  price = price.captures
  (price[0] + price[2]).to_i
end

#get_price(node) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/steam_prices/steam_updater.rb', line 31

def get_price(node)
  # remove strikethrough prices
  node.search('span').remove
  return 0 if node.text.match(/FREE/i)
  price = node.text.match(/(\d+(\.|,)\d\d)/i)
  price[0].gsub!(/(\.|,)/, '').to_i if price != nil 
end

#status(price) ⇒ Object



27
28
29
# File 'lib/steam_prices/steam_updater.rb', line 27

def status(price)
  (price == nil ? :not_found : :ok)
end

#update_all(category, currency = nil, display = true) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/steam_prices/steam_updater.rb', line 116

def update_all(category, currency = nil, display = true)
  games = Hash.new

  @countries = self.currency(currency)

  @countries.each do |curr, country|
    doc = Nokogiri::HTML(open(URI.encode("http://store.steampowered.com/search/results?sort_by=Name&sort_order=ASC&category1=#{category}&cc=#{country}&v6=1&page=1")))
    gamesPerPage, totalGames = doc.search('.search_pagination_left')[0].text.match(/showing\s\d+\s-\s(\d+)\sof\s(\d+)/m).captures

    totalPages = (totalGames.to_f / gamesPerPage.to_f).ceil
    puts totalPages
    puts totalGames
    puts gamesPerPage

    exceptions = Array.new



    for i in 1..totalPages

      if display then
        printf "\n   Loading '#{country}', page %02d of %02d                                                \n", i, totalPages
        printf "   Entries % 5d - % 5d of %d                                                \n", ((i - 1) * gamesPerPage.to_i + 1), (i * gamesPerPage.to_i), totalGames
        printf "+---------+---------+---------------+--------------------------------------------\n"
        printf "|  AppID  |  Price  |    Release    |                 Game Title                 \n"
        printf "+---------+---------+---------------+--------------------------------------------\n"
      end
      
      doc.search('.search_result_row').each do |game|
        url = game.attr('href').match(/(.*store\.steampowered\.com\/(app|sub)\/([\d]+)\/)/)
        # for things like /sale/
        if !url then
          games[game.attr('href')] = Array.new if !games[game.attr('href')]
          games[game.attr('href')] << { :status => :bad_request, :game => nil}
        else
          link, type, app_id = url.captures
          
          #remove retail price and put sale price
          original_price = self.get_original_price(game.search('.search_price'))
          price = self.get_price(game.search('.search_price'))
          date = game.search('.search_released').text
          name = game.search('h4').text
           = game.search('.search_capsule img').attr('src').value

          print "|% 8s |" % app_id + "% 8.2f |" % (price.to_f / 100) + "% 14s |" % date if display
          printf " %s%" + (43 - name[0,43].length).to_s + "s\n", name[0,42], " " if display
          status = self.status(price)
          game = SteamPrices::Game.new(name, app_id, link, , date, (status == :ok ? Money.new(price, curr) : nil), (!original_price.nil? ? Money.new(original_price, curr) : nil))
          
          games[app_id.to_i] = Array.new if !games[app_id.to_i]
          games[app_id.to_i] << { :game => game, :status => status, :type => category }
        end
      end

      printf "+---------+---------+---------------+--------------------------------------------\n\n" if display
      #grab the next page only if we're not on the last page
      doc = nil
      doc = Nokogiri::HTML(open(URI.encode("http://store.steampowered.com/search/results?sort_by=Name&sort_order=ASC&category1=#{category}&cc=#{country}&v6=1&page=#{i+1}"))) if i != totalPages

    end
    
    # check if there are any special cases
    EXCEPTIONS.each do |app_id, v|
      if games.has_key? app_id then
        games[app_id].each do |g|
          price = Money.new(v, currency)                
          g[:game].price = price
        end
      end
    end
    
  end
  games      
end

#update_all_games(currency = nil, display = true) ⇒ Object



107
108
109
# File 'lib/steam_prices/steam_updater.rb', line 107

def update_all_games(currency = nil, display = true)
  self.update_all(GAME, currency, display)
end

#update_all_packs(currency = nil, display = true) ⇒ Object



111
112
113
# File 'lib/steam_prices/steam_updater.rb', line 111

def update_all_packs(currency = nil, display = true)
  self.update_all(PACK, currency, display)
end

#update_everything(currency = nil, display = true) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/steam_prices/steam_updater.rb', line 96

def update_everything(currency = nil, display = true)
  #combine the two hashes
  g = self.update_all_games(currency, display)
  p = self.update_all_packs(currency, display)
  # return g.merge(p)
  n = Hash.new
  g.each {|app_id, v| v.each {|game| n[app_id] = Array.new if !n[app_id]; n[app_id] << game }}
  p.each {|app_id, v| v.each {|game| n[app_id] = Array.new if !n[app_id]; n[app_id] << game }}
  n
end

#update_one(category, name, app_id, currency = nil) ⇒ Object

update a single one



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
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/steam_prices/steam_updater.rb', line 58

def update_one(category, name, app_id, currency = nil)      
  prices = Hash.new

  @countries = self.currency(currency)

  @countries.each do |curr, country|
    if EXCEPTIONS.key? app_id then
      v = EXCEPTIONS[app_id]
      case v.class.name
      when 'Fixnum'
        p = self.update_one(category, name, v, curr)
        price = p[curr][:price]
        status = p[curr][:status]
      when 'Float'
        # it's a price
        price = Money.new(v, curr)
        status = :ok
      else
        # it's a sub
        # get the price of the sub
      end   
      prices[curr] = { :price => price, :status => status, :original_price => nil }         
    else
      doc = Nokogiri::HTML(open(URI.encode("http://store.steampowered.com/search/results?sort_by=Name&sort_order=ASC&category1=#{category}&cc=#{country}&v6=1&page=1&term=#{name}")))
      node = doc.xpath('.//a[regex(., "/store\.steampowered\.com\/(app|sub)\/' + app_id.to_s + '/")]', Class.new {
        def regex node_set, regex
          node_set.find_all { |node| node['href'] =~ /#{regex}/ }
        end
      }.new).search('.search_price')
      sale_price = get_original_price(node)
      price = self.get_price(node)
      status = (node.empty? ? :bad_request : self.status(price))
      prices[curr] = { :price => (status == :ok ? Money.new(price, curr) : nil), :status => status, :type => category, :original_price => (!sale_price.nil? ? Money.new(sale_price, curr) : nil) }
    end
  end
  prices
end

#update_one_game(name, app_id, currency = nil) ⇒ Object



49
50
51
# File 'lib/steam_prices/steam_updater.rb', line 49

def update_one_game(name, app_id, currency = nil)
  self.update_one(GAME, name, app_id, currency)
end

#update_one_pack(name, app_id, currency = nil) ⇒ Object



53
54
55
# File 'lib/steam_prices/steam_updater.rb', line 53

def update_one_pack(name, app_id, currency = nil)
  self.update_one(PACK, name, app_id, currency)
end