bnet_scraper 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,12 @@
1
1
  # Changelog!
2
2
 
3
+ ## 0.2.0 (Aug 03 2012)
4
+
5
+ * Adds domain remapping to regions
6
+ * Fixes bug with EU profile scraping due to language selection
7
+ * Parses new player stats (Career Wins, Most Played Type, Games This Season)
8
+ * Parses new league stats (Current/Highest Solo/Team Leagues)
9
+
3
10
  ## 0.1.3 (Jul 26 2012)
4
11
 
5
12
  * Adds checks to prevent parse issues with accounts that don't ladder
data/README.md CHANGED
@@ -41,6 +41,13 @@ scraper.scrape
41
41
  race: 'Protoss',
42
42
  wins: '684',
43
43
  achievement_points: '3630',
44
+ current_solo_league: 'Not Yet Ranked',
45
+ highest_solo_league: 'Platinum',
46
+ current_team_league: 'Not Yet Ranked',
47
+ highest_team_league: 'Diamond',
48
+ career_games: '1568',
49
+ games_this_season: '0',
50
+ most_played: '4v4',
44
51
  leagues: [
45
52
  {
46
53
  name: "1v1 Platinum Rank 95",
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "bnet_scraper"
6
- s.version = "0.1.3"
6
+ s.version = "0.2.0"
7
7
  s.authors = ["Andrew Nordman"]
8
8
  s.email = ["anordman@majorleaguegaming.com"]
9
9
  s.homepage = "https://github.com/agoragames/bnet_scraper/"
@@ -12,10 +12,19 @@ module BnetScraper
12
12
  module Starcraft2
13
13
  REGIONS = {
14
14
  'na' => { domain: 'us.battle.net', dir: 'en', label: 'North America' },
15
- 'eu' => { domain: 'eu.battle.net', dir: 'eu', label: 'Europe' },
15
+ 'eu' => { domain: 'eu.battle.net', dir: 'en', label: 'Europe' },
16
16
  'cn' => { domain: 'www.battlenet.com.cn', dir: 'zh', label: 'China' },
17
17
  'sea' => { domain: 'sea.battle.net', dir: 'en', label: 'South-East Asia' },
18
- 'fea' => { domain: 'tw.battle.net', dir: 'zh', label: 'Korea' }
18
+ 'fea' => { domain: 'tw.battle.net', dir: 'zh', label: 'Korea' }
19
+ }
20
+
21
+ REGION_DOMAINS = {
22
+ 'us.battle.net' => 'na',
23
+ 'eu.battle.net' => 'eu',
24
+ 'www.battlenet.com.cn' => 'cn',
25
+ 'sea.battle.net' => 'sea',
26
+ 'kr.battle.net' => 'fea',
27
+ 'tw.battle.net' => 'fea'
19
28
  }
20
29
 
21
30
  # This is a convenience method that chains calls to ProfileScraper,
@@ -20,7 +20,7 @@ module BnetScraper
20
20
  def initialize options = {}
21
21
  if options[:url]
22
22
  extracted_data = options[:url].match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.[^\/]+)\//)
23
- @region = REGIONS.select { |k,v| v[:domain] == extracted_data[1] && v[:dir] == extracted_data[2] }.first.first
23
+ @region = REGION_DOMAINS[extracted_data[1]]
24
24
  @bnet_id = extracted_data[3]
25
25
  @bnet_index = extracted_data[4]
26
26
  @account = extracted_data[5]
@@ -58,7 +58,7 @@ module BnetScraper
58
58
  # converts region short-code to region-based URL information
59
59
  # 'na' => { domain: 'us.battle.net', :dir: 'en' }
60
60
  def region_info
61
- REGIONS[region]
61
+ REGIONS[region]
62
62
  end
63
63
 
64
64
  def valid?
@@ -12,6 +12,13 @@ module BnetScraper
12
12
  # race: 'Protoss',
13
13
  # wins: '684',
14
14
  # achievement_points: '3630',
15
+ # current_solo_league: 'Not Yet Ranked',
16
+ # highest_solo_league: 'Platinum',
17
+ # current_team_league: 'Not Yet Ranked',
18
+ # highest_team_league: 'Diamond',
19
+ # career_games: '1568',
20
+ # games_this_season: '0',
21
+ # most_played: '4v4',
15
22
  # leagues: [
16
23
  # {
17
24
  # name: "1v1 Platinum Rank 95",
@@ -21,7 +28,15 @@ module BnetScraper
21
28
  # ]
22
29
  # }
23
30
  class ProfileScraper < BaseScraper
24
- attr_reader :achievement_points, :wins, :race, :leagues
31
+ attr_reader :achievement_points, :career_games, :race, :leagues, :most_played,
32
+ :games_this_season, :highest_solo_league, :current_solo_league, :highest_team_league,
33
+ :current_team_league
34
+
35
+ def initialize options = {}
36
+ super
37
+ @leagues = []
38
+ end
39
+
25
40
  def scrape
26
41
  get_profile_data
27
42
  get_league_list
@@ -35,12 +50,29 @@ module BnetScraper
35
50
  if response.success?
36
51
  html = Nokogiri::HTML(response.body)
37
52
 
38
- if race_div = html.css("#season-snapshot .module-footer a").first()
39
- @race = race_div.inner_html()
40
- end
41
53
 
42
- @wins = html.css("#career-stats h2").inner_html()
54
+ @race = html.css(".stat-block:nth-child(4) h2").inner_html()
43
55
  @achievement_points = html.css("#profile-header h3").inner_html()
56
+ @career_games = html.css(".stat-block:nth-child(3) h2").inner_html()
57
+ @most_played = html.css(".stat-block:nth-child(2) h2").inner_html()
58
+ @games_this_season = html.css(".stat-block:nth-child(1) h2").inner_html()
59
+
60
+ if html.css("#best-finish-SOLO div")[0]
61
+ @highest_solo_league = html.css("#best-finish-SOLO div")[0].children[2].inner_text.strip
62
+ @current_solo_league = html.css("#best-finish-SOLO div")[0].children[8].inner_text.strip
63
+ else
64
+ @highest_solo_league = "Not Yet Ranked"
65
+ @current_solo_league = "Not Yet Ranked"
66
+ end
67
+
68
+ if html.css("#best-finish-TEAM div")[0]
69
+ @highest_team_league = html.css("#best-finish-TEAM div")[0].children[2].inner_text.strip
70
+ @current_team_league = html.css("#best-finish-TEAM div")[0].children[8].inner_text.strip
71
+ else
72
+ @highest_team_league = "Not Yet Ranked"
73
+ @current_team_league = "Not Yet Ranked"
74
+ end
75
+
44
76
  else
45
77
  raise BnetScraper::InvalidProfileError
46
78
  end
@@ -70,7 +102,13 @@ module BnetScraper
70
102
  account: @account,
71
103
  bnet_index: @bnet_index,
72
104
  race: @race,
73
- wins: @wins,
105
+ current_solo_league: @current_solo_league,
106
+ highest_solo_league: @highest_solo_league,
107
+ current_team_league: @current_team_league,
108
+ highest_team_league: @highest_team_league,
109
+ career_games: @career_games,
110
+ games_this_season: @games_this_season,
111
+ most_played: @most_played,
74
112
  achievement_points: @achievement_points,
75
113
  leagues: @leagues
76
114
  }
