Skip to content

Instantly share code, notes, and snippets.

@johnf
Created April 3, 2013 14:15
Show Gist options
  • Save johnf/5301582 to your computer and use it in GitHub Desktop.
Save johnf/5301582 to your computer and use it in GitHub Desktop.
Withdraw all LinkedIn Invitations
#!/usr/bin/env ruby
# Go through linked in invitations and withdraw them all
#
# Usage
#
# First collect the inviites
# ./uninvite collect email@example.com password > data.csv
#
# Then clean up the CSV in your favourite editor
#
# Then withdraw the invites
# ./uninvite execute email@example.com password < data.csv
require 'capybara'
require 'fileutils'
require 'capybara/poltergeist'
Capybara.configure do |config|
config.run_server = false
config.default_driver = :poltergeist
config.app_host = 'https://www.linkedin.com'
config.save_and_open_page_path = '/tmp'
end
class Linkedin
include Capybara::DSL
def initialize(email, pass)
usage unless email and pass
@email = email
@pass = pass
end
def signin
visit '/'
fill_in 'session_key', :with => @email
fill_in 'session_password', :with => @pass
click_on 'Sign In'
end
def collect
signin
visit '/inbox/invitations/sent'
loop do
html = Nokogiri::HTML page.body
check_invites html
next_link = html.css('a.pagination-next')
break if next_link.empty?
next_link = next_link.attr('href').value
unless next_link =~ /^http/
next_link = "http://www.linkedin.com/#{next_link}"
end
visit next_link
$stderr.puts next_link
end
end
def execute
signin
STDIN.read.split("\n").each do |line|
email, name, link = line.split(',')
print "Withdrawing #{email} - #{name}... "
unless link =~ /^http/
link = "http://www.linkedin.com/#{link}"
end
visit link
begin
click_on 'Withdraw'
rescue Capybara::ElementNotFound
puts "- Failed (No withdraw button)"
next
end
if page.body =~ /has been withdrawn/
puts "- OK"
next
else
puts "- Failed (Unknown)"
save_and_open_page
exit
end
end
end
def check_invites(html)
html.css('.message-item').each do |msg|
next if msg.css('span.item-status-accepted').size > 0
participant = msg.css('.participants')
if participant.size != 1
puts 'BAD PARTICIPANT'
puts participant.size
puts "#{msg}"
next
end
if participant.css('a').empty?
name = ''
email = participant.text.strip
else
participant = participant.css('a')
name = participant.first.content.strip
profile_link = participant.attr('href').value
visit profile_link
html = Nokogiri::HTML page.body
email_link = html.css('#email-view a')
email = email_link.empty? ? 'Unknown' : email_link.first.content.strip
end
link = msg.css('p.subject a').attr('href').value
email.gsub(/,/, '')
name.gsub(/,/, '')
puts "#{email},#{name},#{link}"
end
end
def usage
$stderr.puts 'usage: uninvite USER PASSWORD'
exit 1
end
end
if ARGV[0] == 'collect'
Linkedin.new(ARGV[1], ARGV[2]).collect
elsif ARGV[0] == 'execute'
Linkedin.new(ARGV[1], ARGV[2]).execute
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment