Skip to content

Commit 7e30ee3

Browse files
committed
Documentation galore!
1 parent d1003ff commit 7e30ee3

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BnetScraper
2+
3+
A Nokogiri-based scraper of Battle.net profiles. Currently this only includes Starcraft2.
4+
5+
# Usage
6+
7+
The gem allows you to parse profiles separate from individual leagues, using `BnetScraper::Starcraft2::ProfileScraper`
8+
or `BnetScraper::Starcraft2::LeagueScraper`. See documentation for more information on those.
9+
10+
One convenience function, `BnetScraper::Starcraft2#full_profile_scrape`, exists to do a full scrape of a given profile.
11+
Note that this is a rather slow function given the number of HTTP calls necessary to retrieve all of the data.

lib/bnet_scraper/starcraft2.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
require 'bnet_scraper/starcraft2/league_scraper'
33

44
module BnetScraper
5+
# This module contains everything about scraping Starcraft 2 Battle.net accounts.
6+
# See `BnetScraper::Starcraft2::ProfileScraper` and `BnetScraper::Starcraft2::LeagueScraper`
7+
# for more details
58
module Starcraft2
69
REGIONS = {
710
'na' => { domain: 'us.battle.net', dir: 'en' },
@@ -11,6 +14,19 @@ module Starcraft2
1114
'fea' => { domain: 'tw.battle.net', dir: 'zh' }
1215
}
1316

17+
# This is a convenience method that chains calls to ProfileScraper,
18+
# followed by a scrape of each league returned in the `leagues` array
19+
# in the profile_data. The end result is a fully scraped profile with
20+
# profile and league data in a hash.
21+
#
22+
# See `BnetScraper::Starcraft2::ProfileScraper` for more information on
23+
# the parameters being sent to `#full_profile_scrape`.
24+
#
25+
# @param bnet_id - Battle.net Account ID
26+
# @param account - Battle.net Account Name
27+
# @param region - Battle.net Account Region
28+
# @return profile_data - Hash containing complete profile and league data
29+
# scraped from the website
1430
def self.full_profile_scrape bnet_id, account, region = 'na'
1531
profile_scraper = ProfileScraper.new bnet_id, account, region
1632
profile_output = profile_scraper.scrape

lib/bnet_scraper/starcraft2/league_scraper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module BnetScraper
22
module Starcraft2
3+
# LeagueScraper
4+
#
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
312
class LeagueScraper
413
attr_reader :url, :bnet_id, :bnet_index, :account, :league_id, :lang,
514
:season, :size, :random, :name, :division

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
module BnetScraper
22
module Starcraft2
3+
# ProfileScraper
4+
#
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.
313
class ProfileScraper
414
attr_reader :bnet_id, :account, :region, :agent, :bnet_index
515

16+
# @param bnet_id - The unique ID for each battle.net account. in the URL scheme, it immediately
17+
# follows the /profile/ directive.
18+
# @param account - The account name. This is a non-unique string that immediately follows the
19+
# bnet_index
20+
# @oaram region - The region of the account. This defaults to 'na' for North America. This
21+
# uses the bnet region codes instead of the full names. The region is used to determine the
22+
# domain to find the profile, as well as teh default language code to use.
23+
# @return profile_data - The hash of profile data scraped, including array of leagues to scrape
624
def initialize bnet_id, account, region = 'na'
725
@bnet_id = bnet_id
826
@account = account
927
@region = region
1028
set_bnet_index
1129
end
1230

31+
# set_bnet_index
32+
#
33+
# Because profile URLs have to have a specific bnet_index that is seemingly incalculable,
34+
# we must ping both variants to determine the correct bnet_index. We then store that value.
1335
def set_bnet_index
1436
[1,2].each do |idx|
1537
res = Net::HTTP.get_response URI profile_url idx

0 commit comments

Comments
 (0)