-
Notifications
You must be signed in to change notification settings - Fork 0
/
recent_added_purchase.rb
executable file
·84 lines (67 loc) · 2.05 KB
/
recent_added_purchase.rb
1
2
3
4
5
6
7
8
9
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
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
83
84
#!/usr/bin/ruby
#
require "nokogiri"
require "bunny"
require "httparty"
require "json"
require "couchrest"
require "yaml"
class Scraper
attr_reader :average_price
def initialize()
@db = CouchRest.database("http://localhost:5984/steam")
@average_price = 0.00
end
def get_average_from_db(card_name, game_name)
rows = @db.view('_design/prices/_view/average_per_game_card', :key => [game_name, card_name])["rows"]
if rows.length > 0
@average_price = rows[0]["value"]
else
@average_price = nil
end
end
def scrape()
url = "http://steamcommunity.com/market"
html = HTTParty.get(url).body
doc = Nokogiri::HTML(html)
items = doc.css('.market_listing_row')
items_newly = items[10...20]
items_newly.each do |item|
item_name = item.css('.market_listing_item_name').text
item_name = item_name.gsub(/ \(Trading Card\)/, '')
game_name = item.css('.market_listing_game_name').text
game_name = game_name.gsub(/ Trading Card/, '')
price = item.css('.market_listing_price.market_listing_price_with_fee').text
price = price.match(/(\d*(\.|,)\d*)/)[0]
price = price.gsub(/,/, '.')
price = price.to_f
price_average = self.get_average_from_db(item_name, game_name)
puts "Found... #{item_name}, #{game_name} for #{price}, with average #{price_average}"
if (price_average.to_f * 0.7) > price
buy_it = true
end
if @average_price
puts "Item name: #{item_name}"
puts "Game name: #{game_name}"
puts "Item price: #{price}"
puts "Item average price: #{@average_price}"
puts "PRICE IS 70% OF AVERAGE" if buy_it
puts "\n"
end
end
end
def should_buy?(item_name, game_name, listed_price)
price_average = self.get_average_from_db(item_name, game_name)
if (price_average.to_f * 0.7) > listed_price
return true
else
return false
end
end
def check_list()
self.scrape
end
end
s = Scraper.new()
#s.get_average_from_db("Charger", "Left 4 Dead 2")
s.scrape