Class: Graybook::Importer::Aol

Inherits:
PageScraper show all
Defined in:
lib/graybook/importer/aol.rb

Overview

Imports contacts from AOL

Instance Attribute Summary

Attributes inherited from PageScraper

#agent

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from PageScraper

#create_agent, #fetch_contacts!, #strip_html

Methods inherited from Base

#fetch_contacts!, #import, #service_name

Instance Method Details

#=~(options) ⇒ Object

Matches this importer to an user’s name/address



11
12
13
# File 'lib/graybook/importer/aol.rb', line 11

def =~( options )
  options && options[:username] =~ /@(aol|aim)\.com$/i ? true : false
end

#loginObject

Login process:

  • Get mail.aol.com which redirects to a page containing a javascript redirect

  • Get the URL that the javascript is supposed to redirect you to

  • Fill out and submit the login form

  • Get the URL from another javascript redirect



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graybook/importer/aol.rb', line 22

def 
  page = agent.get( 'http://webmail.aol.com/' )

  form = page.forms.find{|form| form.name == 'AOLLoginForm'}
  form.loginId = options[:username].split('@').first # Drop the domain
  form.password = options[:password]
  page = agent.submit(form, form.buttons.first)

  case page.body
  when /Invalid Screen Name or Password. Please try again./
    return Graybook::Problem.new("Username and password were not accepted. Please check them and try again.")
  when /Terms of Service/
    return Graybook::Problem.new("Your AOL account is not setup for WebMail. Please signup: http://webmail.aol.com")
  end

  # aol bumps to a wait page while logging in.  if we can't scrape out the js then its a bad login
  extractor = proc { |var_name| page.body.scan(/var\s*#{var_name}\s*=\s*\"(.*?)\"\s*;/).first.first }

  base_uri = extractor.call( 'gSuccessPath' )
  return Graybook::Problem.new("An error occurred. Please try again.") unless base_uri
  page = agent.get base_uri
  true
end

#prepareObject

must login to prepare



49
50
51
# File 'lib/graybook/importer/aol.rb', line 49

def prepare
  
end

#scrape_contactsObject

The url to scrape contacts from has to be put together from the Auth cookie and a known uri that hosts their contact service. An array of hashes with :name and :email keys is returned.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/graybook/importer/aol.rb', line 58

def scrape_contacts
  unless auth_cookie = agent.cookies.find{|c| c.name =~ /^Auth/}
    return Graybook::Problem.new("An error occurred. Please try again.")
  end

  # jump through the hoops of formulating a request to get printable contacts
  uri = agent.current_page.uri.dup
  inputs = agent.current_page.search("//input")
  user = inputs.detect{|i| i['type'] == 'hidden' && i['name'] == 'user'}
  utoken = user['value']

  path = uri.path.split('/')
  path.pop
  path << 'addresslist-print.aspx'
  uri.path = path.join('/')
  uri.query = "command=all&sort=FirstLastNick&sortDir=Ascending&nameFormat=FirstLastNick&user=#{utoken}"
  page = agent.get uri.to_s

  # Grab all the contacts
  names = page.body.scan( /<span class="fullName">([^<]+)<\/span>/ ).flatten
  emails = page.body.scan( /<span>Email 1:<\/span> <span>([^<]+)<\/span>/ ).flatten
  (0...[names.size,emails.size].max).collect do |i|
    {
      :name => names[i],
      :email => emails[i]
    }
  end
end