rails_address_fetcher 1.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
File without changes
@@ -0,0 +1,17 @@
1
+ Author.txt
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/address_fetcher.rb
7
+ lib/address_fetcher/aol.rb
8
+ lib/address_fetcher/base.rb
9
+ lib/address_fetcher/gmail.rb
10
+ lib/address_fetcher/hotmail.rb
11
+ lib/address_fetcher/mechanize_patch.rb
12
+ lib/address_fetcher/yahoo.rb
13
+ test/aol_test.rb
14
+ test/gmail_test.rb
15
+ test/hotmail_test.rb
16
+ test/test_helper.rb
17
+ test/yahoo_test.rb
@@ -0,0 +1,14 @@
1
+ = rails-address-fetcher
2
+ A ruby lib to fetch email address, such as Gmail, Yahoo, Hotmail, AOL.
3
+
4
+ == Install
5
+
6
+ gem install -l rails_address_fetcher-1.0.gem
7
+
8
+ == Usage
9
+
10
+ {{{
11
+ require "address_fetcher"
12
+ result = AddressFetcher::AddressFetcher.fetch :username => 'xxx', :password => 'xxx'
13
+ }}}
14
+
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+
4
+ Hoe.new('rails_address_fetcher', '1.0') do |p|
5
+ p.author = 'Naveen Joshi'
6
+ p.email = 'joshinav@gmail.com'
7
+ p.url = ''
8
+ p.summary = 'Email Address fetcher lib for ruby'
9
+ p.description = 'A ruby lib to fetch email address'
10
+ p.extra_deps << [ "mechanize", ">= 0.7.0" ]
11
+ p.extra_deps << [ "fastercsv", ">= 1.2.3" ]
12
+ end
@@ -0,0 +1,35 @@
1
+ require 'singleton'
2
+
3
+ module AddressFetcher
4
+ class AddressFetcher
5
+ include Singleton
6
+
7
+ attr_accessor :fetchers
8
+
9
+ def initialize
10
+ @fetchers = { }
11
+ end
12
+
13
+ def self.fetch(options = {})
14
+ instance.fetchers.each do |name, fetcher|
15
+ if fetcher =~ options
16
+ return fetcher.fetch(options)
17
+ end
18
+ end
19
+ nil
20
+ end
21
+
22
+ def self.register(name, fetcher_class)
23
+ fetcher = fetcher_class.new
24
+ raise ArgumentError, 'Unknown fetcher' unless fetcher.kind_of? Base
25
+ instance.fetchers[name] = fetcher
26
+ end
27
+ end
28
+ end
29
+
30
+ require File.dirname(__FILE__) + '/address_fetcher/base'
31
+ require File.dirname(__FILE__) + '/address_fetcher/gmail'
32
+ require File.dirname(__FILE__) + '/address_fetcher/yahoo'
33
+ require File.dirname(__FILE__) + '/address_fetcher/hotmail'
34
+ require File.dirname(__FILE__) + '/address_fetcher/aol'
35
+ require File.dirname(__FILE__) + '/address_fetcher/mechanize_patch'
@@ -0,0 +1,38 @@
1
+ module AddressFetcher
2
+ class Aol < Base
3
+ def =~(options)
4
+ options && options[:username] =~ /@aol.com$/i ? true : false
5
+ end
6
+
7
+ def login
8
+ page = @agent.get("http://webmail.aol.com")
9
+ login_form = page.form('AOLLoginForm')
10
+ login_form.loginId = options[:username].split("@").first
11
+ login_form.password = options[:password]
12
+ page = @agent.submit(login_form)
13
+
14
+ raise BadCredentialsError, "Wrong username and password pair" if page.body =~ /Invalid Screen Name or Password. Please try again./
15
+
16
+ redirect_url = page.body.scan(/http:\/\/[^']*/).first
17
+ page = @agent.get redirect_url
18
+
19
+ success_url = page.body.scan(/gSuccessPath[^"]*\"([^"]*)".*/).flatten.first
20
+ page = @agent.get success_url
21
+ end
22
+
23
+ def do_fetch
24
+ raise BadCredentialsError, "Login at first" unless auth_cookie = agent.cookies.find{|c| c.name =~ /^Auth/}
25
+
26
+ user = auth_cookie.value.scan(/&uid:([^&]+)&/).first.first
27
+ page = agent.get "http://webmail.aol.com/aim/en-us/Lite/addresslist-print.aspx?command=all&sort=LastFirstNick&sortDir=Ascending&nameFormat=LastFirstNick&user=#{user}"
28
+
29
+ names = page.body.scan(/<span class="fullName">([^<]+)<\/span>/).flatten
30
+ emails = page.body.scan(/<span>Email 1:<\/span> <span>([^<]+)<\/span>/).flatten
31
+
32
+ (0...[names.size,emails.size].max).collect do |i|
33
+ { :name => names[i], :email => emails[i] }
34
+ end
35
+ end
36
+ end
37
+ AddressFetcher.register(:aol, Aol)
38
+ end
@@ -0,0 +1,41 @@
1
+ begin
2
+ require 'mechanize'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ end
7
+
8
+ module AddressFetcher
9
+ class AddressFetcherError < StandardError
10
+ end
11
+
12
+ class BadCredentialsError < AddressFetcherError
13
+ end
14
+
15
+ class Base
16
+ attr_accessor :options
17
+ attr_accessor :agent
18
+
19
+ def =~(options); end
20
+
21
+ def create_agent
22
+ @agent = WWW::Mechanize.new
23
+ @agent.user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9"
24
+ @agent.keep_alive = false
25
+ @agent
26
+ end
27
+
28
+ def fetch(options)
29
+ @options = options
30
+ create_agent
31
+ login
32
+ do_fetch
33
+ end
34
+
35
+ def do_fetch; end
36
+
37
+ def type
38
+ self.class.name.split("::").last
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ module AddressFetcher
2
+ class Gmail < Base
3
+ def =~(options)
4
+ options && options[:username] =~ /@gmail.com$/i ? true : false
5
+ end
6
+
7
+ def login
8
+ page = @agent.get("http://www.gmail.com")
9
+ login_form = page.forms.first
10
+ login_form.Email = @options[:username]
11
+ login_form.Passwd = @options[:password]
12
+ page = @agent.submit(login_form)
13
+
14
+ raise BadCredentialsError, "Wrong username and password pair." if page.body =~ /Username and password do not match/
15
+
16
+ url = page.search('//meta').first.attributes['content'].split(/[URL|url]='?/).last rescue nil
17
+ url = url[0...-1] if url[-1] == 39
18
+
19
+ page = @agent.get url
20
+ end
21
+
22
+ def do_fetch
23
+ raise BadCredentialsError, "Login at first" unless @agent.cookies.find { |c| c.name == 'GAUSR' && c.value == "mail:#{options[:username]}" }
24
+
25
+ page = @agent.get('http://mail.google.com/mail/h/?v=cl&pnl=a')
26
+ contact_rows = page.search("input[@name='c']/../..")
27
+ contact_rows.collect do |row|
28
+ columns = row/"td"
29
+
30
+ { :name => ( columns[1]/"b" ).inner_text, :email => columns[2].inner_html.gsub( /(\n|&nbsp;)/, '') }
31
+ end
32
+ end
33
+ end
34
+ AddressFetcher.register(:gmail, Gmail)
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'fastercsv'
2
+
3
+ module AddressFetcher
4
+ class Hotmail < Base
5
+ def =~(options)
6
+ options && options[:username] =~ /@hotmail.com$/i ? true : false
7
+ end
8
+
9
+ def login
10
+ page = @agent.get("http://login.live.com/login.srf?")
11
+ login_form = page.form("f1")
12
+ login_form.login = options[:username]
13
+ login_form.passwd = options[:password]
14
+ page = @agent.submit(login_form)
15
+
16
+ raise BadCredentialsError, "Wrong username and password pair" if page.body =~ /The e-mail address or password is incorrect/
17
+
18
+ page = agent.get( page.body.scan(/http\:\/\/[^"]+/).first )
19
+ end
20
+
21
+ def do_fetch
22
+ raise BadCredentialsError, "Login at first." unless agent.cookies.find { |c| c.name == 'MSPPre' && c.value == options[:username] }
23
+
24
+ page = agent.get('http://mail.live.com/mail/PrintShell.aspx?type=contact')
25
+ rows = page.search("//div[@class='ContactsPrintPane cPrintContact BorderTop']")
26
+ rows.collect do |row|
27
+ name = row.search("//div[@class='cDisplayName']").first.innerText.strip
28
+ name = name[0,(name.size-3)]
29
+ email = row.search("/table/tr/td[@class='Value']").find { |v| v.inner_text =~ /.*@.*/ }.inner_text.strip rescue ""
30
+ { :name => name, :email => email }
31
+ end
32
+ end
33
+ end
34
+ AddressFetcher.register(:hotmail, Hotmail)
35
+ end
@@ -0,0 +1,18 @@
1
+ require 'generator'
2
+
3
+ class WWW::Mechanize
4
+ def to_absolute_uri(url, cur_page=current_page())
5
+ unless url.is_a? URI
6
+ url = url.to_s.strip
7
+ url = URI.parse(self.class.html_unescape(SyncEnumerator.new(url.split(/%[0-9A-Fa-f]{2}/), url.scan(/%[0-9A-Fa-f]{2}/)).map { |x,y| "#{ URI.escape(x||'')}#{y}" }.join('').gsub(/%23/, '#')))
8
+ end
9
+
10
+ if url.relative?
11
+ raise 'no history. please specify an absolute URL' unless cur_page.uri
12
+ url = cur_page.uri + url
13
+ url.path.sub!(/^(\/\.\.)+(?=\/)/, '')
14
+ end
15
+
16
+ return url
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ require 'fastercsv'
2
+
3
+ module AddressFetcher
4
+ class Yahoo < Base
5
+ def =~(options)
6
+ options && options[:username] =~ /@yahoo.com$/i ? true : false
7
+ end
8
+
9
+ def login
10
+ page = @agent.get('https://login.yahoo.com/config/login_verify2')
11
+ login_form = page.form('login_form')
12
+ login_form.login = options[:username]
13
+ login_form.passwd = options[:password]
14
+ page = @agent.submit(login_form)
15
+
16
+ raise BadCredentialsError, "Wrong username and password pair." if page.body =~ /Invalid ID or password./
17
+
18
+ url = page.search('//meta').first.attributes['content'].split(/url=/).last rescue nil
19
+ page = @agent.get url
20
+ end
21
+
22
+ def do_fetch
23
+ page = @agent.get("http://address.yahoo.com/?1=&VPC=import_export")
24
+ raise BadCredentialsError, "Login at first" if page.body =~ /To access Yahoo! Address Book\.\.\..*Sign in./m
25
+
26
+ form = page.forms.last
27
+ csv = @agent.submit(form, form.buttons[2])
28
+ result = []
29
+ FasterCSV.parse(csv.body, :headers => true) do |row|
30
+ name = ""
31
+ name = [row[0], row[2]].select { |v| v.any? }.join(" ") rescue ""
32
+ name = row[4] if name.empty?
33
+ name = row[7] if name.empty?
34
+
35
+ email = row[4]
36
+ email = "#{row[7]}@yahoo.com" if email.empty?
37
+
38
+ result << {:name => name, :email => email}
39
+ end
40
+ result
41
+ end
42
+ end
43
+ AddressFetcher.register(:yahoo, Yahoo)
44
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ module AddressFetcher
4
+ class AolTest < Test::Unit::TestCase
5
+ def test_fetch
6
+ contacts = AddressFetcher.fetch(:username => 'joshinav@aol.com', :password => '14102000')
7
+ assert_kind_of Array, contacts
8
+ puts contacts.to_yaml
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ module AddressFetcher
4
+ class GmailTest < Test::Unit::TestCase
5
+ def test_fetch
6
+ contacts = AddressFetcher.fetch(:username => 'naveenrails@gmail.com', :password => '14102000')
7
+ assert_kind_of Array, contacts
8
+ puts contacts.to_yaml
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ module AddressFetcher
4
+ class HotmailTest < Test::Unit::TestCase
5
+ def test_fetch
6
+ contacts = AddressFetcher.fetch(:username => 'aadyanav@hotmail.com', :password => 'UgtaSuraj')
7
+ assert_kind_of Array, contacts
8
+ puts contacts.to_yaml
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+
3
+ require 'test/unit'
4
+ require 'address_fetcher'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + "/test_helper"
2
+
3
+ module AddressFetcher
4
+ class YahooTest < Test::Unit::TestCase
5
+ def test_fetch
6
+ contacts = AddressFetcher.fetch(:username => 'aadyanav@yahoo.com', :password => '14102000')
7
+ assert_kind_of Array, contacts
8
+ puts contacts.to_yaml
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_address_fetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Naveen Joshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-02-13 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mechanize
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.7.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: fastercsv
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.2.3
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: hoe
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.0
41
+ version:
42
+ description: A ruby lib to fetch email address
43
+ email: joshinav@gmail.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - Author.txt
50
+ - History.txt
51
+ - Manifest.txt
52
+ - README.txt
53
+ files:
54
+ - Author.txt
55
+ - History.txt
56
+ - Manifest.txt
57
+ - README.txt
58
+ - Rakefile
59
+ - lib/address_fetcher.rb
60
+ - lib/address_fetcher/aol.rb
61
+ - lib/address_fetcher/base.rb
62
+ - lib/address_fetcher/gmail.rb
63
+ - lib/address_fetcher/hotmail.rb
64
+ - lib/address_fetcher/mechanize_patch.rb
65
+ - lib/address_fetcher/yahoo.rb
66
+ - test/aol_test.rb
67
+ - test/gmail_test.rb
68
+ - test/hotmail_test.rb
69
+ - test/test_helper.rb
70
+ - test/yahoo_test.rb
71
+ has_rdoc: true
72
+ homepage:
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --main
76
+ - README.txt
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project: rails_address_fetcher
94
+ rubygems_version: 1.0.1
95
+ signing_key:
96
+ specification_version: 2
97
+ summary: Email Address fetcher lib for ruby
98
+ test_files:
99
+ - test/test_helper.rb