Class: GooglePlayDevScraper::ScraperBase

Inherits:
Object
  • Object
show all
Defined in:
lib/googleplay_dev_scraper/scraper_base.rb

Direct Known Subclasses

Scraper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScraperBase

Returns a new instance of ScraperBase.



17
18
19
20
# File 'lib/googleplay_dev_scraper/scraper_base.rb', line 17

def initialize
  @agent = nil
  @config = ScraperConfig.new
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



14
15
16
# File 'lib/googleplay_dev_scraper/scraper_base.rb', line 14

def agent
  @agent
end

#configObject

Returns the value of attribute config.



15
16
17
# File 'lib/googleplay_dev_scraper/scraper_base.rb', line 15

def config
  @config
end

Instance Method Details

#setupObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/googleplay_dev_scraper/scraper_base.rb', line 22

def setup
  #Mechanize.log = Logger.new("mechanize.log")
  #Mechanize.log.level = Logger::INFO

  unless @agent
    @agent = Mechanize.new
  end
  if @config.proxy_host && @config.proxy_host.length >= 1
    @agent.set_proxy(@config.proxy_host, @config.proxy_port)
  end
end

#try_get(url) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/googleplay_dev_scraper/scraper_base.rb', line 34

def try_get(url)
  unless @agent
    setup
  end

  # try to get
  @agent.get(url)

  # login needed?
  if @agent.page.uri.host != "accounts.google.com" || @agent.page.uri.path != "/ServiceLogin"
    # already login-ed
    return
  end

  # do login
  form = @agent.page.forms.find {|f| f.form_node['id'] == "gaia_loginform"}
  unless form
    raise 'No login form'
  end
  form.field_with(:name => "Email").value = @config.email
  form.field_with(:name => "Passwd").value = @config.password
  form.click_button

  if @agent.page.uri.host == "accounts.google.com"
    STDERR.puts "login failed? : uri = " + @agent.page.uri.to_s
    raise 'Google login failed'
  end

  # retry get
  @agent.get(url)
end