Skip to content

Commit 3f97b45

Browse files
committed
Adds ProfileScraper, starts LeagueScraper
1 parent 6fce2b4 commit 3f97b45

File tree

9 files changed

+13494
-0
lines changed

9 files changed

+13494
-0
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--color

lib/bnet_scraper/starcraft2.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module BnetScraper
2+
module Starcraft2
3+
REGIONS = {
4+
'na' => { domain: 'us.battle.net', dir: 'en' },
5+
'eu' => { domain: 'eu.battle.net', dir: 'eu' },
6+
'cn' => { domain: 'www.battlenet.com.cn', dir: 'zh' },
7+
'sea' => { domain: 'sea.battle.net', dir: 'en' },
8+
'fea' => { domain: 'tw.battle.net', dir: 'zh' }
9+
}
10+
11+
class ProfileScraper
12+
attr_reader :bnet_id, :account, :region, :agent, :bnet_index
13+
14+
def initialize bnet_id, account, region = 'na'
15+
@bnet_id = bnet_id
16+
@account = account
17+
@region = region
18+
@agent = Mechanize.new
19+
set_bnet_index
20+
end
21+
22+
def set_bnet_index
23+
[1,2].each do |idx|
24+
res = Net::HTTP.get_response URI profile_url idx
25+
if res.is_a? Net::HTTPSuccess
26+
@bnet_index = idx
27+
return
28+
end
29+
end
30+
end
31+
32+
def scrape
33+
@response = @agent.get(profile_url)
34+
35+
@race = @response.search("#season-snapshot .module-footer a").first().inner_html()
36+
@wins = @response.search("#career-stats h2").inner_html()
37+
@achievements = @response.search("#profile-header h3").inner_html()
38+
39+
parse_response
40+
end
41+
42+
def parse_response
43+
{
44+
bnet_id: @bnet_id,
45+
account: @account,
46+
bnet_index: @bnet_index,
47+
race: @race,
48+
wins: @wins,
49+
achievements: @achievements
50+
}
51+
end
52+
53+
def profile_url bnet_index = @bnet_index
54+
"http://#{region_info[:domain]}/sc2/#{region_info[:dir]}/profile/#{bnet_id}/#{bnet_index}/#{account}/"
55+
end
56+
57+
def get_bnet_index
58+
unless @bnet_index
59+
[1,2].each do |idx|
60+
61+
end
62+
end
63+
end
64+
65+
def region_info
66+
REGIONS[region]
67+
end
68+
end
69+
70+
class LeagueScraper
71+
attr_reader :url
72+
73+
def initialize(url)
74+
@url = url
75+
end
76+
end
77+
end
78+
end

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
2+
3+
require 'bnet_scraper'
4+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }
5+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe BnetScraper::Starcraft2::LeagueScraper do
4+
describe '#initialize' do
5+
it 'should take a league URL parameter' do
6+
url = "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/12345"
7+
scraper = BnetScraper::Starcraft2::LeagueScraper.new(url)
8+
scraper.url.should == url
9+
end
10+
end
11+
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
require 'spec_helper'
2+
3+
describe BnetScraper::Starcraft2::ProfileScraper do
4+
subject { BnetScraper::Starcraft2::ProfileScraper.new('2377239', 'Demon') }
5+
6+
describe '#initialize' do
7+
it 'should take bnet_id and account parameters' do
8+
subject.bnet_id.should == '2377239'
9+
subject.account.should == 'Demon'
10+
end
11+
12+
it 'should default the region to na' do
13+
subject.region.should == 'na'
14+
end
15+
16+
it 'should assign region if passed' do
17+
BnetScraper::Starcraft2::ProfileScraper.any_instance.should_receive(:set_bnet_index)
18+
scraper = BnetScraper::Starcraft2::ProfileScraper.new('2377239', 'Demon', 'fea')
19+
scraper.region.should == 'fea'
20+
end
21+
end
22+
23+
describe '#region_info' do
24+
it 'should return information based on the set region' do
25+
subject.region_info.should == { domain: 'us.battle.net', dir: 'en' }
26+
end
27+
end
28+
29+
describe '#set_bnet_index' do
30+
it 'should return the valid integer needed for a proper URL parse from bnet' do
31+
subject.set_bnet_index
32+
subject.bnet_index.should == 1
33+
end
34+
end
35+
36+
describe '#profile_url' do
37+
it 'should return a string URL for bnet' do
38+
subject.profile_url.should == 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/'
39+
end
40+
41+
it 'should optionally take a bnet_index to use instead of saved bnet_index' do
42+
subject.profile_url(2).should == 'http://us.battle.net/sc2/en/profile/2377239/2/Demon/'
43+
end
44+
end
45+
46+
describe '#scrape' do
47+
it 'should set the race, wins, and achievements attributes' do
48+
subject.instance_variable_get(:@race).should be_nil
49+
subject.instance_variable_get(:@achievements).should be_nil
50+
subject.instance_variable_get(:@wins).should be_nil
51+
52+
subject.scrape
53+
54+
subject.instance_variable_get(:@race).should == 'Protoss'
55+
subject.instance_variable_get(:@achievements).should == '3630'
56+
subject.instance_variable_get(:@wins).should == '684'
57+
end
58+
59+
it 'should call parse_response' do
60+
subject.should_receive(:parse_response)
61+
subject.scrape
62+
end
63+
end
64+
65+
describe '#parse_response' do
66+
it 'should extract profile data from the response' do
67+
expected = {
68+
bnet_id: '2377239',
69+
account: 'Demon',
70+
bnet_index: 1,
71+
race: 'Protoss',
72+
wins: '684',
73+
achievements: '3630'
74+
}
75+
76+
subject.parse_response.should == { bnet_id: '2377239', account: 'Demon', bnet_index: 1, race: nil, wins: nil, achievements: nil }
77+
subject.scrape
78+
subject.parse_response.should == expected
79+
end
80+
end
81+
end

0 commit comments

Comments
 (0)