Ruby on Rails

Depends how you interpret things

Black Book Web mail import gem-add mail provider


Black book is one the easiest way to add web email importing feature in your application. Recently I am working on a Social network cum movie portal site. By default, black book provides support for gmail, yahoo, hotmail, AOL and csv. But as per my requirement I have to add 20-30 mail providers from different countries. I started with http://www.free.fr.

require “blackbook”
require “hpricot”
require ‘blackbook/importer/page_scraper’

class Free < Blackbook::Importer::PageScraper
# your code
end

How to login into the users’ account for which we are fetching contacts.


def login
page = agent.get(‘http://imp.free.fr/&#8217;)
form = page.form_with(:name => “implogin”)
form.imapuser = options[:username]
form.fields[4].value = 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 =~ /Username and password do not match/
end


def scrape_contacts
unless agent.cookies.find{|c| c.name == “Horde”}
raise( Blackbook::BadCredentialsError, “Must be authenticated to access contacts.” )
end

page = agent.get(‘http://imp.free.fr/horde/turba/browse.php&#8217;)
contact_rows = (page.search(“form[@name=contacts]”))
data = Hpricot(contact_rows.to_s)
data = (data/”table[1]”)/”.listitem”
data.collect do |row|
{
:name  => (row/”a”).first.inner_text,
:email => (row/”a”).last.inner_text
}
end
end

Points to keep track

  • scraping contacts differs from one mail provider to another.
  • dependencies- mechanize, faster-csv for page scraping
  • we can extend  the mail provider within the black book gem as well as with in ur app[no need to patch gem]

2 responses to “Black Book Web mail import gem-add mail provider

  1. Bernard Courtney 2011/12/30 at 10:13 pm

    I am so grateful for your article post.Much thanks again. Awesome.

  2. Layton Wasserman 2011/12/30 at 10:51 pm

    Hey, thanks for the article.Thanks Again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: