-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-extractor.rb
65 lines (48 loc) · 1.5 KB
/
data-extractor.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
require 'nokogiri'
require 'open-uri'
require 'csv'
require 'pg'
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'anonynoob',
username: 'postgres'
)
class Match < ActiveRecord::Base
end
match_id = 1290371320
matches_recorded = 0
while matches_recorded < 3000 do
puts "Opening Match ID: http://www.dotabuff.com/matches/"+match_id.to_s
begin
doc = Nokogiri::HTML(open('http://www.dotabuff.com/matches/'+match_id.to_s))
match = Match.new
match.match_id = match_id
match.anonymous_radiant = 0
match.anonymous_dire = 0
doc.css(".faction-radiant").each do |result_line|
match.anonymous_radiant += 1 if result_line.css("td")[1].text.include? "Anonymous"
end
puts "Radiant had " + match.anonymous_radiant.to_s + " anonymous players."
doc.css(".faction-dire").each do |result_line|
match.anonymous_dire += 1 if result_line.css("td")[1].text.include? "Anonymous"
end
puts "Dire had " + match.anonymous_dire.to_s + " anonymous players."
if doc.css(".match-result").text == "Radiant Victory"
match.victory = "radiant"
else
match.victory = "dire"
end
puts match.victory.capitalize + " won the match."
match.save
puts "Match saved in DB."
matches_recorded += 1
rescue
puts "Page not found"
end
puts "This scraping session is at " + matches_recorded.to_s + " matches recorded."
puts "Done. Moving to next match."
match_id -= 1
sleep(1)
end