-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_scraper.rb
executable file
·63 lines (52 loc) · 1.52 KB
/
game_scraper.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
#!/usr/bin/ruby
#
require "nokogiri"
require "bunny"
require "httparty"
require "json"
require "couchrest"
require "yaml"
class Scraper
attr_reader :cards_hash
def initialize(config)
connection = "http://#{config["server"]}:#{config["port"]}/#{config["database"]}"
@games = Array.new()
@db = CouchRest.database!(connection)
end
def scrape()
url = "http://www.steamcardexchange.net/index.php?showcase-filter-all"
html = HTTParty.get(url).body
doc = Nokogiri::HTML(html)
game_list = doc.css('a.showcase-game')
game_list.each do |game|
game_id = game.attributes["href"].value.split("-")[2].to_i
game_name = game.children.text.to_s
@games.concat([{"game_name" => game_name, "game_id" => game_id}])
end
end
def create_unique()
@games.each do |game|
params = {"key" => game["game_id"]}
doc = @db.view("games/exists_by_id", params)
if doc["rows"].length == 0
new_doc = {"game_name" => nil, "game_id" => game["game_id"]}
@db.save_doc(new_doc)
end
end
end
end
config = YAML.load_file("config.yaml")
conn = Bunny.new()
conn.start()
channel = conn.create_channel()
queue = channel.queue("game_scraper")
queue.subscribe(:block => true, :ack => true) do |devlivery_info, properties, payload|
payload = JSON.parse(payload)
puts "[+] Accepted message to publish"
s = Scraper.new(config)
puts "[+] Beginning scrap of games"
s.scrape
puts "[+] Writing out data to DB"
s.create_unique
puts "[+] Finished scraping and write"
end