Skip to content

Commit 341a9fa

Browse files
committed
Updates documentation of scrapers
[ci skip]
1 parent 494824b commit 341a9fa

File tree

5 files changed

+88
-28
lines changed

5 files changed

+88
-28
lines changed

lib/bnet_scraper/starcraft2/achievement_scraper.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
module BnetScraper
22
module Starcraft2
3+
# This pulls achievement information for an account. Note that currently only returns the overall achievements,
4+
# not the in-depth, by-category achievement information.
5+
#
6+
# scraper = BnetScraper::Starcraft2::AchievementScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/')
7+
# scraper.scrape
8+
# # => {
9+
# recent: [
10+
# { title: 'Blink of an Eye', description: 'Complete round 24 in "Starcraft Master" without losing any stalkers', earned: '3/5/2012' },
11+
# { title: 'Whack-a-Roach', description: 'Complete round 9 in "Starcraft Master" in under 45 seconds', earned: '3/5/2012' },
12+
# { title: 'Safe Zone', description: 'Complete round 8 in "Starcraft Master" without losing any stalkers', earned: '3/5/2012' },
13+
# { title: 'Starcraft Master', description: 'Complete all 30 rounds in "Starcraft Master"', earned: '3/5/2012' },
14+
# { title: 'Starcraft Expert', description: 'Complete any 25 rounds in "Starcraft Master"', earned: '3/5/2012' },
15+
# { title: 'Starcraft Apprentice', description: 'Complete any 20 rounds in "Starcraft Master"', earned: '3/5/2012' }
16+
# ],
17+
# showcase: [
18+
# { title: 'Hot Shot', description: 'Finish a Qualification Round with an undefeated record.' },
19+
# { title: 'Starcraft Master', description: 'Complete all rounds in "Starcraft Master"' },
20+
# { title: 'Team Protoss 500', description: 'Win 500 team league matches as Protoss' },
21+
# { title: 'Night of the Living III', description: 'Survive 15 Infested Horde Attacks in the "Night 2 Die" mode of the "Left 2 Die" scenario.' },
22+
# { title: 'Team Top 100 Diamond', description: 'Finish a Season in Team Diamond Division' }
23+
#
24+
# ],
25+
# progress: {
26+
# liberty_campaign: '1580',
27+
# exploration: '480',
28+
# custom_game: '330',
29+
# cooperative: '660',
30+
# quick_match: '170'
31+
# }
32+
# }
333
class AchievementScraper < BaseScraper
434
attr_reader :recent, :progress, :showcase, :response
535

@@ -11,10 +41,12 @@ def scrape
1141
output
1242
end
1343

44+
# retrieves the account's achievements overview page HTML for scraping
1445
def get_response
1546
@response = Nokogiri::HTML(open(profile_url+"achievements/"))
1647
end
1748

49+
# scrapes the recent achievements from the account's achievements overview page
1850
def scrape_recent
1951
@recent = []
2052
6.times do |num|
@@ -31,6 +63,7 @@ def scrape_recent
3163
@recent
3264
end
3365

66+
# scrapes the progress of each achievement category from the account's achievements overview page
3467
def scrape_progress
3568
progress_ach = response.css("#progress-module .achievements-progress:nth(2) span")
3669
@progress = {
@@ -42,6 +75,7 @@ def scrape_progress
4275
}
4376
end
4477

78+
# scrapes the showcase achievements from the account's achievements overview page
4579
def scrape_showcase
4680
@showcase = response.css("#showcase-module .progress-tile").map do |achievement|
4781
hsh = { title: achievement.css('.tooltip-title').inner_text.strip }

lib/bnet_scraper/starcraft2/base_scraper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
module BnetScraper
22
module Starcraft2
3+
# BaseScraper handles the account information extraction. Each scraper can either be passed a profile URL or
4+
# the minimum information needed to access an account. This means passing in account and bnet_id at minimum.
5+
# Both of the following are valid ways to instantiate a scraper for the same account:
6+
#
7+
# BnetScraper::Starcraft2::BaseScraper.new(url: 'http://us.battle.net/sc2/en/profile/12345/1/TestAccount/')
8+
# BnetScraper::Starcraft2::BaseScraper.new(bnet_id: '12345', account: 'TestAccount')
9+
#
10+
# The URL scheme is the following:
11+
#
12+
# http://<REGION_DOMAIN>/sc2/<REGION_LANG>/profile/<BNET_ID>/<BNET_INDEX>/<ACCOUNT>/
13+
#
14+
# Note that by default, the region will be set to 'na' if you opt not to specify the URL or region. The
15+
# scraper uses the short-codes for regions. See `BnetScraper::Starcraft2::REGIONS` for the address
16+
# translations.
317
class BaseScraper
418
attr_reader :bnet_id, :account, :region, :bnet_index, :url
519

@@ -41,6 +55,8 @@ def profile_url bnet_index = @bnet_index
4155
"http://#{region_info[:domain]}/sc2/#{region_info[:dir]}/profile/#{bnet_id}/#{bnet_index}/#{account}/"
4256
end
4357

58+
# converts region short-code to region-based URL information
59+
# 'na' => { domain: 'us.battle.net', :dir: 'en' }
4460
def region_info
4561
REGIONS[region]
4662
end

