Skip to content

Commit 98066a8

Browse files
committed
Ability to check if Battle.net is online for a region
1 parent ddfca1c commit 98066a8

File tree

7 files changed

+81
-22
lines changed

7 files changed

+81
-22
lines changed

lib/bnet_scraper/starcraft2.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
require 'bnet_scraper/starcraft2/league_scraper'
44
require 'bnet_scraper/starcraft2/achievement_scraper'
55
require 'bnet_scraper/starcraft2/match_history_scraper'
6+
require 'bnet_scraper/starcraft2/status_scraper'
67

78
module BnetScraper
89
# This module contains everything about scraping Starcraft 2 Battle.net accounts.
910
# See `BnetScraper::Starcraft2::ProfileScraper` and `BnetScraper::Starcraft2::LeagueScraper`
1011
# for more details
1112
module Starcraft2
1213
REGIONS = {
13-
'na' => { domain: 'us.battle.net', dir: 'en' },
14-
'eu' => { domain: 'eu.battle.net', dir: 'eu' },
15-
'cn' => { domain: 'www.battlenet.com.cn', dir: 'zh' },
16-
'sea' => { domain: 'sea.battle.net', dir: 'en' },
17-
'fea' => { domain: 'tw.battle.net', dir: 'zh' }
14+
'na' => { domain: 'us.battle.net', dir: 'en', label: 'North America' },
15+
'eu' => { domain: 'eu.battle.net', dir: 'eu', 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' }
1819
}
1920

2021
# This is a convenience method that chains calls to ProfileScraper,

lib/bnet_scraper/starcraft2/base_scraper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class BaseScraper
2020
def initialize options = {}
2121
if options[:url]
2222
extracted_data = options[:url].match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.[^\/]+)\//)
23-
@region = REGIONS.key({ domain: extracted_data[1], dir: extracted_data[2] })
23+
@region = REGIONS.select { |k,v| v[:domain] == extracted_data[1] && v[:dir] == extracted_data[2] }.first.first
2424
@bnet_id = extracted_data[3]
2525
@bnet_index = extracted_data[4]
2626
@account = extracted_data[5]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module BnetScraper
2+
module Starcraft2
3+
# Ask the TL.net bot if Battle.Net is currently online for a given region.
4+
# This page is updated every 5 minutes. Call #fetch to refresh.
5+
#
6+
# Examples:
7+
# BnetScraper::Starcraft2::Status.na => 'Online'
8+
# BnetScraper::Starcraft2::Status.fea => 'Offline'
9+
# BnetScraper::Starcraft2::Status.cn => nil (China is unsupported)
10+
# BnetScraper::Starcraft2::Status.fetch => [
11+
# {:region=>"North America", :status=>"Online"},{:region=>"Europe", :status=>"Online"},
12+
# {:region=>"Korea", :status=>"Online"}, {:region=>"South-East Asia", :status=>"Online"}
13+
# ]
14+
class Status
15+
SOURCE = 'http://www.teamliquid.net/forum/viewmessage.php?topic_id=138846'
16+
17+
class << self
18+
19+
def fetch
20+
Nokogiri::HTML(open(SOURCE))
21+
.css('.forumPost').first.css('span').to_a
22+
.each_slice(2).map { |i| { :region => i.first.text, :status => i.last.text } }
23+
end
24+
25+
def method_missing sym
26+
status? sym if REGIONS.reject { |r| r == 'cn' }.include?(sym.to_s)
27+
end
28+
29+
private
30+
def status? region
31+
@status ||= fetch
32+
@status.select do |r|
33+
r[:region] == REGIONS.select { |k,v| k == region.to_s }.first.last[:label]
34+
end.first[:status]
35+
end
36+
end
37+
end
38+
end
39+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper'
2+
3+
describe BnetScraper::Starcraft2::Status do
4+
describe 'Each supported region' do
5+
it 'should be online' do
6+
BnetScraper::Starcraft2::Status.na.should == 'Online'
7+
BnetScraper::Starcraft2::Status.eu.should == 'Online'
8+
BnetScraper::Starcraft2::Status.sea.should == 'Online'
9+
BnetScraper::Starcraft2::Status.fea.should == 'Online'
10+
end
11+
end
12+
13+
describe 'China' do
14+
BnetScraper::Starcraft2::Status.cn.should == nil
15+
end
16+
end

