Class: Blackbook::Importer::Aol

Inherits:
PageScraper show all
Defined in:
lib/blackbook/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/blackbook/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
# File 'lib/blackbook/importer/aol.rb', line 22

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

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

  raise( Blackbook::BadCredentialsError, "That username and password was not accepted. Please check them and try again." ) if page.body =~ /Invalid Screen Name or Password. Please try again./

  # aol bumps to a wait page while logging in.  if we can't scrape out the js then its a bad login
  wait_url = page.body.scan(/onLoad="checkError[^\)]+/).first.scan(/'([^']+)'/).last.first
  page = agent.get wait_url

  base_uri = page.body.scan(/^var gSuccessPath = \"(.+)\";/).first.first
  raise( Blackbook::BadCredentialsError, "You do not appear to be signed in." ) unless base_uri
  page = agent.get base_uri
end

#prepareObject

must login to prepare



44
45
46
# File 'lib/blackbook/importer/aol.rb', line 44

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blackbook/importer/aol.rb', line 53

def scrape_contacts
  unless auth_cookie = agent.cookies.find{|c| c.name =~ /^Auth/}
    raise( Blackbook::BadCredentialsError, "Must be authenticated to access contacts." )
  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