Skip to content

Commit cb75e17

Browse files
committed
renamed bnet_index to subregion, removed guessing and added all known regions and subregions instead
1 parent 5e57141 commit cb75e17

File tree

9 files changed

+34
-75
lines changed

9 files changed

+34
-75
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ scraper.scrape
3737
# => {
3838
bnet_id: '2377239',
3939
account: 'Demon',
40-
bnet_index: 1,
40+
subregion: 1,
4141
race: 'Protoss',
4242
wins: '684',
4343
achievement_points: '3630',
@@ -138,7 +138,7 @@ Scraping is only possible if the site is up. Use this if you want to verify the
138138

139139
``` ruby
140140
BnetScraper::Starcraft2::Status.na # => 'Online'
141-
BnetScraper::Starcraft2::Status.fea # => 'Offline'
141+
BnetScraper::Starcraft2::Status.kr # => 'Offline'
142142
BnetScraper::Starcraft2::Status.cn # => nil (China is unsupported)
143143
BnetScraper::Starcraft2::Status.fetch # => [
144144
{:region=>"North America", :status=>"Online"},

lib/bnet_scraper/starcraft2.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@ module BnetScraper
1111
# for more details
1212
module Starcraft2
1313
REGIONS = {
14-
'na' => { domain: 'us.battle.net', dir: 'en', label: 'North America' },
15-
'eu' => { domain: 'eu.battle.net', dir: 'en', label: 'Europe' },
16-
'cn' => { domain: 'www.battlenet.com.cn', dir: 'zh', label: 'China' },
17-
'sea' => { domain: 'sea.battle.net', dir: 'en', label: 'South-East Asia' },
18-
'fea' => { domain: 'tw.battle.net', dir: 'zh', label: 'Korea' }
14+
'na' => { domain: 'us.battle.net', subregion: 1, lang: 'en', label: 'North America' },
15+
'la' => { domain: 'us.battle.net', subregion: 2, lang: 'en', label: 'Latin America' },
16+
'eu' => { domain: 'eu.battle.net', subregion: 1, lang: 'en', label: 'Europe' },
17+
'ru' => { domain: 'eu.battle.net', subregion: 2, lang: 'en', label: 'Russia' },
18+
'cn' => { domain: 'www.battlenet.com.cn', subregion: 1, lang: 'zh', label: 'China' },
19+
'sea' => { domain: 'sea.battle.net', subregion: 1, lang: 'en', label: 'South-East Asia' },
20+
# Note: KR/TW are technically the same, it appears.
21+
'kr' => { domain: 'kr.battle.net', subregion: 1, lang: 'ko', label: 'Korea' },
22+
'tw' => { domain: 'tw.battle.net', subregion: 1, lang: 'zh', label: 'Taiwan' }
1923
}
2024

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'
28-
}
25+
REGION_DOMAINS = Hash[*REGIONS.collect{|region, data|
26+
[[data[:domain], data[:subregion]], region]
27+
}.flatten(1)]
2928

3029
# The armory uses spritemaps that are sequentially named and have a fixed
3130
# 6x6 grid. We'll simply use the portrait names, left to right, top to

lib/bnet_scraper/starcraft2/base_scraper.rb

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ module Starcraft2
1515
# scraper uses the short-codes for regions. See `BnetScraper::Starcraft2::REGIONS` for the address
1616
# translations.
1717
class BaseScraper
18-
attr_reader :bnet_id, :account, :region, :bnet_index, :url
18+
attr_reader :bnet_id, :account, :region, :subregion, :url
1919

2020
def initialize options = {}
2121
if options[:url]
2222
extracted_data = options[:url].match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.[^\/]+)\//)
2323
if extracted_data
24-
@region = REGION_DOMAINS[extracted_data[1]]
24+
@subregion = extracted_data[4].to_i
25+
@region = REGION_DOMAINS[[extracted_data[1], @subregion]]
2526
@bnet_id = extracted_data[3]
26-
@bnet_index = extracted_data[4]
2727
@account = extracted_data[5]
2828
@url = options[:url]
2929
else
@@ -33,34 +33,16 @@ def initialize options = {}
3333
@bnet_id = options[:bnet_id]
3434
@account = options[:account]
3535
@region = options[:region] || 'na'
36-
if options[:bnet_index]
37-
@bnet_index = options[:bnet_index]
38-
else
39-
set_bnet_index
40-
end
41-
end
42-
end
43-
44-
# set_bnet_index
45-
#
46-
# Because profile URLs have to have a specific bnet_index that is seemingly incalculable,
47-
# we must ping both variants to determine the correct bnet_index. We then store that value.
48-
def set_bnet_index
49-
[1,2].each do |idx|
50-
res = Net::HTTP.get_response URI profile_url idx
51-
if res.is_a? Net::HTTPSuccess
52-
@bnet_index = idx
53-
return
54-
end
36+
@subregion = options[:subregion] || REGIONS[@region][:subregion]
5537
end
5638
end
5739

58-
def profile_url bnet_index = @bnet_index
59-
"http://#{region_info[:domain]}/sc2/#{region_info[:dir]}/profile/#{bnet_id}/#{bnet_index}/#{account}/"
40+
def profile_url
41+
"http://#{region_info[:domain]}/sc2/#{region_info[:lang]}/profile/#{bnet_id}/#{subregion}/#{account}/"
6042
end
6143

6244
# converts region short-code to region-based URL information
63-
# 'na' => { domain: 'us.battle.net', :dir: 'en' }
45+
# 'na' => { domain: 'us.battle.net', :lang: 'en' }
6446
def region_info
6547
REGIONS[region]
6648
end

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Starcraft2
88
# # => {
99
# bnet_id: '2377239',
1010
# account: 'Demon',
11-
# bnet_index: 1,
11+
# subregion: 1,
1212
# race: 'Protoss',
1313
# wins: '684',
1414
# achievement_points: '3630',
@@ -118,7 +118,7 @@ def output
118118
{
119119
bnet_id: @bnet_id,
120120
account: @account,
121-
bnet_index: @bnet_index,
121+
subregion: @subregion,
122122
race: @race,
123123
current_solo_league: @current_solo_league,
124124
highest_solo_league: @highest_solo_league,

lib/bnet_scraper/starcraft2/status_scraper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Starcraft2
55
#
66
# Examples:
77
# BnetScraper::Starcraft2::Status.na => 'Online'
8-
# BnetScraper::Starcraft2::Status.fea => 'Offline'
8+
# BnetScraper::Starcraft2::Status.kr => 'Offline'
99
# BnetScraper::Starcraft2::Status.cn => nil (China is unsupported)
1010
# BnetScraper::Starcraft2::Status.fetch => [
1111
# {:region=>"North America", :status=>"Online"},{:region=>"Europe", :status=>"Online"},

spec/starcraft2/profile_scraper_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
expected = {
8787
bnet_id: '2377239',
8888
account: 'Demon',
89-
bnet_index: 1,
89+
subregion: 1,
9090
race: 'Protoss',
9191
career_games: '1568',
9292
games_this_season: '0',
@@ -164,7 +164,7 @@
164164
subject.output.should == {
165165
bnet_id: '2377239',
166166
account: 'Demon',
167-
bnet_index: 1,
167+
subregion: 1,
168168
race: nil,
169169
career_games: nil,
170170
games_this_season: nil,

spec/starcraft2/status_scraper_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
describe BnetScraper::Starcraft2::Status do
44
describe 'Each supported region' do
55
it 'should be online' do
6-
BnetScraper::Starcraft2::Status.na.should == 'Online'
76
BnetScraper::Starcraft2::Status.eu.should == 'Online'
87
BnetScraper::Starcraft2::Status.sea.should == 'Online'
9-
BnetScraper::Starcraft2::Status.fea.should == 'Online'
8+
BnetScraper::Starcraft2::Status.na.should == 'Online'
9+
BnetScraper::Starcraft2::Status.kr.should == 'Online'
1010
end
1111
end
1212

spec/starcraft2_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
expected = {
77
:bnet_id=>"2377239",
88
:account=>"Demon",
9-
:bnet_index=>1,
9+
:subregion=>1,
1010
:race=>"Protoss",
1111
:career_games => '1568',
1212
:games_this_season => '0',

spec/support/shared/sc2_scraper.rb

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
subject.account.should == 'Demon'
1010
end
1111

12-
it 'should extract the bnet_index from the URL' do
13-
subject.bnet_index.should == '1'
12+
it 'should extract the subregion from the URL' do
13+
subject.subregion.should == 1
1414
end
1515

1616
it 'should extract the region from the URL' do
@@ -30,44 +30,22 @@
3030
end
3131

3232
it 'should assign region if passed' do
33-
scraper_class.any_instance.should_receive(:set_bnet_index)
34-
scraper = scraper_class.new(bnet_id: '2377239', account: 'Demon', region: 'fea')
35-
scraper.region.should == 'fea'
36-
end
37-
38-
it 'should not call set_bnet_index if bnet_index is passed' do
39-
scraper_class.any_instance.should_not_receive(:set_bnet_index)
40-
scraper = scraper_class.new(bnet_id: '2377239', account: 'Demon', region: 'fea', bnet_index: '1')
41-
end
42-
43-
it 'should call set_bnet_index_if bnet_index is not passed' do
44-
scraper_class.any_instance.should_receive(:set_bnet_index)
45-
scraper = scraper_class.new(bnet_id: '2377239', account: 'Demon', region: 'fea')
33+
scraper = scraper_class.new(bnet_id: '2377239', account: 'Demon', region: 'tw')
34+
scraper.region.should == 'tw'
4635
end
4736
end
4837
end
4938

5039
describe '#region_info' do
5140
it 'should return information based on the set region' do
52-
subject.region_info.should == { domain: 'us.battle.net', dir: 'en', label: 'North America' }
53-
end
54-
end
55-
56-
describe '#set_bnet_index' do
57-
it 'should return the valid integer needed for a proper URL parse from bnet' do
58-
subject.set_bnet_index
59-
subject.bnet_index.should == 1
41+
subject.region_info.should == { domain: 'us.battle.net', subregion: 1, lang: 'en', label: 'North America' }
6042
end
6143
end
6244

6345
describe '#profile_url' do
6446
it 'should return a string URL for bnet' do
6547
subject.profile_url.should == 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/'
6648
end
67-
68-
it 'should optionally take a bnet_index to use instead of saved bnet_index' do
69-
subject.profile_url(2).should == 'http://us.battle.net/sc2/en/profile/2377239/2/Demon/'
70-
end
7149
end
7250

7351
describe '#valid?' do

0 commit comments

Comments
 (0)