/
athlete.rb
69 lines (62 loc) · 2.34 KB
/
athlete.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
require 'open-uri'
class Athlete < ActiveRecord::Base
attr_accessible :name, :url, :w1, :w2, :w3, :w4, :w5
def self.populate_initial_data
urls = {wicked: "http://games.crossfit.com/affiliate/2793", southie: "http://games.crossfit.com/affiliate/1365"}
urls.each do |affiliate, url|
athlete_list = Nokogiri::HTML(open(url))
athlete_list.css("#block-search-athlete-affiliate-team-blocks-affiliate-athletes a").each do |athlete|
record = Athlete.find_or_initialize_by_url(athlete['href'])
record.affiliate = affiliate.to_s
record.save
end
end
end
def self.populate_details
Athlete.all.each do |athlete|
details = Nokogiri::HTML(open("http://games.crossfit.com" + athlete.url))
name = details.at_css(".page-title").text
puts name
athlete.name = name.gsub(/^[^:]+:\s*/, "")
puts athlete.name
attributes = details.xpath('//dd//text()').collect(&:text)
athlete.region = attributes[0]
athlete.team = attributes[1]
athlete.gender = attributes[3]
athlete.age = attributes[4]
athlete.height = attributes[5]
athlete.weight = attributes[6]
athlete.save
end
end
def self.populate_scores
Athlete.all.each do |athlete|
details = Nokogiri::HTML(open("http://games.crossfit.com" + athlete.url))
leaderboard_src = details.xpath('//iframe[@id="cf_leaderboard"]/@src').text()
leaderboard = Nokogiri::HTML(open(leaderboard_src))
scores = leaderboard.xpath('//table/tbody/tr[@class="highlight"]/td/span[@class="display"]/text()').collect(&:text)
athlete.w1 = scores[0]
athlete.w2 = scores[2]
athlete.w3 = scores[4]
athlete.w4 = scores[6]
athlete.w5 = scores[8]
positions = []
scores.each do |value|
if (match = value.scan(/\d/))
# a disqualified athlete will have integers for position and score
# values, so check match for 2 values
if (match[1])
positions << match[0].to_i
athlete.qualified = TRUE;
# in the case that a score shows a user is disqualified,
# such as --(186), set the disqualified state in the db
else
athlete.qualified = FALSE;
end
end
end
athlete.total = positions.inject(0) {|sum, i| sum + i}
athlete.save
end
end
end