Class: BnetScraper::Starcraft2::ProfileScraper

Inherits:
BaseScraper
  • Object
show all
Defined in:
lib/bnet_scraper/starcraft2/profile_scraper.rb

Overview

This pulls basic profile information for an account, as well as an array of league URLs. This is a good starting

point for league scraping as it provides the league URLs necessary to do supplemental scraping.

  ``` ruby
  scraper = BnetScraper::Starcraft2::ProfileScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/')
  profile = scraper.scrape
  profile.class.name # => BnetScraper::Starcraft2::Profile
  ```

Additionally, the resulting `BnetScraper::Starcraft2::Profile` object has methods to scrape additional
information without the need of creating another scraper.  For example, if you need to pull league information up
on a player, you may call `BnetScraper::Starcraft2::Profile#leagues` and it will scrape and store the information
for memoized access.

``` ruby
scraper = BnetScraper::Starcraft2::ProfileScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/')
profile = scraper.scrape
profile.leagues.map(&:division) #=> ['Bronze']
```

Instance Attribute Summary (collapse)

Attributes inherited from BaseScraper

#account, #bnet_id, #bnet_index, #region, #url

Instance Method Summary (collapse)

Methods inherited from BaseScraper

#extract_data_from_options, #extract_data_from_url, #profile_url, #region_info, #set_bnet_index, #valid?

Constructor Details

- (ProfileScraper) initialize(options = {})



31
32
33
34
35
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 31

def initialize options = {}
  super
  @leagues = []
  @profile ||= Profile.new url: profile_url, account: 
end

Instance Attribute Details

- (Object) achievement_points (readonly)

Returns the value of attribute achievement_points



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def achievement_points
  @achievement_points
end

- (Object) career_games (readonly)

Returns the value of attribute career_games



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def career_games
  @career_games
end

- (Object) current_solo_league (readonly)

Returns the value of attribute current_solo_league



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def current_solo_league
  @current_solo_league
end

- (Object) current_team_league (readonly)

Returns the value of attribute current_team_league



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def current_team_league
  @current_team_league
end

- (Object) games_this_season (readonly)

Returns the value of attribute games_this_season



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def games_this_season
  @games_this_season
end

- (Object) highest_solo_league (readonly)

Returns the value of attribute highest_solo_league



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def highest_solo_league
  @highest_solo_league
end

- (Object) highest_team_league (readonly)

Returns the value of attribute highest_team_league



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def highest_team_league
  @highest_team_league
end

- (Object) leagues (readonly)

Returns the value of attribute leagues



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def leagues
  @leagues
end

- (Object) portrait (readonly)

Returns the value of attribute portrait



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def portrait
  @portrait
end

- (Object) profile (readonly)

Returns the value of attribute profile



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def profile
  @profile
end

- (Object) protoss_swarm_level (readonly)

Returns the value of attribute protoss_swarm_level



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def protoss_swarm_level
  @protoss_swarm_level
end

- (Object) terran_swarm_level (readonly)

Returns the value of attribute terran_swarm_level



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def terran_swarm_level
  @terran_swarm_level
end

- (Object) zerg_swarm_level (readonly)

Returns the value of attribute zerg_swarm_level



26
27
28
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26

def zerg_swarm_level
  @zerg_swarm_level
end

Instance Method Details

- (Fixnum) extract_portrait_info(html)

Extracts portrait information (spritesheet page, portsize size, X, Y) from HTML page



88
89
90
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 88

def extract_portrait_info html
  html.css("#portrait .icon-frame").attr('style').to_s.scan(/url\('.+(\d+)-(\d+)\.jpg'\) -{0,1}(\d+)px -{0,1}(\d+)px/).flatten.map(&:to_i)
end

- (Object) get_campaign_completion(html)

Extracts completion level of the SC2 Campaigns



156
157
158
159
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 156

def get_campaign_completion html
  @profile.terran_campaign_completion = html.css('.campaign-wings-of-liberty .badge')[0].attr('class').split[1].to_sym
  @profile.zerg_campaign_completion = html.css('.campaign-heart-of-the-swarm .badge')[0].attr('class').split[1].to_sym
end

- (Object) get_clan_info(html)



179
180
181
182
183
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 179

def get_clan_info html
  @profile.clan_tag = html.css(".clan-tag").inner_html
  @profile.clan_name = html.css(".clan-tagname").inner_html
  
end