lib/bnet_scraper/starcraft2/league_scraper.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
module BnetScraper
22
module Starcraft2
3-
# LeagueScraper
3+
# This pulls information on a specific league for a specific account. It is best used either in conjunction with a
4+
# profile scrape that profiles a URL, or if you happen to know the specific league\_id and can pass it as an option.
45
#
5-
# Extracts information from an SC2 League URL and scrapes for information.
6-
# Example:
7-
# league_data = BnetScraper::Starcraft2::LeagueScraper.new("http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96905")
8-
# league_data # => { bnet_id: '2377239', account: 'Demon', season: '6', size: '4v4', name: "Aleksander Pepper", division: "Diamond", random: false }
9-
#
10-
# @param [String] url - The league URL on battle.net
11-
# @return [Hash] league_data - Hash of data extracted
6+
# scraper = BnetScraper::Starcraft2::LeagueScraper.new(league_id: '12345', account: 'Demon', bnet_id: '2377239')
7+
# scraper.scrape
8+
# # => {
9+
# season: '6',
10+
# name: 'Aleksander Pepper',
11+
# division: 'Diamond',
12+
# size: '4v4',
13+
# random: false,
14+
# bnet_id: '2377239',
15+
# account: 'Demon'
16+
# }
1217
class LeagueScraper < BaseScraper
1318
attr_reader :league_id, :season, :size, :random, :name, :division
1419

20+
# @param [String] url - The league URL on battle.net
21+
# @return [Hash] league_data - Hash of data extracted
1522
def initialize options = {}
1623
super(options)
1724

lib/bnet_scraper/starcraft2/match_history_scraper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module BnetScraper
22
module Starcraft2
3-
## BnetScraper::Starcraft2::MatchHistoryScraper
4-
#
53
# This pulls the 25 most recent matches played for an account. Note that this is only as up-to-date as battle.net is, and
64
# will likely not be as fast as in-game.
75
#
@@ -20,10 +18,12 @@ module Starcraft2
2018
class MatchHistoryScraper < BaseScraper
2119
attr_reader :matches, :wins, :losses, :response
2220

21+
# account's match history URL
2322
def match_url
2423
profile_url + "matches"
2524
end
2625

26+
# retrieves the match history HTML for scraping
2727
def get_response
2828
@response = Nokogiri::HTML(open(match_url))
2929
end

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
module BnetScraper
22
module Starcraft2
3-
# ProfileScraper
3+
# This pulls basic profile information for an account, as well as an array of league URLs. This is a good starting
4+
# point for league scraping as it provides the league URLs necessary to do supplemental scraping.
45
#
5-
# Scrapes SC2 profile data from battle.net and returns it as a hash. Example:
6-
# profile_data = BnetScraper::Starcraft2::ProfileScraper.new('2377239', 'Demon')
7-
# profile_data # => { bnet_id: '2377239', account: 'Demon', race: 'Protoss',
8-
# wins: '684', achievements: '3260', leagues: [], bnet_index: 1 }
9-
#
10-
# One thing of note is the bnet_index. In Battle.net URLs, there is a single-digit index
11-
# used on accounts (1 or 2). This index is seemingly arbitrary, but critical to properly
12-
# accessing the data.
13-
#
14-
# ProfileScraper requires that either you pass the URL of the profile, or the bnet_id and
15-
# account name of the profile. The URL scheme is as such:
16-
#
17-
# http://<REGION_DOMAIN>/sc2/<REGION_LANG>/profile/<BNET_ID>/<BNET_INDEX>/<ACCOUNT>/
18-
#
19-
# Using this URL we can extract the critical information. However, sometimes we do not have
20-
# the URL and have to make do with a bnet_id and account. This is the bare minimum needed,
21-
# unless the account is in a region other than 'na'. In such cases, region all needs to be passed.
6+
# scraper = BnetScraper::Starcraft2::ProfileScraper.new(url: 'http://us.battle.net/sc2/en/profile/2377239/1/Demon/')
7+
# scraper.scrape
8+
# # => {
9+
# bnet_id: '2377239',
10+
# account: 'Demon',
11+
# bnet_index: 1,
12+
# race: 'Protoss',
13+
# wins: '684',
14+
# achievement_points: '3630',
15+
# leagues: [
16+
# {
17+
# name: "1v1 Platinum Rank 95",
18+
# id: "96905",
19+
# href: "http://us.battle.net/sc2/en/profile/2377239/1/Demon/ladder/96905#current-rank"
20+
# }
21+
# ]
22+
# }
2223
class ProfileScraper < BaseScraper
2324
attr_reader :achievement_points, :wins, :race, :leagues
2425
def scrape
@@ -27,6 +28,7 @@ def scrape
2728
output
2829
end
2930

31+
# scrapes the profile page for basic account information
3032
def get_profile_data
3133
response = Nokogiri::HTML(open(profile_url))
3234

@@ -35,6 +37,7 @@ def get_profile_data
3537
@achievement_points = response.css("#profile-header h3").inner_html()
3638
end
3739

40+
# scrapes the league list from account's league page and sets an array of URLs
3841
def get_league_list
3942
response = Nokogiri::HTML(open(profile_url + "ladder/leagues"))
4043

0 commit comments

Comments
 (0)