|
1 | 1 | module BnetScraper
|
2 | 2 | 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. |
3 | 13 | class ProfileScraper
|
4 | 14 | attr_reader :bnet_id, :account, :region, :agent, :bnet_index
|
5 | 15 |
|
| 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 |
6 | 24 | def initialize bnet_id, account, region = 'na'
|
7 | 25 | @bnet_id = bnet_id
|
8 | 26 | @account = account
|
9 | 27 | @region = region
|
10 | 28 | set_bnet_index
|
11 | 29 | end
|
12 | 30 |
|
| 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. |
13 | 35 | def set_bnet_index
|
14 | 36 | [1,2].each do |idx|
|
15 | 37 | res = Net::HTTP.get_response URI profile_url idx
|
|
0 commit comments