Class: Linkedin

Inherits:
Object
  • Object
show all
Includes:
Searchy
Defined in:
lib/esearchy/linkedin.rb

Overview

http:///

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Searchy

#clean, #fix, #hash_url, #maxhits=, #print_emails, #search_depth, #search_docs, #search_emails, #search_office_xml, #search_pdfs, #search_txts

Constructor Details

#initialize(maxhits = nil) ⇒ Linkedin

Returns a new instance of Linkedin.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/esearchy/linkedin.rb', line 9

def initialize(maxhits=nil)
  @totalhits = maxhits || 0
  @pages = 1
  @emails = []
  @lock = Mutex.new
  @start = 0
  @threads = []
  @lock = Mutex.new
  @username = String.new
  @password = String.new
  @company_name = nil
  @cookie = nil
end

Instance Attribute Details

#company_nameObject

Returns the value of attribute company_name.



22
23
24
# File 'lib/esearchy/linkedin.rb', line 22

def company_name
  @company_name
end

#emailsObject

Returns the value of attribute emails.



22
23
24
# File 'lib/esearchy/linkedin.rb', line 22

def emails
  @emails
end

#passwordObject

Returns the value of attribute password.



22
23
24
# File 'lib/esearchy/linkedin.rb', line 22

def password
  @password
end

#usernameObject

Returns the value of attribute username.



22
23
24
# File 'lib/esearchy/linkedin.rb', line 22

def username
  @username
end

Instance Method Details

#create_emailsObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/esearchy/linkedin.rb', line 115

def create_emails
  @domain = @query.match(/@/) ? @query : ("@" + @query)
  @people.each do |person|
    name,last = person 
    @emails << "#{name.split(' ')[0]}.#{last.split(' ')[0]}#{@domain}"
    @emails << "#{name.first}#{last.split(' ')[0]}#{@domain}"
    #@emails.concat(fix(search_person(name,last)))
    @emails.uniq!
  end 
  print_emails(@emails)
end

#loginObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/esearchy/linkedin.rb', line 24

def 
  begin
    http = Net::HTTP.new("www.linkedin.com",443)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.start do |http|
      request = Net::HTTP::Post.new("/secure/login",
                                    {'Content-Type' => "application/x-www-form-urlencoded"})
      request.body = "session_key=#{@username}" +
                     "&session_password=#{@password}" +
                     "&session_login=Sign+In&session_login=&session_rikey="
      response = http.request(request)
      case response
      when Net::HTTPSuccess, Net::HTTPRedirection
        return response['Set-Cookie']
      else
        return response.error!
      end
    end
  rescue Net::HTTPFatalError
    puts "Error: Something went wrong with the HTTP request"
  end
end

#parse(string) ⇒ Object



95
96
97
# File 'lib/esearchy/linkedin.rb', line 95

def parse(string)
  @totalhits = string.scan(/<p class="summary>"<strong>(.*)<\/strong>/) if @totalhits == 0
end

#search(query) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/esearchy/linkedin.rb', line 48

def search(query)
  @query = query
  begin 
      @cookie = 
  rescue
    puts "Unable to parse Linkedin. Something went Wrong with the Credentials"
    return nil
  end
  begin
    http = Net::HTTP.new("www.linkedin.com",80)
    http.start do |http|
      #request = Net::HTTP::Get.new("/search?search=&viewCriteria=1&currentCompany=co" + 
      #          "&searchLocationType=Y&newnessType=Y" +
      #          "&proposalType=Y&pplSearchOrigin=ADVS&company=#{CGI.escape(@company_name)}" +
      #          "&sortCriteria=Relevance&page_num=#{@pages}", {'Cookie' => @cookie} )
      headers = {'Cookie' => @cookie, 'User-Agent' => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19"}
      request = Net::HTTP::Get.new("/search?search=&company=" + 
                                   CGI.escape(@company_name) +
                                   "&currentCompany=currentCompany" + 
                                   "&trk=coprofile_in_network_see_more" + 
                                   "&page_num=" + @pages.to_s, headers)
      response = http.request(request)
      case response
      when Net::HTTPSuccess, Net::HTTPRedirection
        parse(response.body)
        @start = @start + 10
        if @totalhits > @start
          @pages = @pages + 1
          puts "Searching in: #{self.class} up to point #{@start}"
          search_people(response.body)
          create_emails
          sleep(4)
          search(@query)
        else
          puts "Searching in: #{self.class} up to point #{@start}"
          search_people(response.body)
          create_emails
        end
      else
        return response.error!
      end
    end
  rescue Net::HTTPFatalError
    puts "Error: Something went wrong with the HTTP request"
  end
end

#search_people(string) ⇒ Object



99
100
101
# File 'lib/esearchy/linkedin.rb', line 99

def search_people(string)
  @people = string.scan(/span class="given-name">(.*)<\/span>[\n\s]+<span class="family-name">(.*)<\/span>/)
end

#search_person(name, last) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/esearchy/linkedin.rb', line 102

def search_person(name,last)
  email = []
  # Search Yahoo
  y = Yahoo.new(50)
  y.search("first:\"#{name}\" last:\"#{last}\"")
  emails.concat(y.emails).uniq!
  # Search Google
  #g = Google.new(50)
  #g.search("#{name} #{last}")
  #emails.concat(g.emails).uniq!
  return emails
end