Skip to content

Commit 11dec94

Browse files
committed
Merge pull request agoragames#5 from ggtracker/portrait
added portrait name scraping to ProfileScraper
2 parents f1c96f1 + fe54be6 commit 11dec94

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

lib/bnet_scraper/starcraft2.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,58 @@ module Starcraft2
2727
'tw.battle.net' => 'fea'
2828
}
2929

30+
# The armory uses spritemaps that are sequentially named and have a fixed
31+
# 6x6 grid. We'll simply use the portrait names, left to right, top to
32+
# bottom.
33+
#
34+
# Note: I couldn't identify the exact names of some of these and instead of
35+
# guessing, I didn't name them. Some appear in multiple files too, which
36+
# is odd.
37+
#
38+
# I decided th pad the arrays even if there are no images to make various
39+
# helping functionality (e.g. retrieving position for a name) easier.
40+
# I've also kept them in 6x6 here for better overview.
41+
PORTRAITS = [
42+
# http://eu.battle.net/sc2/static/local-common/images/sc2/portraits/0-75.jpg?v42
43+
['Kachinsky', 'Cade', 'Thatcher', 'Hall', 'Tiger Marine', 'Panda Marine',
44+
'General Warfield', 'Jim Raynor', 'Arcturus Mengsk', 'Sarah Kerrigan', 'Kate Lockwell', 'Rory Swann',
45+
'Egon Stetmann', 'Hill', 'Adjutant', 'Dr. Ariel Hanson', 'Gabriel Tosh', 'Matt Horner',
46+
# Could not identify in order: Raynor in a Suit? Bullmarine? Nova?
47+
# Fiery Marine?
48+
'Tychus Findlay', 'Zeratul', 'Valerian Mengsk', 'Spectre', '?', '?',
49+
'?', '?', 'SCV', 'Firebat', 'Vulture', 'Hellion',
50+
'Medic', 'Spartan Company', 'Wraith', 'Diamondback', 'Probe', 'Scout'],
51+
52+
# http://eu.battle.net/sc2/static/local-common/images/sc2/portraits/1-75.jpg?v42
53+
# Special Rewards - couldn't identify most of these.
54+
['?', '?', '?', '?', '?', 'PanTerran Marine',
55+
'?', '?', '?', '?', '', '',
56+
'', '', '', '', '', '',
57+
'', '', '', '', '', '',
58+
'', '', '', '', '', '',
59+
'', '', '', '', '', ''],
60+
61+
# http://eu.battle.net/sc2/static/local-common/images/sc2/portraits/2-75.jpg?v42
62+
['Ghost', 'Thor', 'Battlecruiser', 'Nova', 'Zealot', 'Stalker',
63+
'Phoenix', 'Immortal', 'Void Ray', 'Colossus', 'Carrier', 'Tassadar',
64+
'Reaper', 'Sentry', 'Overseer', 'Viking', 'High Templar', 'Mutalisk',
65+
# Unidentified: Bird? Dog? Robot?
66+
'Banshee', 'Hybrid Destroyer', 'Dark Voice', '?', '?', '?',
67+
# Unidentified: Worgen? Goblin? Chef?
68+
'Orian', 'Wolf Marine', 'Murloc Marine', '?', '?', 'Zealot Chef',
69+
# Unidentified: KISS Marine? Dragon Marine? Dragon? Another Raynor?
70+
'Stank', 'Ornatus', '?', '?', '?', '?'],
71+
72+
# http://eu.battle.net/sc2/static/local-common/images/sc2/portraits/3-75.jpg?v42
73+
['Urun', 'Nyon', 'Executor', 'Mohandar', 'Selendis', 'Artanis',
74+
'Drone', 'Infested Colonist', 'Infested Marine', 'Corruptor', 'Aberration', 'Broodlord',
75+
'Overmind', 'Leviathan', 'Overlord', 'Hydralisk Marine', "Zer'atai Dark Templar", 'Goliath',
76+
# Unidentified: Satan Marine?
77+
'Lenassa Dark Templar', 'Mira Han', 'Archon', 'Hybrid Reaver', 'Predator', '?',
78+
'Zergling', 'Roach', 'Baneling', 'Hydralisk', 'Queen', 'Infestor',
79+
'Ultralisk', 'Queen of Blades', 'Marine', 'Marauder', 'Medivac', 'Siege Tank']
80+
]
81+
3082
# This is a convenience method that chains calls to ProfileScraper,
3183
# followed by a scrape of each league returned in the `leagues` array
3284
# in the profile_data. The end result is a fully scraped profile with

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Starcraft2
3030
class ProfileScraper < BaseScraper
3131
attr_reader :achievement_points, :career_games, :race, :leagues, :most_played,
3232
:games_this_season, :highest_solo_league, :current_solo_league, :highest_team_league,
33-
:current_team_league
33+
:current_team_league, :portrait
3434

3535
def initialize options = {}
3636
super
@@ -50,6 +50,16 @@ def get_profile_data
5050
if response.success?
5151
html = Nokogiri::HTML(response.body)
5252

53+
# Portraits use spritemaps, so we extract positions and map to
54+
# PORTRAITS.
55+
@portrait = begin
56+
portrait = html.css("#profile-header #portrait span").attr('style').to_s.scan(/url\('(.*?)'\) ([\-\d]+)px ([\-\d]+)px/).flatten
57+
portrait_map, portrait_size = portrait[0].scan(/(\d)\-(\d+)\.jpg/)[0]
58+
portrait_position = (((0-portrait[2].to_i) / portrait_size.to_i) * 6) + ((0-portrait[1].to_i) / portrait_size.to_i + 1)
59+
PORTRAITS[portrait_map.to_i][portrait_position-1]
60+
rescue
61+
nil
62+
end
5363

5464
@race = html.css(".stat-block:nth-child(4) h2").inner_html()
5565
@achievement_points = html.css("#profile-header h3").inner_html()
@@ -118,7 +128,8 @@ def output
118128
games_this_season: @games_this_season,
119129
most_played: @most_played,
120130
achievement_points: @achievement_points,
121-
leagues: @leagues
131+
leagues: @leagues,
132+
portrait: @portrait
122133
}
123134
end
124135
end

spec/starcraft2/profile_scraper_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
current_team_league: 'Not Yet Ranked',
9797
most_played: '4v4',
9898
achievement_points: '3660',
99+
portrait: 'Mohandar',
99100
leagues: [
100101
{
101102
name: "1v1 Platinum Rank 95",
@@ -173,7 +174,8 @@
173174
highest_team_league: nil,
174175
current_team_league: nil,
175176
achievement_points: nil,
176-
leagues: []
177+
leagues: [],
178+
portrait: nil
177179
}
178180

179181
subject.scrape

spec/starcraft2_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
:highest_solo_league => 'Platinum',
1616
:current_team_league => 'Not Yet Ranked',
1717
:highest_team_league => 'Diamond',
18-
:achievement_points=>"3660",
18+
:achievement_points => '3660',
19+
:portrait => 'Mohandar',
1920
:leagues=>[
2021
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},
2122
{:season=>"6", :size=>"4v4", :name=>"Aleksander Pepper", :division=>"Diamond", :random=>false, :bnet_id=>"2377239", :account=>"Demon"},

0 commit comments

Comments
 (0)