Permalink
Browse files

Ability to check if Battle.net is online for a region

  • Loading branch information...
1 parent ddfca1c commit 98066a859787f7b8934f687de3bd0d424c750b00 @logankoester logankoester committed Apr 13, 2012
@@ -3,18 +3,19 @@
require 'bnet_scraper/starcraft2/league_scraper'
require 'bnet_scraper/starcraft2/achievement_scraper'
require 'bnet_scraper/starcraft2/match_history_scraper'
+require 'bnet_scraper/starcraft2/status_scraper'
module BnetScraper
# This module contains everything about scraping Starcraft 2 Battle.net accounts.
# See `BnetScraper::Starcraft2::ProfileScraper` and `BnetScraper::Starcraft2::LeagueScraper`
# for more details
module Starcraft2
REGIONS = {
- 'na' => { domain: 'us.battle.net', dir: 'en' },
- 'eu' => { domain: 'eu.battle.net', dir: 'eu' },
- 'cn' => { domain: 'www.battlenet.com.cn', dir: 'zh' },
- 'sea' => { domain: 'sea.battle.net', dir: 'en' },
- 'fea' => { domain: 'tw.battle.net', dir: 'zh' }
+ 'na' => { domain: 'us.battle.net', dir: 'en', label: 'North America' },
+ 'eu' => { domain: 'eu.battle.net', dir: 'eu', label: 'Europe' },
+ 'cn' => { domain: 'www.battlenet.com.cn', dir: 'zh', label: 'China' },
+ 'sea' => { domain: 'sea.battle.net', dir: 'en', label: 'South-East Asia' },
+ 'fea' => { domain: 'tw.battle.net', dir: 'zh', label: 'Korea' }
}
# This is a convenience method that chains calls to ProfileScraper,
@@ -20,7 +20,7 @@ class BaseScraper
def initialize options = {}
if options[:url]
extracted_data = options[:url].match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.[^\/]+)\//)
- @region = REGIONS.key({ domain: extracted_data[1], dir: extracted_data[2] })
+ @region = REGIONS.select { |k,v| v[:domain] == extracted_data[1] && v[:dir] == extracted_data[2] }.first.first
@bnet_id = extracted_data[3]
@bnet_index = extracted_data[4]
@account = extracted_data[5]
@@ -0,0 +1,39 @@
+module BnetScraper
+ module Starcraft2
+ # Ask the TL.net bot if Battle.Net is currently online for a given region.
+ # This page is updated every 5 minutes. Call #fetch to refresh.
+ #
+ # Examples:
+ # BnetScraper::Starcraft2::Status.na => 'Online'
+ # BnetScraper::Starcraft2::Status.fea => 'Offline'
+ # BnetScraper::Starcraft2::Status.cn => nil (China is unsupported)
+ # BnetScraper::Starcraft2::Status.fetch => [
+ # {:region=>"North America", :status=>"Online"},{:region=>"Europe", :status=>"Online"},
+ # {:region=>"Korea", :status=>"Online"}, {:region=>"South-East Asia", :status=>"Online"}
+ # ]
+ class Status
+ SOURCE = 'http://www.teamliquid.net/forum/viewmessage.php?topic_id=138846'
+
+ class << self
+
+ def fetch
+ Nokogiri::HTML(open(SOURCE))
+ .css('.forumPost').first.css('span').to_a
+ .each_slice(2).map { |i| { :region => i.first.text, :status => i.last.text } }
+ end
+
+ def method_missing sym
+ status? sym if REGIONS.reject { |r| r == 'cn' }.include?(sym.to_s)
+ end
+
+ private
+ def status? region
+ @status ||= fetch
+ @status.select do |r|
+ r[:region] == REGIONS.select { |k,v| k == region.to_s }.first.last[:label]
+ end.first[:status]
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe BnetScraper::Starcraft2::Status do
+ describe 'Each supported region' do
+ it 'should be online' do
+ BnetScraper::Starcraft2::Status.na.should == 'Online'
+ BnetScraper::Starcraft2::Status.eu.should == 'Online'
+ BnetScraper::Starcraft2::Status.sea.should == 'Online'
+ BnetScraper::Starcraft2::Status.fea.should == 'Online'
+ end
+ end
+
+ describe 'China' do
+ BnetScraper::Starcraft2::Status.cn.should == nil
+ end
+end
@@ -5,22 +5,24 @@
league_html = File.read File.dirname(__FILE__) + '/league.html'
achievements_html = File.read File.dirname(__FILE__) + '/achievements.html'
matches_html = File.read File.dirname(__FILE__) + '/matches.html'
+status_html = File.read File.dirname(__FILE__) + '/status.html'
FakeWeb.allow_net_connect = false
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/', body: profile_html, status: 200, content_type: 'text/html'
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'
-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'
-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'
-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'
-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'
-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'
-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'
-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'
-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'
-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'
-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'
-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'
-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'
-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'
-FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/achievements/', body: achievements_html, status: 200, content_type: 'text/html'
-FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/matches', body: matches_html, status: 200, content_type: 'text/html'
+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'
+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'
+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'
+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'
+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'
+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'
+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'
+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'
+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'
+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'
+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'
+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'
+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'
+FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/achievements/', body: achievements_html, status: 200, content_type: 'text/html'
+FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/matches', body: matches_html, status: 200, content_type: 'text/html'
+FakeWeb.register_uri :get, 'http://www.teamliquid.net/forum/viewmessage.php?topic_id=138846', body: status_html, status: 200, content_type: 'text/html'
@@ -49,7 +49,7 @@
describe '#region_info' do
it 'should return information based on the set region' do
- subject.region_info.should == { domain: 'us.battle.net', dir: 'en' }
+ subject.region_info.should == { domain: 'us.battle.net', dir: 'en', label: 'North America' }
end
end
View
@@ -0,0 +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 comments on commit 98066a8

Please sign in to comment.