Class: DuckScraper

Inherits:
Object
  • Object
show all
Includes:
CSVHandlers
Defined in:
lib/linsc/duck.rb

Instance Method Summary collapse

Methods included from CSVHandlers

#append_to_csv, #create_file, #create_file_with_headers, #create_row, #get_headers

Constructor Details

#initialize(working_dir, input_file, output_file, options) ⇒ DuckScraper

Returns a new instance of DuckScraper.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/linsc/duck.rb', line 21

def initialize(working_dir, input_file, output_file, options)
  @working_dir, @input_file, @output_file, @noproxy =
    working_dir, input_file, output_file, options[:noproxy]

  @headers = get_headers(@input_file)
  @headers.delete("LinkedIn Profile")
  @headers << "Linkedin Import Status" unless @headers.include?("Linkedin Import Status")
  @headers << "Urls" unless @headers.include?("Urls")
  @input_length = CSV.read(@input_file).length - 1
  if File.exist?(@output_file)
    @start = CSV.read(@output_file, headers: true).length
    puts "resuming from row #{@start + 1}"
  else
    create_file(@output_file)
  end
  @cooldown = 5
  @proxies = ProxyHandler.new(@cooldown) unless @noproxy
end

Instance Method Details

#append_ddg_row(row, status, urls) ⇒ Object



111
112
113
114
115
116
# File 'lib/linsc/duck.rb', line 111

def append_ddg_row(row, status, urls)
  row << ["Linkedin Import Status", status]
  row << ["Urls", urls]
  output_row = create_row(row, @headers)
  append_to_csv(@output_file, output_row)
end

#create_query(row) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/linsc/duck.rb', line 185

def create_query(row)
  query_parts = [row["First Name"], row["Last Name"], row["Employer 1 Title"],
                 row["Employer Organization Name 1"]]
  query_parts.collect! do |part|
    part.gsub!(row["Email"], ' ')
    part.downcase.alnum.strip
  end
  "linkedin #{query_parts.join(' ')}"
end

#find_profilesObject



40
41
42
43
44
45
46
47
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/linsc/duck.rb', line 40

def find_profiles
  count = 0

  CSV.foreach(@input_file, headers: true) do |input_row|
    count += 1
    next if @start && @start >= count
    if @proxies
      tries = @proxies.length
    else
      tries = 3
    end
    puts "ddg #{count}/#{@input_length}"
    begin
      lp = input_row["LinkedIn Profile"]
      input_row.delete("LinkedIn Profile")
      if lp && lp.include?('linkedin')
        puts "Existing Linkedin url found, skipping DDG"
        append_ddg_row(input_row, "Using existing url", lp)
        next
      end
      unless sufficient_data?(input_row)
        puts "Insufficient data, skipping"
        append_ddg_row(input_row, "Insufficient Data", nil)
        next
      end
      cert_file = Pathname.new(File.dirname __dir__).realdirpath + '../data/cacert.pem'
      cert_store = OpenSSL::X509::Store.new
      cert_store.add_file(cert_file.to_s)
      agent = Mechanize.new
      agent.cert_store = cert_store

      unless @noproxy
        proxy = @proxies.get_proxy
        agent.set_proxy(proxy.ip, proxy.port, proxy.username, proxy.password)
        agent.user_agent = proxy.user_agent
        puts "proxy: #{proxy.ip}"
      end
      sleep(@cooldown) if @noproxy
      query_string = create_query(input_row)
      puts "query string: #{query_string}"
      ddg_page = agent.get('https://www.duckduckgo.com/html')
      search_form = ddg_page.form_with(id: 'search_form_homepage')
      search_form.q = query_string
      results_page = agent.submit(search_form)
      urls = find_results(results_page, input_row)
      if urls.length > 0
        puts "Success! #{urls.length} possible urls found"
        append_ddg_row(input_row, "DDG results found", urls.join(', '))
      else
        puts "no results found"
        append_ddg_row(input_row, "No DDG results found", nil)
      end
      proxy.good if proxy

    rescue StandardError => msg
      tries -= 1
      if tries > 0
        puts "\n\n"
        puts msg.backtrace
        puts 'RETRYING'
        puts "\n\n"
        proxy.used if proxy
        retry
      else
        append_ddg_row(input_row, msg, nil)
        puts msg.backtrace
      end
    end
  end
end

#find_results(page, row) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/linsc/duck.rb', line 135

def find_results(page, row)
  matches = []
  full_name = "#{row['First Name']} #{row['Last Name']}".gsub(row["Email"], ' ').alnum.strip
  if page.css("#links .results_links_deep")
    results = page.css("#links .results_links_deep")
  else
    return matches
  end
  results.each do |result|
    if result.at_css("a.result__a")

      url_text = result.css("a.result__a").text.alnum
      url = result.at_css('a.result__a')['href']
      bio = result.css("a.result__snippet").text.alnum || ""
      valid_url = true
      short_title = row["Employer 1 Title"].alnum.split.first(2)
      short_employer = row["Employer Organization Name 1"].alnum.split.first

      if result.css("a.large").text.include?("profiles | LinkedIn")
        valid_url = false
      end
      unless url.include?("linkedin") && (url.include?("/in/") || url.include?("/pub/"))
        valid_url = false
      end

      if valid_url && name_check(url_text, full_name)
        if bio.downcase.include?(short_title.join(' ').downcase) && bio.downcase.include?(short_employer.to_s.downcase)
          matches.unshift(url)
        else
          matches.push(url)
        end
      else
      end
    end
  end
  matches
end

#name_check(lin_name, csv_name) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/linsc/duck.rb', line 173

def name_check(lin_name, csv_name)
  csv_array = csv_name.downcase.split(" ")
  lin_array = lin_name.downcase.split(" ")
  match = true
  csv_array.each do |chunk|
    unless lin_array.include?(chunk)
      match = false
    end
  end
  return match
end

#sufficient_data?(row) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/linsc/duck.rb', line 118

def sufficient_data?(row)
  data_presence = 0
  if row["First Name"] && row["First Name"].alnum.strip != ""
    data_presence += 1
  end
  if row["Last Name"] && row["Last Name"].alnum.strip != ""
    data_presence += 1
  end
  if row["Employer Organization Name 1"] && row["Employer Organization Name 1"].alnum.strip != ""
    data_presence += 1
  end
  if row["Employer 1 Title"] && row["Employer 1 Title"].alnum.strip != ""
    data_presence += 1
  end
  data_presence == 4 ? true : false
end