require 'blackbook/importer/page_scraper' ## # Imports contacts from AOL class Blackbook::Importer::Aol < Blackbook::Importer::PageScraper ## # Matches this importer to an user's name/address def =~( options ) options && options[:username] =~ /@(aol|aim)\.com$/i ? true : false end ## # 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 def login 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 # aol generates the URL below from some JS magic in the wait_url page agent.get 'http://webmail.aol.com/31361/aol/en-us/Suite.aspx' end ## # must login to prepare def prepare login end ## # 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. def scrape_contacts unless auth_cookie = agent.cookies.find{|c| c.name =~ /^Auth/} raise( Blackbook::BadCredentialsError, "Must be authenticated to access contacts." ) end # Get user id from cookies user_id = auth_cookie.value.scan(/&uid:([^&]+)&/).first.first # Get contacts print page page = agent.get "http://webmail.aol.com/aim/en-us/Lite/addresslist-print.aspx?command=all&sort=FirstLastNick&sortDir=Ascending&nameFormat=FirstLastNick&user=#{user_id}" # Grab all the contacts names = page.body.scan( /([^<]+)<\/span>/ ).flatten emails = page.body.scan( /Email 1:<\/span> ([^<]+)<\/span>/ ).flatten (0...[names.size,emails.size].max).collect do |i| { :name => names[i], :email => emails[i] } end end Blackbook.register :aol, self end