Skip to content

Loading…

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

Merged
merged 1 commit into from

2 participants

@logankoester

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"}
]
@logankoester logankoester commented on the diff
spec/support/load_fakeweb.rb
((9 lines not shown))
-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'

Sorry about this diff, just realignment. The only change is the addtion of line 28.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@cadwallion

Looks good.

@cadwallion cadwallion merged commit 703690e into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
11 lib/bnet_scraper/starcraft2.rb
@@ -3,6 +3,7 @@
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.
@@ -10,11 +11,11 @@ module BnetScraper
# 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,
View
2 lib/bnet_scraper/starcraft2/base_scraper.rb
@@ -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]
View
39 lib/bnet_scraper/starcraft2/status_scraper.rb
@@ -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
View
16 spec/starcraft2/status_scraper_spec.rb
@@ -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
View
32 spec/support/load_fakeweb.rb
@@ -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'

Sorry about this diff, just realignment. The only change is the addtion of line 28.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+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'
View
2 spec/support/shared/sc2_scraper.rb
@@ -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
1 spec/support/status.html
@@ -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>
Something went wrong with that request. Please try again.