Skip to content

Commit 6f0aa69

Browse files
committed
Adds AchievementScraper
1 parent 0a89b44 commit 6f0aa69

File tree

5 files changed

+1329
-3
lines changed

5 files changed

+1329
-3
lines changed

lib/bnet_scraper/starcraft2.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'bnet_scraper/starcraft2/profile_scraper'
22
require 'bnet_scraper/starcraft2/league_scraper'
3+
require 'bnet_scraper/starcraft2/achievement_scraper'
34

45
module BnetScraper
56
# This module contains everything about scraping Starcraft 2 Battle.net accounts.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module BnetScraper
2+
module Starcraft2
3+
class AchievementScraper
4+
attr_reader :region, :bnet_id, :account, :bnet_index, :recent, :progress, :showcase
5+
def initialize options = {}
6+
if options[:url]
7+
extracted_data = options[:url].match(/http:\/\/(.+)\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.+)\/achievements\//)
8+
@region = REGIONS.key({ domain: extracted_data[1], dir: extracted_data[2] })
9+
@bnet_id = extracted_data[3]
10+
@bnet_index = extracted_data[4]
11+
@account = extracted_data[5]
12+
elsif options[:bnet_id] && options[:account]
13+
@account = options[:account]
14+
@bnet_id = options[:bnet_id]
15+
@region = options[:region] || 'na'
16+
17+
if options[:bnet_index]
18+
@bnet_index = options[:bnet_index]
19+
else
20+
set_bnet_index
21+
end
22+
end
23+
end
24+
25+
# set_bnet_index
26+
#
27+
# Because profile URLs have to have a specific bnet_index that is seemingly incalculable,
28+
# we must ping both variants to determine the correct bnet_index. We then store that value.
29+
def set_bnet_index
30+
[1,2].each do |idx|
31+
res = Net::HTTP.get_response URI achievement_url idx
32+
if res.is_a? Net::HTTPSuccess
33+
@bnet_index = idx
34+
return
35+
end
36+
end
37+
end
38+
39+
def achievement_url bnet_index = @bnet_index
40+
"http://#{region_info[:domain]}/sc2/#{region_info[:dir]}/profile/#{bnet_id}/#{bnet_index}/#{account}/achievements/"
41+
end
42+
43+
def region_info
44+
REGIONS[region]
45+
end
46+
47+
def scrape
48+
response = Nokogiri::HTML(open(achievement_url))
49+
@showcase = response.css("#showcase-module .progress-tile").map do |achievement|
50+
hsh = { title: achievement.css('.tooltip-title').inner_text.strip }
51+
hsh[:description] = achievement.css('div').inner_text.gsub(hsh[:title], '').strip
52+
hsh
53+
end
54+
55+
@recent = []
56+
6.times do |num|
57+
achievement = {}
58+
div = response.css("#achv-recent-#{num}")
59+
if div
60+
achievement[:title] = div.css("div").inner_text.strip
61+
achievement[:description] = div.inner_text.gsub(achievement[:title], '').strip
62+
achievement[:earned] = response.css("#recent-achievements a:nth(#{num}) span:nth(1)").inner_text
63+
64+
@recent << achievement
65+
end
66+
end
67+
68+
progress_ach = response.css("#progress-module .achievements-progress:nth(2) span")
69+
@progress = {
70+
liberty_campaign: progress_ach[0].inner_text,
71+
exploration: progress_ach[1].inner_text,
72+
custom_game: progress_ach[2].inner_text,
73+
cooperative: progress_ach[3].inner_text,
74+
quick_match: progress_ach[4].inner_text,
75+
}
76+
end
77+
end
78+
end
79+
end
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require 'spec_helper'
2+
3+
describe BnetScraper::Starcraft2::AchievementScraper do
4+
describe '#initialize' do
5+
context 'with url parameter passed' do
6+
subject { BnetScraper::Starcraft2::AchievementScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/achievements/') }
7+
8+
it 'should extract bnet_id from the URL' do
9+
subject.bnet_id.should == '2377239'
10+
end
11+
12+
it 'should extract account from the URL' do
13+
subject.account.should == 'Demon'
14+
end
15+
16+
it 'should extract the bnet_index from the URL' do
17+
subject.bnet_index.should == '1'
18+
end
19+
20+
it 'should extract the region from the URL' do
21+
subject.region.should == 'na'
22+
end
23+
end
24+
25+
context 'when bnet_id and account parameters are passed' do
26+
subject { BnetScraper::Starcraft2::AchievementScraper.new(bnet_id: '2377239', account: 'Demon') }
27+
it 'should set the bnet_id and account parameters' do
28+
subject.bnet_id.should == '2377239'
29+
subject.account.should == 'Demon'
30+
end
31+
32+
it 'should default the region to na' do
33+
subject.region.should == 'na'
34+
end
35+
36+
it 'should assign region if passed' do
37+
BnetScraper::Starcraft2::AchievementScraper.any_instance.should_receive(:set_bnet_index)
38+
scraper = BnetScraper::Starcraft2::AchievementScraper.new(bnet_id: '2377239', account: 'Demon', region: 'fea')
39+
scraper.region.should == 'fea'
40+
end
41+
42+
it 'should not call set_bnet_index if bnet_index is passed' do
43+
BnetScraper::Starcraft2::AchievementScraper.any_instance.should_not_receive(:set_bnet_index)
44+
scraper = BnetScraper::Starcraft2::AchievementScraper.new(bnet_id: '2377239', account: 'Demon', region: 'fea', bnet_index: '1')
45+
end
46+
47+
it 'should call set_bnet_index_if bnet_index is not passed' do
48+
BnetScraper::Starcraft2::AchievementScraper.any_instance.should_receive(:set_bnet_index)
49+
scraper = BnetScraper::Starcraft2::AchievementScraper.new(bnet_id: '2377239', account: 'Demon', region: 'fea')
50+
end
51+
end
52+
end
53+
54+
describe '#scrape' do
55+
subject { BnetScraper::Starcraft2::AchievementScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/achievements/') }
56+
before :each do
57+
subject.scrape
58+
end
59+
60+
it 'should set the showcase' do
61+
subject.showcase.should have(5).achievements
62+
end
63+
64+
it 'should set the recently earned' do
65+
subject.recent.should have(6).achievements
66+
end
67+
68+
it 'should set the liberty campaign progress' do
69+
subject.progress[:liberty_campaign].should == '1580'
70+
end
71+
72+
it 'should set the exploration progress' do
73+
subject.progress[:exploration].should == '480'
74+
end
75+
76+
it 'should set the custom game progress' do
77+
subject.progress[:custom_game].should == '330'
78+
end
79+
80+
it 'should set the cooperative progress' do
81+
subject.progress[:cooperative].should == '660'
82+
end
83+
84+
it 'should set the quick match progress' do
85+
subject.progress[:quick_match].should == '170'
86+
end
87+
end
88+
end

0 commit comments

Comments
 (0)