@@ -30,9 +30,11 @@ module BnetScraper
30
30
  private
31
31
  def status? region
32
32
  @status ||= fetch
33
- @status.select do |r|
34
- r[:region] == REGIONS.select { |k,v| k == region.to_s }.first.last[:label]
35
- end.first[:status]
33
+ region_status = @status.find do |r|
34
+ r[:region] == REGIONS[region.to_s][:label]
35
+ end
36
+
37
+ region_status[:status]
36
38
  end
37
39
  end
38
40
  end
@@ -9,25 +9,27 @@ describe BnetScraper::Starcraft2::ProfileScraper do
9
9
  subject { BnetScraper::Starcraft2::ProfileScraper.new(bnet_id: '2377239', account: 'Demon') }
10
10
 
11
11
  describe '#get_profile_data' do
12
- it 'should set the race, wins, and achievements attributes' do
13
- subject.instance_variable_get(:@race).should be_nil
14
- subject.instance_variable_get(:@achievement_points).should be_nil
15
- subject.instance_variable_get(:@wins).should be_nil
16
-
12
+ before do
17
13
  subject.get_profile_data
18
-
19
- subject.instance_variable_get(:@race).should == 'Protoss'
20
- subject.instance_variable_get(:@achievement_points).should == '3630'
21
- subject.instance_variable_get(:@wins).should == '684'
22
14
  end
