Sha256: f396ba3884ade8ec56fd1e88c4e6dfaa02ed726020db641b66e066aacb1305de

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'json'

module Garlenko

  class Yahoo < Base

    LOGIN_URL = "https://login.yahoo.com/config/login_verify2"
    CONTACTS_URL = "http://address.mail.yahoo.com/?.rand=930244936"
    CONTACTS_LIST_URL ="http://address.mail.yahoo.com/?_src=&_crumb=crumb&sortfield=3&bucket=1&scroll=1&VPC=social_list&.r=time"

    include Crawler

    def connect!
      login_form = agent.get(LOGIN_URL).form("login_form")
      login_form.login = @username
      login_form.passwd = @password
      login_res = agent.submit(login_form, login_form.submits.first)
      address_page = agent.get(CONTACTS_URL)
      @connected = address_page.body.include?("masthead_welcome2")
    end

    def contacts
      return @contacts if @contacts
      connect! unless connected?
      @contacts = []
      @contacts = fetch_page(1, crumb_from(agent.get(CONTACTS_URL).body))
      @contacts
    end

    private

    def crumb_from(body)
      body.to_s[/dotCrumb:   '(.*?)'/][13...-1]
    end

    def fetch_page(page, crumb)
      url = URI.parse(CONTACTS_LIST_URL.sub("_crumb=crumb","_crumb=#{crumb}").sub("time", Time.now.to_f.to_s.sub('.', '')[0...-2]))
      data = agent.get(url, nil, CONTACTS_URL, {"X-Requested-With" => "XMLHttpRequest"}).body
      contacts_data, total_contacts, current_page = parse_response(data)
      contacts = parse_contacts_from(contacts_data) if contacts_data

      if contacts.size < total_contacts
        contacts += fetch_page(current_page + 1, crumb)
      end
      contacts
    end

    def parse_contacts_from(cs)
      cs.map do |contact|
        {:name => contact['contactName'], :email => contact['email'] }
      end
    end

    def parse_response(data)
      response = JSON.parse(data)['response']["ResultSet"]
      total = response['TotalContacts'].to_i
      page = response['FetchedBucket'].to_i
      return response['Contacts'], total, page
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
garlenko-0.0.2 lib/garlenko/yahoo.rb