spec/support/load_fakeweb.rb

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
league_html = File.read File.dirname(__FILE__) + '/league.html'
66
achievements_html = File.read File.dirname(__FILE__) + '/achievements.html'
77
matches_html = File.read File.dirname(__FILE__) + '/matches.html'
8+
status_html = File.read File.dirname(__FILE__) + '/status.html'
89

910
FakeWeb.allow_net_connect = false
1011
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/', body: profile_html, status: 200, content_type: 'text/html'
1112
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues', body: leagues_html, status: 200, content_type: 'text/html'
12-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/12345', body: league_html, status: 200, content_type: 'text/html'
13-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96905', body: league_html, status: 200, content_type: 'text/html'
14-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96716', body: league_html, status: 200, content_type: 'text/html'
15-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98162', body: league_html, status: 200, content_type: 'text/html'
16-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97369', body: league_html, status: 200, content_type: 'text/html'
17-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96828', body: league_html, status: 200, content_type: 'text/html'
18-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97985', body: league_html, status: 200, content_type: 'text/html'
19-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98523', body: league_html, status: 200, content_type: 'text/html'
20-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96863', body: league_html, status: 200, content_type: 'text/html'
21-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97250', body: league_html, status: 200, content_type: 'text/html'
22-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96830', body: league_html, status: 200, content_type: 'text/html'
23-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98336', body: league_html, status: 200, content_type: 'text/html'
24-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98936', body: league_html, status: 200, content_type: 'text/html'
25-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/achievements/', body: achievements_html, status: 200, content_type: 'text/html'
26-
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/matches', body: matches_html, status: 200, content_type: 'text/html'
13+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/12345', body: league_html, status: 200, content_type: 'text/html'
14+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96905', body: league_html, status: 200, content_type: 'text/html'
15+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96716', body: league_html, status: 200, content_type: 'text/html'
16+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98162', body: league_html, status: 200, content_type: 'text/html'
17+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97369', body: league_html, status: 200, content_type: 'text/html'
18+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96828', body: league_html, status: 200, content_type: 'text/html'
19+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97985', body: league_html, status: 200, content_type: 'text/html'
20+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98523', body: league_html, status: 200, content_type: 'text/html'
21+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96863', body: league_html, status: 200, content_type: 'text/html'
22+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97250', body: league_html, status: 200, content_type: 'text/html'
23+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96830', body: league_html, status: 200, content_type: 'text/html'
24+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98336', body: league_html, status: 200, content_type: 'text/html'
25+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98936', body: league_html, status: 200, content_type: 'text/html'
26+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/achievements/', body: achievements_html, status: 200, content_type: 'text/html'
27+
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/matches', body: matches_html, status: 200, content_type: 'text/html'
28+
FakeWeb.register_uri :get, 'http://www.teamliquid.net/forum/viewmessage.php?topic_id=138846', body: status_html, status: 200, content_type: 'text/html'

spec/support/shared/sc2_scraper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
describe '#region_info' do
5151
it 'should return information based on the set region' do
52-
subject.region_info.should == { domain: 'us.battle.net', dir: 'en' }
52+
subject.region_info.should == { domain: 'us.battle.net', dir: 'en', label: 'North America' }
5353
end
5454
end
5555

spec/support/status.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<td class="forumPost" width="100%">Current StarCraft II Battle.Net status (auto-updated every five minutes):<br><br><span style="font-size: large">North America</span><br><span style="color: green">Online</span> (build 21028 - Patch 1.4.3)<br><br><span style="font-size: large">Europe</span><br><span style="color: green">Online</span> (build 21028 - Patch 1.4.3)<br><br><span style="font-size: large">Korea</span><br><span style="color: green">Online</span> (build 21028 - Patch 1.4.3)<br><br><span style="font-size: large">South-East Asia</span><br><span style="color: green">Online</span> (build 21028 - Patch 1.4.3)</td>

0 commit comments

Comments
 (0)