15
+
16
+ its(:race) { should == 'Protoss' }
17
+ its(:achievement_points) { should == '3660' }
18
+ its(:current_solo_league) { should == 'Not Yet Ranked' }
19
+ its(:highest_solo_league) { should == 'Platinum' }
20
+ its(:current_team_league) { should == 'Not Yet Ranked' }
21
+ its(:highest_team_league) { should == 'Diamond' }
22
+ its(:career_games) { should == '1568' }
23
+ its(:games_this_season) { should == '0' }
24
+ its(:most_played) { should == '4v4' }
23
25
  end
24
26
 
25
27
  describe 'get_league_list' do
26
28
  it 'should set an array of leagues' do
27
- subject.instance_variable_get(:@leagues).should be_nil
29
+ subject.should have(0).leagues
28
30
  subject.get_league_list
29
31
 
30
- subject.instance_variable_get(:@leagues).should have(12).leagues
32
+ subject.should have(12).leagues
31
33
  end
32
34
  end
33
35
 
@@ -59,10 +61,6 @@ describe BnetScraper::Starcraft2::ProfileScraper do
59
61
  before do
60
62
  scraper.scrape
61
63
  end
62
-
63
- it 'should set nil race' do
64
- scraper.race.should be_nil
65
- end
66
64
 
67
65
  it 'should have an empty array of leagues' do
68
66
  scraper.leagues.should == []
@@ -77,73 +75,94 @@ describe BnetScraper::Starcraft2::ProfileScraper do
77
75
  account: 'Demon',
78
76
  bnet_index: 1,
79
77
  race: 'Protoss',
80
- wins: '684',
81
- achievement_points: '3630',
78
+ career_games: '1568',
79
+ games_this_season: '0',
80
+ highest_solo_league: 'Platinum',
81
+ current_solo_league: 'Not Yet Ranked',
82
+ highest_team_league: 'Diamond',
83
+ current_team_league: 'Not Yet Ranked',
84
+ most_played: '4v4',
85
+ achievement_points: '3660',
82
86
  leagues: [
83
87
  {
84
- name: "1v1 Platinum Rank 95",
85
- id: "96905",
88
+ name: "1v1 Platinum Rank 95",
89
+ id: "96905",
86
90
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96905#current-rank"
87
- },
91
+ },
88
92
  {
89
- name: "2v2 Random Platinum ...",
90
- id: "96716",
93
+ name: "2v2 Random Platinum ...",
94
+ id: "96716",
91
95
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96716#current-rank"
92
96
  },
93
97
  {
94
98
  name: "2v2 Diamond Rank 45",
95
- id: "98162",
99
+ id: "98162",
96
100
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98162#current-rank"
97
- },
101
+ },
98
102
  {
99
103
  name: "2v2 Silver Rank 8",
100
- id: "97369",
104
+ id: "97369",
101
105
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97369#current-rank"
102
106
  },
103
107
  {
104
108
  name: "3v3 Random Gold Rank...",
105
- id: "96828",
109
+ id: "96828",
106
110
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96828#current-rank"
107
- },
111
+ },
108
112
  {
109
113
  name: "3v3 Diamond Rank 56",
110
- id: "97985",
114
+ id: "97985",
111
115
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97985#current-rank"
112
- },
116
+ },
113
117
  {
114
118
  name: "3v3 Silver Rank 5",
115
- id: "98523",
119
+ id: "98523",
116
120
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98523#current-rank"
117
121
  },
118
122
  {
119
- name: "3v3 Platinum Rank 88",
120
- id: "96863",
123
+ name: "3v3 Platinum Rank 88",
124
+ id: "96863",
121
125
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96863#current-rank"
122
126
  },
123
127
  {
124
- name: "3v3 Gold Rank 75",
125
- id: "97250",
128
+ name: "3v3 Gold Rank 75",
129
+ id: "97250",
126
130
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97250#current-rank"
127
131
  },
128
132
  {
129
133
  name: "4v4 Random Platinum ...",
130
- id: "96830",
134
+ id: "96830",
131
135
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96830#current-rank"
132
136
  },
133
137
  {
134
138
  name: "4v4 Gold Rank 38",
135
- id: "98336",
139
+ id: "98336",
136
140
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98336#current-rank"
137
141
  },
138
142
  {
139
143
  name: "4v4 Diamond Rank 54",
140
- id: "98936",
144
+ id: "98936",
141
145
  href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98936#current-rank"
142
- }
146
+ }
143
147
  ]
144
148
  }
145
149
 
146
- subject.output.should == { bnet_id: '2377239', account: 'Demon', bnet_index: 1, race: nil, wins: nil, achievement_points: nil, leagues: nil }
150
+ subject.output.should == {
151
+ bnet_id: '2377239',
152
+ account: 'Demon',
153
+ bnet_index: 1,
154
+ race: nil,
155
+ career_games: nil,
156
+ games_this_season: nil,
157
+ most_played: nil,
158
+ highest_solo_league: nil,
159
+ current_solo_league: nil,
160
+ highest_team_league: nil,
161
+ current_team_league: nil,
162
+ achievement_points: nil,
163
+ leagues: []
164
+ }
165
+
147
166
  subject.scrape
148
167
  subject.output.should == expected
149
168
  end
@@ -8,8 +8,14 @@ describe BnetScraper::Starcraft2 do
8
8
  :account=>"Demon",
9
9
  :bnet_index=>1,
10
10
  :race=>"Protoss",
11
- :wins=>"684",
12
- :achievement_points=>"3630",
11
+ :career_games => '1568',
12
+ :games_this_season => '0',
13
+ :most_played => '4v4',
14
+ :current_solo_league => 'Not Yet Ranked',
15
+ :highest_solo_league => 'Platinum',
16
+ :current_team_league => 'Not Yet Ranked',
17
+ :highest_team_league => 'Diamond',
18
+ :achievement_points=>"3660",
13
19
  :leagues=>[
14
20
  {:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
15
21
  {:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
@@ -125,6 +125,7 @@ _gaq.push(['_trackPageLoadTime']);
125
125
  <a href="/sc2/en/" rel="np">
126
126
  StarCraft II
127
127
  </a>
128
+ <span class="breadcrumb-arrow"></span>
128
129
  </li>
129
130
  <li class="last">
130
131
  <a href="/sc2/en/profile/3354437/1/ClarkeKent/" rel="np">
@@ -230,54 +231,96 @@ ClarkeKent
230
231
  <div id="profile-right">
231
232
 
232
233
  <div class="profile-module">
234
+
233
235
  <div class="module-top">
234
236
  <div class="module-bot">
235
237
  <div class="module-left" id="season-snapshot">
236
238
  <div class="module-title">
237
- <h3 class="title-globe">
238
- Season 8 <span>-</span>
239
- Snapshot
240
- </h3>
239
+ <h3 class="title-globe">Player Stats</h3>
241
240
  </div>
242
241
 
242
+ <div class="module-body snapshot-terran">
243
+
244
+ <div class="stat-block">
245
+ <h3>Games Played This Season</h3>
246
+ <h2>0</h2>
247
+ </div>
248
+
249
+ <div class="stat-block">
250
+ <h3>Most Played Mode</h3>
251
+ <h2>1v1</h2>
252
+ </div>
253
+
254
+ <div class="stat-block">
255
+ <h3>Total Career Games</h3>
256
+ <h2>2</h2>
257
+ </div>
258
+
259
+ <div class="stat-block">
260
+ <h3>Most Played Race</h3>
261
+ <h2 class="race">Terran</h2>
262
+ </div>
243
263
 
244
- <div class="module-body snapshot-empty">
245
- <h3>No games have been played.</h3>
246
264
  </div>
247
265
  </div>
248
266
 
249
267
  <div class="module-right" id="career-stats">
250
- <div class="module-title">
251
- <h3 class="title-graph">Career Stats</h3>
252
- </div>
268
+ <div class="module-title"></div>
253
269
 
270
+ <div class="module-body">
271
+
254
272
 
255
273
 
256
- <div class="module-body campaign-unearned">
257
- <h4>League Wins</h4>
258
- <h2>0</h2>
259
274
 
260
- <br />
261
- <h4>Games Won</h4>
262
-
263
- <ul>
264
- <li>
265
- <span>0</span>
266
- Custom Games
267
- </li>
268
- <li>
269
- <span>0</span>
270
- FFA
271
- </li>
272
- <li>
273
- <span>0</span>
274
- Co-Op vs AI
275
- </li>
276
- </ul>
275
+ <div class="badge-item" data-tooltip="#best-finish-SOLO">
276
+
277
+ <div class="badge">
278
+
279
+ <span class="badge badge-none badge-medium-1">
280
+ </span>
281
+ </div>
282
+ <div class="mode">1V1</div>
283
+ <div class="league-name">
284
+ No Career Finishes
285
+ </div>
286
+
287
+ <div id="best-finish-SOLO" style="display: none">
288
+ Not Yet Ranked
289
+ </div>
290
+ </div>
291
+
292
+
293
+
294
+
295
+
296
+ <div class="badge-item" data-tooltip="#best-finish-TEAM">
297
+
298
+ <div class="badge">
299
+
300
+ <span class="badge badge-none badge-medium-1">
301
+ </span>
302
+ </div>
303
+ <div class="mode">TEAM</div>
304
+ <div class="league-name">
305
+ No Career Finishes
306
+ </div>
307
+
308
+ <div id="best-finish-TEAM" style="display: none">
309
+ Not Yet Ranked
310
+ </div>
311
+ </div>
277
312
 
278
313
  <br />
279
314
 
280
- <h4>Campaign Not Complete</h4>
315
+
316
+
317
+ <div class="badge-item campaign">
318
+ <div class="badge unearned"></div>
319
+ <div class="mode">Wings of Liberty</div>
320
+ <div class="rank">Campaign Not Complete</div>
321
+ </div>
322
+
323
+ <span class="clear"><!-- --></span>
281
324
  </div>
282
325
  </div>
283
326
 
@@ -880,10 +923,18 @@ Login.embeddedUrl = 'https://us.battle.net/login/login.frag';
880
923
  </script>
881
924
  <script type="text/javascript" src="/sc2/static/local-common/js/menu.js?v42"></script>
882
925
  <script type="text/javascript" src="/sc2/static/js/sc2.js?v21"></script>
926
+ <!--[if IE 6]> <script type="text/javascript" src="/sc2/static/local-common/js/third-party/DD_belatedPNG.js?v42"></script>
927
+ <script type="text/javascript">
928
+ //<![CDATA[
929
+ DD_belatedPNG.fix('.icon');
930
+ //]]>
931
+ </script>
932
+ <![endif]-->
883
933
  <script type="text/javascript">
884
934
  //<![CDATA[
885
935
  $(function(){
886
936
  Menu.initialize('/data/menu.json?v42');
937
+ Search.initialize('/sc2/en/search/ta');
887
938
  Tooltip.options.useTable = true;
888
939
  });
889
940
  //]]>
@@ -913,3 +964,4 @@ s.parentNode.insertBefore(ga, s.nextSibling);
913
964
  </script>
914
965
  </body>
915
966
  </html>
967
+