organizze-contacts 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/contacts.rb +10 -0
- data/lib/contacts/aol.rb +154 -0
- data/lib/contacts/base.rb +222 -0
- data/lib/contacts/gmail.rb +33 -0
- data/lib/contacts/hotmail.rb +125 -0
- data/lib/contacts/json_picker.rb +16 -0
- data/lib/contacts/mailru.rb +68 -0
- data/lib/contacts/plaxo.rb +130 -0
- data/lib/contacts/yahoo.rb +105 -0
- data/lib/organizze-contacts.rb +8 -0
- data/lib/organizze-contacts/version.rb +5 -0
- data/organizze-contacts.gemspec +25 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/contacts.rb
ADDED
data/lib/contacts/aol.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
class Contacts
|
2
|
+
require 'hpricot'
|
3
|
+
require 'csv'
|
4
|
+
class Aol < Base
|
5
|
+
URL = "http://www.aol.com/"
|
6
|
+
LOGIN_URL = "https://my.screenname.aol.com/_cqr/login/login.psp"
|
7
|
+
LOGIN_REFERER_URL = "http://webmail.aol.com/"
|
8
|
+
LOGIN_REFERER_PATH = "sitedomain=sns.webmail.aol.com&lang=en&locale=us&authLev=0&uitype=mini&loginId=&redirType=js&xchk=false"
|
9
|
+
AOL_NUM = "29970-343" # this seems to change each time they change the protocol
|
10
|
+
|
11
|
+
CONTACT_LIST_URL = "http://webmail.aol.com/#{AOL_NUM}/aim-2/en-us/Lite/ContactList.aspx?folder=Inbox&showUserFolders=False"
|
12
|
+
CONTACT_LIST_CSV_URL = "http://webmail.aol.com/#{AOL_NUM}/aim-2/en-us/Lite/ABExport.aspx?command=all"
|
13
|
+
PROTOCOL_ERROR = "AOL has changed its protocols, please upgrade this library first. If that does not work, dive into the code and submit a patch at http://github.com/cardmagic/contacts"
|
14
|
+
|
15
|
+
def real_connect
|
16
|
+
if login.strip =~ /^(.+)@aol\.com$/ # strip off the @aol.com for AOL logins
|
17
|
+
login = $1
|
18
|
+
end
|
19
|
+
|
20
|
+
postdata = {
|
21
|
+
"loginId" => login,
|
22
|
+
"password" => password,
|
23
|
+
"rememberMe" => "on",
|
24
|
+
"_sns_fg_color_" => "",
|
25
|
+
"_sns_err_color_" => "",
|
26
|
+
"_sns_link_color_" => "",
|
27
|
+
"_sns_width_" => "",
|
28
|
+
"_sns_height_" => "",
|
29
|
+
"offerId" => "mail-second-en-us",
|
30
|
+
"_sns_bg_color_" => "",
|
31
|
+
"sitedomain" => "sns.webmail.aol.com",
|
32
|
+
"regPromoCode" => "",
|
33
|
+
"mcState" => "initialized",
|
34
|
+
"uitype" => "std",
|
35
|
+
"siteId" => "",
|
36
|
+
"lang" => "en",
|
37
|
+
"locale" => "us",
|
38
|
+
"authLev" => "0",
|
39
|
+
"siteState" => "",
|
40
|
+
"isSiteStateEncoded" => "false",
|
41
|
+
"use_aam" => "0",
|
42
|
+
"seamless" => "novl",
|
43
|
+
"aolsubmit" => CGI.escape("Sign In"),
|
44
|
+
"idType" => "SN",
|
45
|
+
"usrd" => "",
|
46
|
+
"doSSL" => "",
|
47
|
+
"redirType" => "",
|
48
|
+
"xchk" => "false"
|
49
|
+
}
|
50
|
+
|
51
|
+
# Get this cookie and stick it in the form to confirm to Aol that your cookies work
|
52
|
+
data, resp, cookies, forward = get(URL)
|
53
|
+
postdata["stips"] = cookie_hash_from_string(cookies)["stips"]
|
54
|
+
postdata["tst"] = cookie_hash_from_string(cookies)["tst"]
|
55
|
+
|
56
|
+
data, resp, cookies, forward, old_url = get(LOGIN_REFERER_URL, cookies) + [URL]
|
57
|
+
until forward.nil?
|
58
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
59
|
+
end
|
60
|
+
|
61
|
+
data, resp, cookies, forward, old_url = get("#{LOGIN_URL}?#{LOGIN_REFERER_PATH}", cookies) + [LOGIN_REFERER_URL]
|
62
|
+
until forward.nil?
|
63
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
64
|
+
end
|
65
|
+
|
66
|
+
doc = Hpricot(data)
|
67
|
+
(doc/:input).each do |input|
|
68
|
+
postdata["usrd"] = input.attributes["value"] if input.attributes["name"] == "usrd"
|
69
|
+
end
|
70
|
+
# parse data for <input name="usrd" value="2726212" type="hidden"> and add it to the postdata
|
71
|
+
|
72
|
+
postdata["SNS_SC"] = cookie_hash_from_string(cookies)["SNS_SC"]
|
73
|
+
postdata["SNS_LDC"] = cookie_hash_from_string(cookies)["SNS_LDC"]
|
74
|
+
postdata["LTState"] = cookie_hash_from_string(cookies)["LTState"]
|
75
|
+
# raise data.inspect
|
76
|
+
|
77
|
+
data, resp, cookies, forward, old_url = post(LOGIN_URL, h_to_query_string(postdata), cookies, LOGIN_REFERER_URL) + [LOGIN_REFERER_URL]
|
78
|
+
|
79
|
+
until forward.nil?
|
80
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
81
|
+
end
|
82
|
+
|
83
|
+
if data.index("Invalid Username or Password. Please try again.")
|
84
|
+
raise AuthenticationError, "Username and password do not match"
|
85
|
+
elsif data.index("Required field must not be blank")
|
86
|
+
raise AuthenticationError, "Login and password must not be blank"
|
87
|
+
elsif data.index("errormsg_0_logincaptcha")
|
88
|
+
raise AuthenticationError, "Captcha error"
|
89
|
+
elsif data.index("Invalid request")
|
90
|
+
raise ConnectionError, PROTOCOL_ERROR
|
91
|
+
elsif cookies == ""
|
92
|
+
raise ConnectionError, PROTOCOL_ERROR
|
93
|
+
end
|
94
|
+
|
95
|
+
@cookies = cookies
|
96
|
+
end
|
97
|
+
|
98
|
+
def contacts
|
99
|
+
postdata = {
|
100
|
+
"file" => 'contacts',
|
101
|
+
"fileType" => 'csv'
|
102
|
+
}
|
103
|
+
|
104
|
+
return @contacts if @contacts
|
105
|
+
if connected?
|
106
|
+
data, resp, cookies, forward, old_url = get(CONTACT_LIST_URL, @cookies, CONTACT_LIST_URL) + [CONTACT_LIST_URL]
|
107
|
+
|
108
|
+
until forward.nil?
|
109
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
110
|
+
end
|
111
|
+
|
112
|
+
if resp.code_type != Net::HTTPOK
|
113
|
+
raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR)
|
114
|
+
end
|
115
|
+
|
116
|
+
# parse data and grab <input name="user" value="8QzMPIAKs2" type="hidden">
|
117
|
+
doc = Hpricot(data)
|
118
|
+
(doc/:input).each do |input|
|
119
|
+
postdata["user"] = input.attributes["value"] if input.attributes["name"] == "user"
|
120
|
+
end
|
121
|
+
|
122
|
+
data, resp, cookies, forward, old_url = get(CONTACT_LIST_CSV_URL, @cookies, CONTACT_LIST_URL) + [CONTACT_LIST_URL]
|
123
|
+
|
124
|
+
until forward.nil?
|
125
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
126
|
+
end
|
127
|
+
|
128
|
+
if data.include?("error.gif")
|
129
|
+
raise AuthenticationError, "Account invalid"
|
130
|
+
end
|
131
|
+
|
132
|
+
parse data
|
133
|
+
end
|
134
|
+
end
|
135
|
+
private
|
136
|
+
|
137
|
+
def parse(data, options={})
|
138
|
+
data = CSV::Reader.parse(data)
|
139
|
+
col_names = data.shift
|
140
|
+
@contacts = data.map do |person|
|
141
|
+
["#{person[0]} #{person[1]}", person[4]] if person[4] && !person[4].empty?
|
142
|
+
end.compact
|
143
|
+
end
|
144
|
+
|
145
|
+
def h_to_query_string(hash)
|
146
|
+
u = ERB::Util.method(:u)
|
147
|
+
hash.map { |k, v|
|
148
|
+
u.call(k) + "=" + u.call(v)
|
149
|
+
}.join("&")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
TYPES[:aol] = Aol
|
154
|
+
end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
require "cgi"
|
2
|
+
require "net/http"
|
3
|
+
require "net/https"
|
4
|
+
require "uri"
|
5
|
+
require "zlib"
|
6
|
+
require "stringio"
|
7
|
+
require "thread"
|
8
|
+
require "erb"
|
9
|
+
|
10
|
+
class Contacts
|
11
|
+
TYPES = {}
|
12
|
+
VERSION = "1.2.4"
|
13
|
+
|
14
|
+
class Base
|
15
|
+
def initialize(login, password, options={})
|
16
|
+
@login = login
|
17
|
+
@password = password
|
18
|
+
@captcha_token = options[:captcha_token]
|
19
|
+
@captcha_response = options[:captcha_response]
|
20
|
+
@connections = {}
|
21
|
+
connect
|
22
|
+
end
|
23
|
+
|
24
|
+
def connect
|
25
|
+
raise AuthenticationError, "Login and password must not be nil, login: #{@login.inspect}, password: #{@password.inspect}" if @login.nil? || @login.empty? || @password.nil? || @password.empty?
|
26
|
+
real_connect
|
27
|
+
end
|
28
|
+
|
29
|
+
def connected?
|
30
|
+
@cookies && !@cookies.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
def contacts(options = {})
|
34
|
+
return @contacts if @contacts
|
35
|
+
if connected?
|
36
|
+
url = URI.parse(contact_list_url)
|
37
|
+
http = open_http(url)
|
38
|
+
resp, data = http.get("#{url.path}?#{url.query}",
|
39
|
+
"Cookie" => @cookies
|
40
|
+
)
|
41
|
+
|
42
|
+
if resp.code_type != Net::HTTPOK
|
43
|
+
raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR)
|
44
|
+
end
|
45
|
+
|
46
|
+
parse(data, options)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def login
|
51
|
+
@attempt ||= 0
|
52
|
+
@attempt += 1
|
53
|
+
|
54
|
+
if @attempt == 1
|
55
|
+
@login
|
56
|
+
else
|
57
|
+
if @login.include?("@#{domain}")
|
58
|
+
@login.sub("@#{domain}","")
|
59
|
+
else
|
60
|
+
"#{@login}@#{domain}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def password
|
66
|
+
@password
|
67
|
+
end
|
68
|
+
|
69
|
+
def skip_gzip?
|
70
|
+
false
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def domain
|
76
|
+
@d ||= URI.parse(self.class.const_get(:URL)).host.sub(/^www\./,'')
|
77
|
+
end
|
78
|
+
|
79
|
+
def contact_list_url
|
80
|
+
self.class.const_get(:CONTACT_LIST_URL)
|
81
|
+
end
|
82
|
+
|
83
|
+
def address_book_url
|
84
|
+
self.class.const_get(:ADDRESS_BOOK_URL)
|
85
|
+
end
|
86
|
+
|
87
|
+
def open_http(url)
|
88
|
+
c = @connections[Thread.current.object_id] ||= {}
|
89
|
+
http = c["#{url.host}:#{url.port}"]
|
90
|
+
unless http
|
91
|
+
http = Net::HTTP.new(url.host, url.port)
|
92
|
+
if url.port == 443
|
93
|
+
http.use_ssl = true
|
94
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
95
|
+
end
|
96
|
+
c["#{url.host}:#{url.port}"] = http
|
97
|
+
end
|
98
|
+
http.start unless http.started?
|
99
|
+
http
|
100
|
+
end
|
101
|
+
|
102
|
+
def cookie_hash_from_string(cookie_string)
|
103
|
+
cookie_string.split(";").map{|i|i.split("=", 2).map{|j|j.strip}}.inject({}){|h,i|h[i[0]]=i[1];h}
|
104
|
+
end
|
105
|
+
|
106
|
+
def parse_cookies(data, existing="")
|
107
|
+
return existing if data.nil?
|
108
|
+
|
109
|
+
cookies = cookie_hash_from_string(existing)
|
110
|
+
|
111
|
+
data.gsub!(/ ?[\w]+=EXPIRED;/,'')
|
112
|
+
data.gsub!(/ ?expires=(.*?, .*?)[;,$]/i, ';')
|
113
|
+
data.gsub!(/ ?(domain|path)=[\S]*?[;,$]/i,';')
|
114
|
+
data.gsub!(/[,;]?\s*(secure|httponly)/i,'')
|
115
|
+
data.gsub!(/(;\s*){2,}/,', ')
|
116
|
+
data.gsub!(/(,\s*){2,}/,', ')
|
117
|
+
data.sub!(/^,\s*/,'')
|
118
|
+
data.sub!(/\s*,$/,'')
|
119
|
+
|
120
|
+
data.split(", ").map{|t|t.to_s.split(";").first}.each do |data|
|
121
|
+
k, v = data.split("=", 2).map{|j|j.strip}
|
122
|
+
if cookies[k] && v.empty?
|
123
|
+
cookies.delete(k)
|
124
|
+
elsif v && !v.empty?
|
125
|
+
cookies[k] = v
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
cookies.map{|k,v| "#{k}=#{v}"}.join("; ")
|
130
|
+
end
|
131
|
+
|
132
|
+
def remove_cookie(cookie, cookies)
|
133
|
+
parse_cookies("#{cookie}=", cookies)
|
134
|
+
end
|
135
|
+
|
136
|
+
def post(url, postdata, cookies="", referer="")
|
137
|
+
url = URI.parse(url)
|
138
|
+
http = open_http(url)
|
139
|
+
http_header = { "User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0",
|
140
|
+
"Accept-Encoding" => "gzip",
|
141
|
+
"Cookie" => cookies,
|
142
|
+
"Referer" => referer,
|
143
|
+
"Content-Type" => 'application/x-www-form-urlencoded'
|
144
|
+
}
|
145
|
+
http_header.reject!{|k, v| k == 'Accept-Encoding'} if skip_gzip?
|
146
|
+
resp, data = http.post(url.path, postdata, http_header)
|
147
|
+
data = uncompress(resp, data)
|
148
|
+
cookies = parse_cookies(resp.response['set-cookie'], cookies)
|
149
|
+
forward = resp.response['Location']
|
150
|
+
forward ||= (data =~ /<meta.*?url='([^']+)'/ ? CGI.unescapeHTML($1) : nil)
|
151
|
+
if (not forward.nil?) && URI.parse(forward).host.nil?
|
152
|
+
forward = url.scheme.to_s + "://" + url.host.to_s + forward
|
153
|
+
end
|
154
|
+
return data, resp, cookies, forward
|
155
|
+
end
|
156
|
+
|
157
|
+
def get(url, cookies="", referer="")
|
158
|
+
url = URI.parse(url)
|
159
|
+
http = open_http(url)
|
160
|
+
resp, data = http.get("#{url.path}?#{url.query}",
|
161
|
+
"User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0",
|
162
|
+
"Accept-Encoding" => "gzip",
|
163
|
+
"Cookie" => cookies,
|
164
|
+
"Referer" => referer
|
165
|
+
)
|
166
|
+
data = uncompress(resp, data)
|
167
|
+
cookies = parse_cookies(resp.response['set-cookie'], cookies)
|
168
|
+
forward = resp.response['Location']
|
169
|
+
if (not forward.nil?) && URI.parse(forward).host.nil?
|
170
|
+
forward = url.scheme.to_s + "://" + url.host.to_s + forward
|
171
|
+
end
|
172
|
+
return data, resp, cookies, forward
|
173
|
+
end
|
174
|
+
|
175
|
+
def uncompress(resp, data)
|
176
|
+
case resp.response['content-encoding']
|
177
|
+
when 'gzip'
|
178
|
+
gz = Zlib::GzipReader.new(StringIO.new(data))
|
179
|
+
data = gz.read
|
180
|
+
gz.close
|
181
|
+
resp.response['content-encoding'] = nil
|
182
|
+
# FIXME: Not sure what Hotmail was feeding me with their 'deflate',
|
183
|
+
# but the headers definitely were not right
|
184
|
+
when 'deflate'
|
185
|
+
data = Zlib::Inflate.inflate(data)
|
186
|
+
resp.response['content-encoding'] = nil
|
187
|
+
end
|
188
|
+
|
189
|
+
data
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
class ContactsError < StandardError
|
194
|
+
end
|
195
|
+
|
196
|
+
class AuthenticationError < ContactsError
|
197
|
+
end
|
198
|
+
|
199
|
+
class ConnectionError < ContactsError
|
200
|
+
end
|
201
|
+
|
202
|
+
class TypeNotFound < ContactsError
|
203
|
+
end
|
204
|
+
|
205
|
+
def self.new(type, login, password, options={})
|
206
|
+
if TYPES.include?(type.to_s.intern)
|
207
|
+
TYPES[type.to_s.intern].new(login, password, options)
|
208
|
+
else
|
209
|
+
raise TypeNotFound, "#{type.inspect} is not a valid type, please choose one of the following: #{TYPES.keys.inspect}"
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def self.guess(login, password, options={})
|
214
|
+
TYPES.inject([]) do |a, t|
|
215
|
+
begin
|
216
|
+
a + t[1].new(login, password, options).contacts
|
217
|
+
rescue AuthenticationError
|
218
|
+
a
|
219
|
+
end
|
220
|
+
end.uniq
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Contacts
|
2
|
+
class Gmail < Base
|
3
|
+
|
4
|
+
CONTACTS_SCOPE = 'http://www.google.com/m8/feeds/'
|
5
|
+
CONTACTS_FEED = CONTACTS_SCOPE + 'contacts/default/full/?max-results=1000'
|
6
|
+
|
7
|
+
def contacts
|
8
|
+
return @contacts if @contacts
|
9
|
+
end
|
10
|
+
|
11
|
+
def real_connect
|
12
|
+
@client = GData::Client::Contacts.new
|
13
|
+
@client.clientlogin(@login, @password, @captcha_token, @captcha_response)
|
14
|
+
|
15
|
+
feed = @client.get(CONTACTS_FEED).to_xml
|
16
|
+
|
17
|
+
@contacts = feed.elements.to_a('entry').collect do |entry|
|
18
|
+
title, email = entry.elements['title'].text, nil
|
19
|
+
entry.elements.each('gd:email') do |e|
|
20
|
+
email = e.attribute('address').value if e.attribute('primary')
|
21
|
+
end
|
22
|
+
[title, email] unless email.nil?
|
23
|
+
end
|
24
|
+
@contacts.compact!
|
25
|
+
rescue GData::Client::AuthorizationError => e
|
26
|
+
raise AuthenticationError, "Username or password are incorrect"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
TYPES[:gmail] = Gmail
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
class Contacts
|
2
|
+
class Hotmail < Base
|
3
|
+
URL = "https://login.live.com/login.srf?id=2"
|
4
|
+
OLD_CONTACT_LIST_URL = "http://%s/cgi-bin/addresses"
|
5
|
+
NEW_CONTACT_LIST_URL = "http://%s/mail/GetContacts.aspx"
|
6
|
+
CONTACT_LIST_URL = "http://mpeople.live.com/default.aspx?pg=0"
|
7
|
+
COMPOSE_URL = "http://%s/cgi-bin/compose?"
|
8
|
+
PROTOCOL_ERROR = "Hotmail has changed its protocols, please upgrade this library first. If that does not work, report this error at http://rubyforge.org/forum/?group_id=2693"
|
9
|
+
PWDPAD = "IfYouAreReadingThisYouHaveTooMuchFreeTime"
|
10
|
+
MAX_HTTP_THREADS = 8
|
11
|
+
|
12
|
+
def real_connect
|
13
|
+
data, resp, cookies, forward = get(URL)
|
14
|
+
old_url = URL
|
15
|
+
until forward.nil?
|
16
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
17
|
+
end
|
18
|
+
|
19
|
+
postdata = "PPSX=%s&PwdPad=%s&login=%s&passwd=%s&LoginOptions=2&PPFT=%s" % [
|
20
|
+
CGI.escape(data.split("><").grep(/PPSX/).first[/=\S+$/][2..-3]),
|
21
|
+
PWDPAD[0...(PWDPAD.length-@password.length)],
|
22
|
+
CGI.escape(login),
|
23
|
+
CGI.escape(password),
|
24
|
+
CGI.escape(data.split("><").grep(/PPFT/).first[/=\S+$/][2..-3])
|
25
|
+
]
|
26
|
+
|
27
|
+
form_url = data.split("><").grep(/form/).first.split[5][8..-2]
|
28
|
+
data, resp, cookies, forward = post(form_url, postdata, cookies)
|
29
|
+
|
30
|
+
old_url = form_url
|
31
|
+
until cookies =~ /; PPAuth=/ || forward.nil?
|
32
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
33
|
+
end
|
34
|
+
|
35
|
+
if data.index("The e-mail address or password is incorrect")
|
36
|
+
raise AuthenticationError, "Username and password do not match"
|
37
|
+
elsif data != ""
|
38
|
+
raise AuthenticationError, "Required field must not be blank"
|
39
|
+
elsif cookies == ""
|
40
|
+
raise ConnectionError, PROTOCOL_ERROR
|
41
|
+
end
|
42
|
+
|
43
|
+
data, resp, cookies, forward = get("http://mail.live.com/mail", cookies)
|
44
|
+
until forward.nil?
|
45
|
+
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
|
46
|
+
end
|
47
|
+
|
48
|
+
@domain = URI.parse(old_url).host
|
49
|
+
@cookies = cookies
|
50
|
+
rescue AuthenticationError => m
|
51
|
+
if @attempt == 1
|
52
|
+
retry
|
53
|
+
else
|
54
|
+
raise m
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def contacts(options = {})
|
59
|
+
if connected?
|
60
|
+
url = URI.parse(contact_list_url)
|
61
|
+
data, resp, cookies, forward = get( contact_list_url, @cookies )
|
62
|
+
|
63
|
+
if resp.code_type != Net::HTTPOK
|
64
|
+
raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR)
|
65
|
+
end
|
66
|
+
|
67
|
+
@contacts = []
|
68
|
+
build_contacts = []
|
69
|
+
go = true
|
70
|
+
index = 0
|
71
|
+
|
72
|
+
while(go) do
|
73
|
+
go = false
|
74
|
+
url = URI.parse(get_contact_list_url(index))
|
75
|
+
http = open_http(url)
|
76
|
+
resp, data = http.get(get_contact_list_url(index), "Cookie" => @cookies)
|
77
|
+
|
78
|
+
email_match_text_beginning = Regexp.escape("http://m.mail.live.com/?rru=compose&to=")
|
79
|
+
email_match_text_end = Regexp.escape("&")
|
80
|
+
|
81
|
+
raw_html = resp.body.split("
|
82
|
+
").grep(/(?:e|dn)lk[0-9]+/)
|
83
|
+
raw_html.inject(-1) do |memo, row|
|
84
|
+
c_info = row.match(/(e|dn)lk([0-9])+/)
|
85
|
+
|
86
|
+
# Same contact, or different?
|
87
|
+
build_contacts << [] if memo != c_info[2]
|
88
|
+
|
89
|
+
# Grab info
|
90
|
+
case c_info[1]
|
91
|
+
when "e" # Email
|
92
|
+
build_contacts.last[1] = row.match(/#{email_match_text_beginning}(.*)#{email_match_text_end}/)[1]
|
93
|
+
when "dn" # Name
|
94
|
+
build_contacts.last[0] = row.match(/<a[^>]*>(.+)<\/a>/)[1]
|
95
|
+
end
|
96
|
+
|
97
|
+
# Set memo to contact id
|
98
|
+
c_info[2]
|
99
|
+
end
|
100
|
+
|
101
|
+
go = resp.body.include?("ContactList_next")
|
102
|
+
index += 1
|
103
|
+
end
|
104
|
+
|
105
|
+
build_contacts.each do |contact|
|
106
|
+
unless contact[1].nil?
|
107
|
+
# Only return contacts with email addresses
|
108
|
+
contact[1] = CGI::unescape(contact[1])
|
109
|
+
@contacts << contact
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
return @contacts
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def get_contact_list_url(index)
|
118
|
+
"http://mpeople.live.com/default.aspx?pg=#{index}"
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
TYPES[:hotmail] = Hotmail
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
if !Object.const_defined?('ActiveSupport')
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
class Contacts
|
6
|
+
def self.parse_json( string )
|
7
|
+
if Object.const_defined?('ActiveSupport') and
|
8
|
+
ActiveSupport.const_defined?('JSON')
|
9
|
+
ActiveSupport::JSON.decode( string )
|
10
|
+
elsif Object.const_defined?('JSON')
|
11
|
+
JSON.parse( string )
|
12
|
+
else
|
13
|
+
raise 'Contacts requires JSON or Rails (with ActiveSupport::JSON)'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
class Contacts
|
4
|
+
class Mailru < Base
|
5
|
+
LOGIN_URL = "https://auth.mail.ru/cgi-bin/auth"
|
6
|
+
ADDRESS_BOOK_URL = "http://win.mail.ru/cgi-bin/abexport/addressbook.csv"
|
7
|
+
|
8
|
+
attr_accessor :cookies
|
9
|
+
|
10
|
+
def real_connect
|
11
|
+
username = login
|
12
|
+
|
13
|
+
postdata = "Login=%s&Domain=%s&Password=%s" % [
|
14
|
+
CGI.escape(username),
|
15
|
+
CGI.escape(domain_param(username)),
|
16
|
+
CGI.escape(password)
|
17
|
+
]
|
18
|
+
|
19
|
+
data, resp, self.cookies, forward = post(LOGIN_URL, postdata, "")
|
20
|
+
|
21
|
+
if data.index("fail=1")
|
22
|
+
raise AuthenticationError, "Username and password do not match"
|
23
|
+
elsif cookies == "" or data == ""
|
24
|
+
raise ConnectionError, PROTOCOL_ERROR
|
25
|
+
end
|
26
|
+
|
27
|
+
data, resp, cookies, forward = get(login_token_link(data), login_cookies.join(';'))
|
28
|
+
end
|
29
|
+
|
30
|
+
def contacts
|
31
|
+
postdata = "confirm=1&abtype=6"
|
32
|
+
data, resp, cookies, forward = post(ADDRESS_BOOK_URL, postdata, login_cookies.join(';'))
|
33
|
+
|
34
|
+
@contacts = []
|
35
|
+
CSV.parse(data) do |row|
|
36
|
+
@contacts << [row[0], row[4]] unless header_row?(row)
|
37
|
+
end
|
38
|
+
|
39
|
+
@contacts
|
40
|
+
end
|
41
|
+
|
42
|
+
def skip_gzip?
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def login_token_link(data)
|
48
|
+
data.match(/url=(.+)\">/)[1]
|
49
|
+
end
|
50
|
+
|
51
|
+
def login_cookies
|
52
|
+
self.cookies.split(';').collect{|c| c if (c.include?('t=') or c.include?('Mpop='))}.compact.collect{|c| c.strip}
|
53
|
+
end
|
54
|
+
|
55
|
+
def header_row?(row)
|
56
|
+
row[0] == 'AB-Name'
|
57
|
+
end
|
58
|
+
|
59
|
+
def domain_param(login)
|
60
|
+
login.include?('@') ?
|
61
|
+
login.match(/.+@(.+)/)[1] :
|
62
|
+
'mail.ru'
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
TYPES[:mailru] = Mailru
|
68
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
|
3
|
+
class Contacts
|
4
|
+
class Plaxo < Base
|
5
|
+
URL = "http://www.plaxo.com/"
|
6
|
+
LOGIN_URL = "https://www.plaxo.com/signin"
|
7
|
+
ADDRESS_BOOK_URL = "http://www.plaxo.com/po3/?module=ab&operation=viewFull&mode=normal"
|
8
|
+
CONTACT_LIST_URL = "http://www.plaxo.com/axis/soap/contact?_action=getContacts&_format=xml"
|
9
|
+
PROTOCOL_ERROR = "Plaxo has changed its protocols, please upgrade this library first. If that does not work, dive into the code and submit a patch at http://github.com/cardmagic/contacts"
|
10
|
+
|
11
|
+
def real_connect
|
12
|
+
|
13
|
+
end # real_connect
|
14
|
+
|
15
|
+
def contacts
|
16
|
+
getdata = "&authInfo.authByEmail.email=%s" % CGI.escape(login)
|
17
|
+
getdata += "&authInfo.authByEmail.password=%s" % CGI.escape(password)
|
18
|
+
data, resp, cookies, forward = get(CONTACT_LIST_URL + getdata)
|
19
|
+
|
20
|
+
if resp.code_type != Net::HTTPOK
|
21
|
+
raise ConnectionError, PROTOCOL_ERROR
|
22
|
+
end
|
23
|
+
|
24
|
+
parse data
|
25
|
+
end # contacts
|
26
|
+
|
27
|
+
private
|
28
|
+
def parse(data, options={})
|
29
|
+
doc = REXML::Document.new(data)
|
30
|
+
code = doc.elements['//response/code'].text
|
31
|
+
|
32
|
+
if code == '401'
|
33
|
+
raise AuthenticationError, "Username and password do not match"
|
34
|
+
elsif code == '200'
|
35
|
+
@contacts = []
|
36
|
+
doc.elements.each('//contact') do |cont|
|
37
|
+
name = if cont.elements['fullName']
|
38
|
+
cont.elements['fullName'].text
|
39
|
+
elsif cont.elements['displayName']
|
40
|
+
cont.elements['displayName'].text
|
41
|
+
end
|
42
|
+
email = if cont.elements['email1']
|
43
|
+
cont.elements['email1'].text
|
44
|
+
end
|
45
|
+
if name || email
|
46
|
+
@contacts << [name, email]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
@contacts
|
50
|
+
else
|
51
|
+
raise ConnectionError, PROTOCOL_ERROR
|
52
|
+
end
|
53
|
+
|
54
|
+
end # parse
|
55
|
+
|
56
|
+
end # Plaxo
|
57
|
+
|
58
|
+
TYPES[:plaxo] = Plaxo
|
59
|
+
|
60
|
+
end # Contacts
|
61
|
+
|
62
|
+
|
63
|
+
# sample contacts responses
|
64
|
+
=begin
|
65
|
+
Bad email
|
66
|
+
=========
|
67
|
+
<?xml version="1.0" encoding="utf-8" ?>
|
68
|
+
<ns1:GetContactsResponse xmlns:ns1="Plaxo">
|
69
|
+
<response>
|
70
|
+
<code>401</code>
|
71
|
+
<subCode>1</subCode>
|
72
|
+
<message>User not found.</message>
|
73
|
+
</response>
|
74
|
+
</ns1:GetContactsResponse>
|
75
|
+
|
76
|
+
|
77
|
+
Bad password
|
78
|
+
============
|
79
|
+
<?xml version="1.0" encoding="utf-8" ?>
|
80
|
+
<ns1:GetContactsResponse xmlns:ns1="Plaxo">
|
81
|
+
<response>
|
82
|
+
<code>401</code>
|
83
|
+
<subCode>4</subCode>
|
84
|
+
<message>Bad password or security token.</message>
|
85
|
+
</response>
|
86
|
+
</ns1:GetContactsResponse>
|
87
|
+
|
88
|
+
|
89
|
+
Success
|
90
|
+
=======
|
91
|
+
<?xml version="1.0" encoding="utf-8" ?>
|
92
|
+
<ns1:GetContactsResponse xmlns:ns1="Plaxo">
|
93
|
+
|
94
|
+
<response>
|
95
|
+
<code>200</code>
|
96
|
+
<message>OK</message>
|
97
|
+
<userId>77311236242</userId>
|
98
|
+
</response>
|
99
|
+
|
100
|
+
<contacts>
|
101
|
+
|
102
|
+
<contact>
|
103
|
+
<itemId>61312569</itemId>
|
104
|
+
<displayName>Joe Blow1</displayName>
|
105
|
+
<fullName>Joe Blow1</fullName>
|
106
|
+
<firstName>Joe</firstName>
|
107
|
+
<lastName>Blow1</lastName>
|
108
|
+
<homeEmail1>joeblow1@mailinator.com</homeEmail1>
|
109
|
+
<email1>joeblow1@mailinator.com</email1>
|
110
|
+
<folderId>5291351</folderId>
|
111
|
+
</contact>
|
112
|
+
|
113
|
+
<contact>
|
114
|
+
<itemId>61313159</itemId>
|
115
|
+
<displayName>Joe Blow2</displayName>
|
116
|
+
<fullName>Joe Blow2</fullName>
|
117
|
+
<firstName>Joe</firstName>
|
118
|
+
<lastName>Blow2</lastName>
|
119
|
+
<homeEmail1>joeblow2@mailinator.com</homeEmail1>
|
120
|
+
<email1>joeblow2@mailinator.com</email1>
|
121
|
+
<folderId>5291351</folderId>
|
122
|
+
</contact>
|
123
|
+
|
124
|
+
</contacts>
|
125
|
+
|
126
|
+
<totalCount>2</totalCount>
|
127
|
+
<editCounter>3</editCounter>
|
128
|
+
|
129
|
+
</ns1:GetContactsResponse>
|
130
|
+
=end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
class Contacts
|
2
|
+
class Yahoo < Base
|
3
|
+
URL = "http://mail.yahoo.com/"
|
4
|
+
LOGIN_URL = "https://login.yahoo.com/config/login"
|
5
|
+
ADDRESS_BOOK_URL = "http://address.mail.yahoo.com/?.rand=430244936"
|
6
|
+
CONTACT_LIST_URL = "http://address.mail.yahoo.com/?_src=&_crumb=crumb&sortfield=3&bucket=1&scroll=1&VPC=social_list&.r=time"
|
7
|
+
PROTOCOL_ERROR = "Yahoo has changed its protocols, please upgrade this library first. If that does not work, dive into the code and submit a patch at http://github.com/cardmagic/contacts"
|
8
|
+
|
9
|
+
def real_connect
|
10
|
+
postdata = ".tries=2&.src=ym&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass="
|
11
|
+
postdata += "&.partner=&.u=4eo6isd23l8r3&.v=0&.challenge=gsMsEcoZP7km3N3NeI4mX"
|
12
|
+
postdata += "kGB7zMV&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=1&.chkP=Y&."
|
13
|
+
postdata += "done=#{CGI.escape(URL)}&login=#{CGI.escape(login)}&passwd=#{CGI.escape(password)}"
|
14
|
+
|
15
|
+
data, resp, cookies, forward = post(LOGIN_URL, postdata)
|
16
|
+
|
17
|
+
if data.index("Invalid ID or password") || data.index("This ID is not yet taken")
|
18
|
+
raise AuthenticationError, "Username and password do not match"
|
19
|
+
elsif data.index("Sign in") && data.index("to Yahoo!")
|
20
|
+
raise AuthenticationError, "Required field must not be blank"
|
21
|
+
elsif !data.match(/uncompressed\/chunked/)
|
22
|
+
raise ConnectionError, PROTOCOL_ERROR
|
23
|
+
elsif cookies == ""
|
24
|
+
raise ConnectionError, PROTOCOL_ERROR
|
25
|
+
end
|
26
|
+
|
27
|
+
data, resp, cookies, forward = get(forward, cookies, LOGIN_URL)
|
28
|
+
|
29
|
+
if resp.code_type != Net::HTTPOK
|
30
|
+
raise ConnectionError, PROTOCOL_ERROR
|
31
|
+
end
|
32
|
+
|
33
|
+
@cookies = cookies
|
34
|
+
end
|
35
|
+
|
36
|
+
def contacts
|
37
|
+
return @contacts if @contacts
|
38
|
+
@contacts = []
|
39
|
+
|
40
|
+
if connected?
|
41
|
+
# first, get the addressbook site with the new crumb parameter
|
42
|
+
url = URI.parse(address_book_url)
|
43
|
+
http = open_http(url)
|
44
|
+
resp, data = http.get("#{url.path}?#{url.query}",
|
45
|
+
"Cookie" => @cookies
|
46
|
+
)
|
47
|
+
|
48
|
+
if resp.code_type != Net::HTTPOK
|
49
|
+
raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR)
|
50
|
+
end
|
51
|
+
|
52
|
+
crumb = data.to_s[/dotCrumb: '(.*?)'/][13...-1]
|
53
|
+
|
54
|
+
# now proceed with the new ".crumb" parameter to get the csv data
|
55
|
+
url = URI.parse(contact_list_url.sub("_crumb=crumb","_crumb=#{crumb}").sub("time", Time.now.to_f.to_s.sub(".","")[0...-2]))
|
56
|
+
http = open_http(url)
|
57
|
+
resp, more_data = http.get("#{url.path}?#{url.query}",
|
58
|
+
"Cookie" => @cookies,
|
59
|
+
"X-Requested-With" => "XMLHttpRequest",
|
60
|
+
"Referer" => address_book_url
|
61
|
+
)
|
62
|
+
|
63
|
+
if resp.code_type != Net::HTTPOK
|
64
|
+
raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR)
|
65
|
+
end
|
66
|
+
|
67
|
+
if more_data =~ /"TotalABContacts":(\d+)/
|
68
|
+
total = $1.to_i
|
69
|
+
((total / 50.0).ceil).times do |i|
|
70
|
+
# now proceed with the new ".crumb" parameter to get the csv data
|
71
|
+
url = URI.parse(contact_list_url.sub("bucket=1","bucket=#{i}").sub("_crumb=crumb","_crumb=#{crumb}").sub("time", Time.now.to_f.to_s.sub(".","")[0...-2]))
|
72
|
+
http = open_http(url)
|
73
|
+
resp, more_data = http.get("#{url.path}?#{url.query}",
|
74
|
+
"Cookie" => @cookies,
|
75
|
+
"X-Requested-With" => "XMLHttpRequest",
|
76
|
+
"Referer" => address_book_url
|
77
|
+
)
|
78
|
+
|
79
|
+
if resp.code_type != Net::HTTPOK
|
80
|
+
raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR)
|
81
|
+
end
|
82
|
+
|
83
|
+
parse more_data
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
@contacts
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def parse(data, options={})
|
94
|
+
@contacts ||= []
|
95
|
+
@contacts += Contacts.parse_json(data)["response"]["ResultSet"]["Contacts"].to_a.select{|contact|!contact["email"].to_s.empty?}.map do |contact|
|
96
|
+
name = contact["contactName"].split(",")
|
97
|
+
[[name.pop, name.join(",")].join(" ").strip, contact["email"]]
|
98
|
+
end if data =~ /^\{"response":/
|
99
|
+
@contacts
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
TYPES[:yahoo] = Yahoo
|
105
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "organizze-contacts/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "organizze-contacts"
|
7
|
+
s.version = Organizze::Contacts::VERSION
|
8
|
+
s.authors = ["Esdras Mayrink"]
|
9
|
+
s.email = ["falecom@oesdras.com.br"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Wrapper of the contacts gem using gdata_19}
|
12
|
+
s.description = %q{Wrapper of the contacts gem using gdata_19}
|
13
|
+
|
14
|
+
s.rubyforge_project = "organizze-contacts"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_dependency "json", ">= 1.1.1"
|
24
|
+
s.add_dependency "gdata_19", ">= 1.1.3"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: organizze-contacts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Esdras Mayrink
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-15 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: &70354606192660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70354606192660
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: gdata_19
|
27
|
+
requirement: &70354606192160 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.1.3
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70354606192160
|
36
|
+
description: Wrapper of the contacts gem using gdata_19
|
37
|
+
email:
|
38
|
+
- falecom@oesdras.com.br
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- Rakefile
|
46
|
+
- lib/.contacts.rb.swp
|
47
|
+
- lib/contacts.rb
|
48
|
+
- lib/contacts/.gmail.rb.swp
|
49
|
+
- lib/contacts/aol.rb
|
50
|
+
- lib/contacts/base.rb
|
51
|
+
- lib/contacts/gmail.rb
|
52
|
+
- lib/contacts/hotmail.rb
|
53
|
+
- lib/contacts/json_picker.rb
|
54
|
+
- lib/contacts/mailru.rb
|
55
|
+
- lib/contacts/plaxo.rb
|
56
|
+
- lib/contacts/yahoo.rb
|
57
|
+
- lib/organizze-contacts.rb
|
58
|
+
- lib/organizze-contacts/version.rb
|
59
|
+
- organizze-contacts.gemspec
|
60
|
+
homepage: ''
|
61
|
+
licenses: []
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project: organizze-contacts
|
80
|
+
rubygems_version: 1.8.11
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Wrapper of the contacts gem using gdata_19
|
84
|
+
test_files: []
|