Class: Linkedin::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/linkedin-scraper/profile.rb

Constant Summary collapse

USER_AGENTS =
['Windows IE 6', 'Windows IE 7', 'Windows Mozilla', 'Mac Safari', 'Mac FireFox', 'Mac Mozilla', 'Linux Mozilla', 'Linux Firefox', 'Linux Konqueror']
ATTRIBUTES =
%w(name first_name last_name title location country industry summary picture linkedin_url education groups websites languages skills certifications organizations past_companies current_companies recommended_visitors)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Profile

Returns a new instance of Profile.



18
19
20
21
# File 'lib/linkedin-scraper/profile.rb', line 18

def initialize(url)
  @linkedin_url = url
  @page         = http_client.get(url)
end

Instance Attribute Details

#linkedin_urlObject (readonly)

Returns the value of attribute linkedin_url.



10
11
12
# File 'lib/linkedin-scraper/profile.rb', line 10

def linkedin_url
  @linkedin_url
end

#pageObject (readonly)

Returns the value of attribute page.



10
11
12
# File 'lib/linkedin-scraper/profile.rb', line 10

def page
  @page
end

Class Method Details

.get_profile(url) ⇒ Object



12
13
14
15
16
# File 'lib/linkedin-scraper/profile.rb', line 12

def self.get_profile(url)
  Linkedin::Profile.new(url)
rescue => e
  puts e
end

Instance Method Details

#certificationsObject



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

def certifications
  @certifications ||= @page.search('background-certifications').map do |item|
    name       = item.at('h4').text.gsub(/\s+|\n/, ' ').strip                         rescue nil
    authority  = item.at('h5').text.gsub(/\s+|\n/, ' ').strip            rescue nil
    license    = item.at('.specifics/.licence-number').text.gsub(/\s+|\n/, ' ').strip rescue nil
    start_date = item.at('.certification-date').text.gsub(/\s+|\n/, ' ').strip        rescue nil

    { :name => name, :authority => authority, :license => license, :start_date => start_date }
  end
end

#countryObject



43
44
45
# File 'lib/linkedin-scraper/profile.rb', line 43

def country
  @country ||= (@page.at('.locality').text.split(',').last.strip if @page.at('.locality'))
end

#current_companiesObject



67
68
69
# File 'lib/linkedin-scraper/profile.rb', line 67

def current_companies
  @current_companies ||= get_companies('current')
end

#educationObject



71
72
73
74
75
76
77
78
79
# File 'lib/linkedin-scraper/profile.rb', line 71

def education
  @education ||= @page.search('.background-education .education').map do |item|
    name   = item.at('h4').text.gsub(/\s+|\n/, ' ').strip      if item.at('h4')
    desc   = item.at('h5').text.gsub(/\s+|\n/, ' ').strip      if item.at('h5')
    period = item.at('.education-date').text.gsub(/\s+|\n/, ' ').strip if item.at('.education-date')

    {:name => name, :description => desc, :period => period }
  end
end

#first_nameObject



27
28
29
# File 'lib/linkedin-scraper/profile.rb', line 27

def first_name
  @first_name ||= (@page.at('.full-name').text.split(' ', 2)[0].strip if @page.at('.full-name'))
end

#groupsObject



89
90
91
92
93
94
95
# File 'lib/linkedin-scraper/profile.rb', line 89

def groups
  @groups ||= @page.search('.groups-name').map do |item|
    name = item.text.gsub(/\s+|\n/, ' ').strip
    link = "http://www.linkedin.com#{item.at('a')['href']}"
    { :name => name, :link => link }
  end
end

#industryObject



47
48
49
# File 'lib/linkedin-scraper/profile.rb', line 47

def industry
  @industry ||= (@page.at('.industry').text.gsub(/\s+/, ' ').strip if @page.at('.industry'))
end

#languagesObject



107
108
109
110
111
112
113
# File 'lib/linkedin-scraper/profile.rb', line 107

def languages
  @languages ||= @page.search('.background-languages #languages ol li').map do |item|
    language    = item.at('h4').text rescue nil
    proficiency = item.at('div.languages-proficiency').text.gsub(/\s+|\n/, ' ').strip rescue nil
    { :language => language, :proficiency => proficiency }
  end
end

#last_nameObject



31
32
33
# File 'lib/linkedin-scraper/profile.rb', line 31

def last_name
  @last_name ||= (@page.at('.full-name').text.split(' ', 2)[1].strip if @page.at('.full-name'))
end

#locationObject



39
40
41
# File 'lib/linkedin-scraper/profile.rb', line 39

def location
  @location ||= (@page.at('.locality').text.split(',').first.strip if @page.at('.locality'))
end

#nameObject



23
24
25
# File 'lib/linkedin-scraper/profile.rb', line 23

def name
  "#{first_name} #{last_name}"
end

#organizationsObject



97
98
99
100
101
102
103
104
105
# File 'lib/linkedin-scraper/profile.rb', line 97

def organizations
  @organizations ||= @page.search('.background-organizations .organization p a').map do |item|
    name       = item.text.gsub(/\s+|\n/, ' ').strip rescue nil
    start_date, end_date = item.search('ul.specifics li').text.gsub(/\s+|\n/, ' ').strip.split(' to ')
    start_date = Date.parse(start_date) rescue nil
    end_date   = Date.parse(end_date)   rescue nil
    { :name => name, :start_date => start_date, :end_date => end_date }
  end
end

#past_companiesObject



63
64
65
# File 'lib/linkedin-scraper/profile.rb', line 63

def past_companies
  @past_companies ||= get_companies('past')
end

#pictureObject



55
56
57
# File 'lib/linkedin-scraper/profile.rb', line 55

def picture
  @picture ||= (@page.at('.profile-picture img').attributes['src'].value.strip if @page.at('.profile-picture img'))
end


127
128
129
130
131
132
133
134
135
136
# File 'lib/linkedin-scraper/profile.rb', line 127

def recommended_visitors
  @recommended_visitors ||= @page.search('.insights-browse-map/ul/li').map do |visitor|
    v = {}
    v[:link]    = visitor.at('a')['href']
    v[:name]    = visitor.at('h4/a').text
    v[:title]   = visitor.at('.browse-map-title').text.gsub('...', ' ').split(' at ').first
    v[:company] = visitor.at('.browse-map-title').text.gsub('...', ' ').split(' at ')[1]
    v
  end
end

#skillsObject



59
60
61
# File 'lib/linkedin-scraper/profile.rb', line 59

def skills
  @skills ||= (@page.search('.skill-pill .endorse-item-name-text').map { |skill| skill.text.strip if skill.text } rescue nil)
end

#summaryObject



51
52
53
# File 'lib/linkedin-scraper/profile.rb', line 51

def summary
  @summary ||= (@page.at('.summary .description').text.gsub(/\s+/, ' ').strip if @page.at('.summary .description'))
end

#titleObject



35
36
37
# File 'lib/linkedin-scraper/profile.rb', line 35

def title
  @title ||= (@page.at('.title').text.gsub(/\s+/, ' ').strip if @page.at('.title'))
end

#to_jsonObject



138
139
140
141
# File 'lib/linkedin-scraper/profile.rb', line 138

def to_json
  require 'json'
  ATTRIBUTES.reduce({}){ |hash,attr| hash[attr.to_sym] = self.send(attr.to_sym);hash }.to_json
end

#websitesObject



81
82
83
84
85
86
87
# File 'lib/linkedin-scraper/profile.rb', line 81

def websites
  @websites ||=  @page.search('#overview-summary-websites').flat_map do |site|
    url = "http://www.linkedin.com#{site.at('a')['href']}"
    CGI.parse(URI.parse(url).query)['url']
  end

end