Skip to content

Commit a4e94b9

Browse files
committed
Replaces Mechanize with Nokogiri
1 parent 5229952 commit a4e94b9

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

bnet_scraper.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
1414
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
1515
s.require_paths = ["lib"]
1616

17-
s.add_runtime_dependency 'mechanize'
17+
s.add_runtime_dependency 'nokogiri'
1818
s.add_development_dependency 'rspec'
1919
s.add_development_dependency 'fakeweb'
2020
end

lib/bnet_scraper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'bnet_scraper/starcraft2'
2-
require 'mechanize'
32
require 'net/http'
3+
require 'open-uri'
4+
require 'nokogiri'
45

56
module BnetScraper
67
# Your code goes here...

lib/bnet_scraper/starcraft2/league_scraper.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ class LeagueScraper
66

77
def initialize(url)
88
@url, @lang, @bnet_id, @bnet_index, @account, @league_id = url.match(/http:\/\/.+\/sc2\/(.+)\/profile\/(.+)\/(\d{1})\/(.+)\/ladder\/(.+)(#current-rank)?/).to_a
9-
@agent = Mechanize.new
109
end
1110

1211
def scrape
13-
@response = @agent.get(@url)
14-
value = @response.search(".data-title .data-label h3").inner_text().strip
12+
@response = Nokogiri::HTML(open(@url))
13+
value = @response.css(".data-title .data-label h3").inner_text().strip
1514
header_regex = /Season (\d{1}) - \s+(\dv\d)( Random)? (\w+)\s+Division (.+)/
1615
header_values = value.match(header_regex).to_a
1716
header_values.shift()

lib/bnet_scraper/starcraft2/profile_scraper.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ def initialize bnet_id, account, region = 'na'
77
@bnet_id = bnet_id
88
@account = account
99
@region = region
10-
@agent = Mechanize.new
1110
set_bnet_index
1211
end
1312

@@ -28,17 +27,17 @@ def scrape
2827
end
2928

3029
def get_profile_data
31-
response = @agent.get(profile_url)
30+
response = Nokogiri::HTML(open(profile_url))
3231

33-
@race = response.search("#season-snapshot .module-footer a").first().inner_html()
34-
@wins = response.search("#career-stats h2").inner_html()
35-
@achievements = response.search("#profile-header h3").inner_html()
32+
@race = response.css("#season-snapshot .module-footer a").first().inner_html()
33+
@wins = response.css("#career-stats h2").inner_html()
34+
@achievements = response.css("#profile-header h3").inner_html()
3635
end
3736

3837
def get_league_list
39-
response = @agent.get(profile_url + "ladder/leagues")
38+
response = Nokogiri::HTML(open(profile_url + "ladder/leagues"))
4039

41-
@leagues = response.search("a[href*='#current-rank']").map do |league|
40+
@leagues = response.css("a[href*='#current-rank']").map do |league|
4241
{
4342
name: league.inner_html().strip,
4443
id: league.attr('href').sub('#current-rank',''),

0 commit comments

Comments
 (0)