Skip to content
This repository
Browse code

Mail.ru integration

  • Loading branch information...
commit 72dd554d34be9a598a9bafb57bacd22047cbe61b 1 parent d9c0a6e
Valery Vishnyakov balepc authored committed
3  .gitignore
... ... @@ -1,3 +1,4 @@
1 1 .DS_Store
2 2 pkg/*
3   -test/accounts.yml
  3 +test/accounts.yml
  4 +nbproject/
1  lib/contacts.rb
@@ -9,3 +9,4 @@
9 9 require 'yahoo'
10 10 require 'plaxo'
11 11 require 'aol'
  12 +require 'mailru'
11 lib/contacts/base.rb
@@ -65,6 +65,10 @@ def login
65 65 def password
66 66 @password
67 67 end
  68 +
  69 + def skip_gzip?
  70 + false
  71 + end
68 72
69 73 private
70 74
@@ -132,13 +136,14 @@ def remove_cookie(cookie, cookies)
132 136 def post(url, postdata, cookies="", referer="")
133 137 url = URI.parse(url)
134 138 http = open_http(url)
135   - resp, data = http.post(url.path, postdata,
136   - "User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0",
  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",
137 140 "Accept-Encoding" => "gzip",
138 141 "Cookie" => cookies,
139 142 "Referer" => referer,
140 143 "Content-Type" => 'application/x-www-form-urlencoded'
141   - )
  144 + }
  145 + http_header.reject!{|k, v| k == 'Accept-Encoding'} if skip_gzip?
  146 + resp, data = http.post(url.path, postdata, http_header)
142 147 data = uncompress(resp, data)
143 148 cookies = parse_cookies(resp.response['set-cookie'], cookies)
144 149 forward = resp.response['Location']
68 lib/contacts/mailru.rb
... ... @@ -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
10 test/example_accounts.yml
@@ -38,3 +38,13 @@ aol:
38 38 -
39 39 name: "FirstName2 LastName2"
40 40 email_address: "firstname2@example.com"
  41 +mailru:
  42 + username: <changeme>
  43 + password: <changeme>
  44 + contacts:
  45 + -
  46 + name: "FirstName1 LastName1"
  47 + email_address: "firstname1@example.com"
  48 + -
  49 + name: "FirstName2 LastName2"
  50 + email_address: "firstname2@example.com"
39 test/unit/mailru_contact_importer_test.rb
... ... @@ -0,0 +1,39 @@
  1 +dir = File.dirname(__FILE__)
  2 +require "#{dir}/../test_helper"
  3 +require 'contacts'
  4 +
  5 +class MailruContactImporterTest < ContactImporterTestCase
  6 + def setup
  7 + super
  8 + @account = TestAccounts[:mailru]
  9 + end
  10 +
  11 + def test_successful_login
  12 + Contacts.new(:mailru, @account.username, @account.password)
  13 + end
  14 +
  15 + def test_importer_fails_with_invalid_password
  16 + assert_raise(Contacts::AuthenticationError) do
  17 + Contacts.new(:mailru, @account.username, "wrong_password")
  18 + end
  19 + end
  20 +
  21 + def test_importer_fails_with_blank_password
  22 + assert_raise(Contacts::AuthenticationError) do
  23 + Contacts.new(:mailru, @account.username, "")
  24 + end
  25 + end
  26 +
  27 + def test_importer_fails_with_blank_username
  28 + assert_raise(Contacts::AuthenticationError) do
  29 + Contacts.new(:mailru, "", @account.password)
  30 + end
  31 + end
  32 +
  33 + def test_fetch_contacts
  34 + contacts = Contacts.new(:mailru, @account.username, @account.password).contacts
  35 + @account.contacts.each do |contact|
  36 + assert contacts.include?(contact), "Could not find: #{contact.inspect} in #{contacts.inspect}"
  37 + end
  38 + end
  39 +end

0 comments on commit 72dd554

Please sign in to comment.
Something went wrong with that request. Please try again.