- (String) get_current_league_info(league_type, html)

Extracts the current league achieved for a given league type



126
127
128
129
130
131
132
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 126

def get_current_league_info league_type, html
  if div = html.css("#best-finish-#{league_type.upcase} div")[0].children[8]
    div.inner_text.strip
  else
    "Not Yet Ranked"
  end
end

- (String) get_highest_league_info(league_type, html)

Extracts the highest league achieved for a given league type



113
114
115
116
117
118
119
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 113

def get_highest_league_info league_type, html
  if div = html.css("#best-finish-#{league_type.upcase} div")[0]
    div.children[2].inner_text.strip
  else
    "Not Yet Ranked"
  end
end

- (Object) get_league_list

scrapes the league list from account's league page and sets an array of URLs



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 162

def get_league_list
  response = Faraday.get profile_url + "ladder/leagues"
  if response.success?
    html = Nokogiri::HTML(response.body)

    @profile.leagues = html.css("a[href*='#current-rank']").map do |league|
      League.new({
        name: league.inner_text().strip,
        id: league.attr('href').sub('#current-rank',''),
        href: "#{profile_url}ladder/#{league.attr('href')}"
      })
    end
  else
    raise BnetScraper::InvalidProfileError
  end
end

- (BnetScraper::Starcraft2::Portrait) get_portrait(html)

Extracts background spritesheet and sprite coordinates to map to a multidimensional array of portrait names. The first index is the spritesheet page, the second index is the position within the spritesheet



80
81
82
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 80

def get_portrait html
  @profile.portrait = Portrait.new(*extract_portrait_info(html))
end

- (Object) get_profile_data(html)

Scrapes the Achievement Points, Career Games, and Games this Season from Profile



68
69
70
71
72
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 68

def get_profile_data html
  @profile.achievement_points = html.css("#profile-header h3").inner_html()
  @profile.career_games = html.css(".career-stat-block:nth-child(4) .stat-value").inner_html()
  @profile.games_this_season = html.css(".career-stat-block:nth-child(5) .stat-value").inner_html()
end

- (Object) get_solo_league_info(html)

Extracts the current and highest ever solo league achieved



95
96
97
98
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 95

def get_solo_league_info html
  @profile.highest_solo_league = get_highest_league_info :solo, html
  @profile.current_solo_league = get_current_league_info :solo, html
end

- (Fixnum) get_swarm_level(race, html)

Extracts the swarm level for a given race



148
149
150
151
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 148

def get_swarm_level race, html
  level = html.css(".race-level-block.#{race} .level-value").inner_html
  level.match(/Level (\d+)/)[1].to_i
end

- (Object) get_swarm_levels(html)

Extracts the HotS Swarm Levels for each race



137
138
139
140
141
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 137

def get_swarm_levels html
  @profile.protoss_swarm_level = get_swarm_level :protoss, html
  @profile.terran_swarm_level = get_swarm_level :terran, html
  @profile.zerg_swarm_level = get_swarm_level :zerg, html
end

- (Object) get_team_league_info(html)

Extracts the current and highest ever team league achieved



103
104
105
106
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 103

def get_team_league_info html
  @profile.highest_team_league = get_highest_league_info :team, html
  @profile.current_team_league = get_current_league_info :team, html
end

- (Object) output



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 185

def output
   {
    bnet_id: @bnet_id,
    account: @account,
    bnet_index: @bnet_index,
    race: @race,
    current_solo_league: @current_solo_league,
    highest_solo_league: @highest_solo_league,
    current_team_league: @current_team_league,
    highest_team_league: @highest_team_league,
    career_games: @career_games,
    games_this_season: @games_this_season,
    most_played: @most_played,
    achievement_points: @achievement_points,
    leagues: @leagues,
    portrait: @portrait
  }  
end

- (Nokogiri::HTML) retrieve_data

Retrieves the HTML document and feed into Nokogiri



55
56
57
58
59
60
61
62
63
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 55

def retrieve_data
  response = Faraday.get profile_url

  if response.success?
    Nokogiri::HTML(response.body)
  else
    raise BnetScraper::InvalidProfileError
  end
end

- (Object) scrape



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 37

def scrape
  html = retrieve_data

  get_profile_data html
  get_portrait html
  get_solo_league_info html
  get_team_league_info html
  get_swarm_levels html
  get_campaign_completion html
  get_league_list
  get_clan_info html

  @profile
end