Skip to content

Commit 5229952

Browse files
committed
Adds BnetScraper::Starcraft2#full_profile_scrape
1 parent b64c1a4 commit 5229952

File tree

5 files changed

+71
-15
lines changed

5 files changed

+71
-15
lines changed

lib/bnet_scraper/starcraft2.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,18 @@ module Starcraft2
1010
'sea' => { domain: 'sea.battle.net', dir: 'en' },
1111
'fea' => { domain: 'tw.battle.net', dir: 'zh' }
1212
}
13+
14+
def self.full_profile_scrape bnet_id, account, region = 'na'
15+
profile_scraper = ProfileScraper.new bnet_id, account, region
16+
profile_output = profile_scraper.scrape
17+
18+
parsed_leagues = []
19+
profile_output[:leagues].each do |league|
20+
league_scraper = LeagueScraper.new league[:href]
21+
parsed_leagues << league_scraper.scrape
22+
end
23+
profile_output[:leagues] = parsed_leagues
24+
return profile_output
25+
end
1326
end
1427
end

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ def get_profile_data
3636
end
3737

3838
def get_league_list
39-
url = "#{profile_url}ladder/leagues"
4039
response = @agent.get(profile_url + "ladder/leagues")
4140

4241
@leagues = response.search("a[href*='#current-rank']").map do |league|
4342
{
4443
name: league.inner_html().strip,
4544
id: league.attr('href').sub('#current-rank',''),
46-
href: url + league.attr('href')
45+
href: "#{profile_url}ladder/#{league.attr('href')}"
4746
}
4847
end
4948
end

spec/starcraft2/profile_scraper_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,62 +96,62 @@
9696
{
9797
name: "1v1 Platinum Rank 95",
9898
id: "96905",
99-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues96905#current-rank"
99+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96905#current-rank"
100100
},
101101
{
102102
name: "2v2 Random Platinum ...",
103103
id: "96716",
104-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues96716#current-rank"
104+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96716#current-rank"
105105
},
106106
{
107107
name: "2v2 Diamond Rank 45",
108108
id: "98162",
109-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues98162#current-rank"
109+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98162#current-rank"
110110
},
111111
{
112112
name: "2v2 Silver Rank 8",
113113
id: "97369",
114-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues97369#current-rank"
114+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97369#current-rank"
115115
},
116116
{
117117
name: "3v3 Random Gold Rank...",
118118
id: "96828",
119-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues96828#current-rank"
119+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96828#current-rank"
120120
},
121121
{
122122
name: "3v3 Diamond Rank 56",
123123
id: "97985",
124-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues97985#current-rank"
124+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97985#current-rank"
125125
},
126126
{
127127
name: "3v3 Silver Rank 5",
128128
id: "98523",
129-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues98523#current-rank"
129+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98523#current-rank"
130130
},
131131
{
132132
name: "3v3 Platinum Rank 88",
133133
id: "96863",
134-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues96863#current-rank"
134+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96863#current-rank"
135135
},
136136
{
137137
name: "3v3 Gold Rank 75",
138138
id: "97250",
139-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues97250#current-rank"
139+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/97250#current-rank"
140140
},
141141
{
142142
name: "4v4 Random Platinum ...",
143143
id: "96830",
144-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues96830#current-rank"
144+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96830#current-rank"
145145
},
146146
{
147147
name: "4v4 Gold Rank 38",
148148
id: "98336",
149-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues98336#current-rank"
149+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98336#current-rank"
150150
},
151151
{
152152
name: "4v4 Diamond Rank 54",
153153
id: "98936",
154-
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/leagues98936#current-rank"
154+
href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/98936#current-rank"
155155
}
156156
]
157157
}

spec/starcraft2_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
describe BnetScraper::Starcraft2 do
4+
describe '#full_profile_scrape' do
5+
it 'should return the fully scraped profile with league data' do
6+
expected = {
7+
:bnet_id=>"2377239",
8+
:account=>"Demon",
9+
:bnet_index=>1,
10+
:race=>"Protoss",
11+
:wins=>"684",
12+
:achievements=>"3630",
13+
:leagues=>[
14+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
15+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
16+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
17+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
18+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
19+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
20+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
21+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
22+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
23+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
24+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
25+
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"}
26+
]
27+
}
28+
actual = BnetScraper::Starcraft2.full_profile_scrape('2377239', 'Demon')
29+
actual.should == expected
30+
end
31+
end
32+
end

spec/support/load_fakeweb.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@
66

77
FakeWeb.allow_net_connect = false
88
FakeWeb.register_uri :get, 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/', body: profile_html, status: 200, content_type: 'text/html'
9-
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'
109
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'
10+
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'
11+
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'
12+
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'
13+
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'
14+
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'
15+
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'
16+
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'
17+
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'
18+
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'
19+
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'
20+
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'
21+
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'
22+
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'

0 commit comments

Comments
 (0)