Class: Blackbook::Importer::Yahoo

Inherits:
PageScraper show all
Defined in:
lib/blackbook/importer/yahoo.rb

Overview

contacts importer for Yahoo!

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



12
13
14
# File 'lib/blackbook/importer/yahoo.rb', line 12

def =~(options = {})
  options && options[:username] =~ /@yahoo.com$/i ? true : false
end

#loginObject

login for Yahoo!



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/blackbook/importer/yahoo.rb', line 19

def 
  page = agent.get('https://login.yahoo.com/config/login_verify2?')
  form = page.forms.first
  form. = options[:username].split("@").first
  form.passwd = options[:password]
  page = agent.submit(form, form.buttons.first)
  
  # Check for login success
  raise( Blackbook::BadCredentialsError, "That username and password was not accepted. Please check them and try again." ) if page.body =~ /Invalid ID or password./
  true
end

#prepareObject

prepare the importer



34
35
36
# File 'lib/blackbook/importer/yahoo.rb', line 34

def prepare
  
end

#scrape_contactsObject

scrape yahoo contacts



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/blackbook/importer/yahoo.rb', line 41

def scrape_contacts
  page = agent.get("http://address.yahoo.com/?1=&VPC=import_export")
  if page.body =~ /To access Yahoo! Address Book\.\.\..*Sign in./m
    raise( Blackbook::BadCredentialsError, "Must be authenticated to access contacts." )
  end
  form = page.forms.last
  csv = agent.submit(form, form.buttons[2]) # third button is Yahoo-format CSV
  
  contact_rows = FasterCSV.parse(csv.body)
  labels = contact_rows.shift # TODO: Actually use the labels to find the indexes of the data we want
  contact_rows.collect do |row|
    next if !row[7].blank? && options[:username] =~ /^#{row[7]}/ # Don't collect self
    {
      :name  => "#{row[0]} #{row[2]}".to_s, 
      :email => (row[4] || "#{row[7]}@yahoo.com") # email is a field in the data, but will be blank for Yahoo users so we create their email address    
    } 
  end
end