Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Mail.ru integration

  • Loading branch information...
commit 72dd554d34be9a598a9bafb57bacd22047cbe61b 1 parent d9c0a6e
@balepc balepc authored Lucas Carlson committed
View
3  .gitignore
@@ -1,3 +1,4 @@
.DS_Store
pkg/*
-test/accounts.yml
+test/accounts.yml
+nbproject/
View
1  lib/contacts.rb
@@ -9,3 +9,4 @@
require 'yahoo'
require 'plaxo'
require 'aol'
+require 'mailru'
View
11 lib/contacts/base.rb
@@ -65,6 +65,10 @@ def login
def password
@password
end
+
+ def skip_gzip?
+ false
+ end
private
@@ -132,13 +136,14 @@ def remove_cookie(cookie, cookies)
def post(url, postdata, cookies="", referer="")
url = URI.parse(url)
http = open_http(url)
- resp, data = http.post(url.path, postdata,
- "User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0",
+ http_header = { "User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0",
"Accept-Encoding" => "gzip",
"Cookie" => cookies,
"Referer" => referer,
"Content-Type" => 'application/x-www-form-urlencoded'
- )
+ }
+ http_header.reject!{|k, v| k == 'Accept-Encoding'} if skip_gzip?
+ resp, data = http.post(url.path, postdata, http_header)
data = uncompress(resp, data)
cookies = parse_cookies(resp.response['set-cookie'], cookies)
forward = resp.response['Location']
View
68 lib/contacts/mailru.rb
@@ -0,0 +1,68 @@
+require 'csv'
+
+class Contacts
+ class Mailru < Base
+ LOGIN_URL = "https://auth.mail.ru/cgi-bin/auth"
+ ADDRESS_BOOK_URL = "http://win.mail.ru/cgi-bin/abexport/addressbook.csv"
+
+ attr_accessor :cookies
+
+ def real_connect
+ username = login
+
+ postdata = "Login=%s&Domain=%s&Password=%s" % [
+ CGI.escape(username),
+ CGI.escape(domain_param(username)),
+ CGI.escape(password)
+ ]
+
+ data, resp, self.cookies, forward = post(LOGIN_URL, postdata, "")
+
+ if data.index("fail=1")
+ raise AuthenticationError, "Username and password do not match"
+ elsif cookies == "" or data == ""
+ raise ConnectionError, PROTOCOL_ERROR
+ end
+
+ data, resp, cookies, forward = get(login_token_link(data), login_cookies.join(';'))
+ end
+
+ def contacts
+ postdata = "confirm=1&abtype=6"
+ data, resp, cookies, forward = post(ADDRESS_BOOK_URL, postdata, login_cookies.join(';'))
+
+ @contacts = []
+ CSV.parse(data) do |row|
+ @contacts << [row[0], row[4]] unless header_row?(row)
+ end
+
+ @contacts
+ end
+
+ def skip_gzip?
+ true
+ end
+
+ private
+ def login_token_link(data)
+ data.match(/url=(.+)\">/)[1]
+ end
+
+ def login_cookies
+ self.cookies.split(';').collect{|c| c if (c.include?('t=') or c.include?('Mpop='))}.compact.collect{|c| c.strip}
+ end
+
+ def header_row?(row)
+ row[0] == 'AB-Name'
+ end
+
+ def domain_param(login)
+ login.include?('@') ?
+ login.match(/.+@(.+)/)[1] :
+ 'mail.ru'
+ end
+
+ end
+
+ TYPES[:mailru] = Mailru
+end
View
10 test/example_accounts.yml
@@ -38,3 +38,13 @@ aol:
-
name: "FirstName2 LastName2"
email_address: "firstname2@example.com"
+mailru:
+ username: <changeme>
+ password: <changeme>
+ contacts:
+ -
+ name: "FirstName1 LastName1"
+ email_address: "firstname1@example.com"
+ -
+ name: "FirstName2 LastName2"
+ email_address: "firstname2@example.com"
View
39 test/unit/mailru_contact_importer_test.rb
@@ -0,0 +1,39 @@
+dir = File.dirname(__FILE__)
+require "#{dir}/../test_helper"
+require 'contacts'
+
+class MailruContactImporterTest < ContactImporterTestCase
+ def setup
+ super
+ @account = TestAccounts[:mailru]
+ end
+
+ def test_successful_login
+ Contacts.new(:mailru, @account.username, @account.password)
+ end
+
+ def test_importer_fails_with_invalid_password
+ assert_raise(Contacts::AuthenticationError) do
+ Contacts.new(:mailru, @account.username, "wrong_password")
+ end
+ end
+
+ def test_importer_fails_with_blank_password
+ assert_raise(Contacts::AuthenticationError) do
+ Contacts.new(:mailru, @account.username, "")
+ end
+ end
+
+ def test_importer_fails_with_blank_username
+ assert_raise(Contacts::AuthenticationError) do
+ Contacts.new(:mailru, "", @account.password)
+ end
+ end
+
+ def test_fetch_contacts
+ contacts = Contacts.new(:mailru, @account.username, @account.password).contacts
+ @account.contacts.each do |contact|
+ assert contacts.include?(contact), "Could not find: #{contact.inspect} in #{contacts.inspect}"
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.