Class: BnetScraper::Starcraft2::ProfileScraper
- Inherits:
-
BaseScraper
- Object
- BaseScraper
- BnetScraper::Starcraft2::ProfileScraper
- 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
-
#achievement_points ⇒ Object
readonly
Returns the value of attribute achievement_points.
-
#career_games ⇒ Object
readonly
Returns the value of attribute career_games.
-
#current_solo_league ⇒ Object
readonly
Returns the value of attribute current_solo_league.
-
#current_team_league ⇒ Object
readonly
Returns the value of attribute current_team_league.
-
#games_this_season ⇒ Object
readonly
Returns the value of attribute games_this_season.
-
#highest_solo_league ⇒ Object
readonly
Returns the value of attribute highest_solo_league.
-
#highest_team_league ⇒ Object
readonly
Returns the value of attribute highest_team_league.
-
#leagues ⇒ Object
readonly
Returns the value of attribute leagues.
-
#portrait ⇒ Object
readonly
Returns the value of attribute portrait.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
-
#protoss_swarm_level ⇒ Object
readonly
Returns the value of attribute protoss_swarm_level.
-
#terran_swarm_level ⇒ Object
readonly
Returns the value of attribute terran_swarm_level.
-
#zerg_swarm_level ⇒ Object
readonly
Returns the value of attribute zerg_swarm_level.
Attributes inherited from BaseScraper
#account, #bnet_id, #bnet_index, #region, #url
Instance Method Summary collapse
-
#extract_portrait_info(html) ⇒ Fixnum
Extracts portrait information (spritesheet page, portsize size, X, Y) from HTML page.
-
#get_campaign_completion(html) ⇒ Object
Extracts completion level of the SC2 Campaigns.
- #get_clan_info(html) ⇒ Object
-
#get_current_league_info(league_type, html) ⇒ String
Extracts the current league achieved for a given league type.
-
#get_highest_league_info(league_type, html) ⇒ String
Extracts the highest league achieved for a given league type.
-
#get_league_list ⇒ Object
scrapes the league list from account’s league page and sets an array of URLs.
-
#get_portrait(html) ⇒ BnetScraper::Starcraft2::Portrait
Extracts background spritesheet and sprite coordinates to map to a multidimensional array of portrait names.
-
#get_profile_data(html) ⇒ Object
Scrapes the Achievement Points, Career Games, and Games this Season from Profile.
-
#get_solo_league_info(html) ⇒ Object
Extracts the current and highest ever solo league achieved.
-
#get_swarm_level(race, html) ⇒ Fixnum
Extracts the swarm level for a given race.
-
#get_swarm_levels(html) ⇒ Object
Extracts the HotS Swarm Levels for each race.
-
#get_team_league_info(html) ⇒ Object
Extracts the current and highest ever team league achieved.
-
#initialize(options = {}) ⇒ ProfileScraper
constructor
A new instance of ProfileScraper.
- #output ⇒ Object
-
#retrieve_data ⇒ Nokogiri::HTML
Retrieves the HTML document and feed into Nokogiri.
- #scrape ⇒ Object
Methods inherited from BaseScraper
#extract_data_from_options, #extract_data_from_url, #profile_url, #region_info, #set_bnet_index, #valid?
Constructor Details
#initialize(options = {}) ⇒ ProfileScraper
Returns a new instance of ProfileScraper.
31 32 33 34 35 |
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 31 def initialize = {} super @leagues = [] @profile ||= Profile.new url: profile_url, account: account end |
Instance Attribute Details
#achievement_points ⇒ Object (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 |
#career_games ⇒ Object (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 |
#current_solo_league ⇒ Object (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 |
#current_team_league ⇒ Object (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 |
#games_this_season ⇒ Object (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 |
#highest_solo_league ⇒ Object (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 |
#highest_team_league ⇒ Object (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 |
#leagues ⇒ Object (readonly)
Returns the value of attribute leagues.
26 27 28 |
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26 def leagues @leagues end |
#portrait ⇒ Object (readonly)
Returns the value of attribute portrait.
26 27 28 |
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26 def portrait @portrait end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
26 27 28 |
# File 'lib/bnet_scraper/starcraft2/profile_scraper.rb', line 26 def profile @profile end |
#protoss_swarm_level ⇒ Object (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 |
#terran_swarm_level ⇒ Object (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 |
#zerg_swarm_level ⇒ Object (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
#extract_portrait_info(html) ⇒ Fixnum
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 |
#get_campaign_completion(html) ⇒ Object
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 |
#get_clan_info(html) ⇒ Object
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 |
#get_current_league_info(league_type, html) ⇒ String
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 |
#get_highest_league_info(league_type, html) ⇒ String
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 |
#get_league_list ⇒ Object
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 |
#get_portrait(html) ⇒ BnetScraper::Starcraft2::Portrait
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 |
#get_profile_data(html) ⇒ Object
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 |
#get_solo_league_info(html) ⇒ Object
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 |
#get_swarm_level(race, html) ⇒ Fixnum
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 |
#get_swarm_levels(html) ⇒ Object
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 |
#get_team_league_info(html) ⇒ Object
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 |
#output ⇒ Object
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 |
#retrieve_data ⇒ Nokogiri::HTML
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 |
#scrape ⇒ Object
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 |