-
-
Save craighooghiem/5816913599eafe7c069eadd7ca28f06a to your computer and use it in GitHub Desktop.
A phantomjs script to:a) Login to Linkedin.com b) Scrape all your contacts c) Visit all the contacts d) Map potential contacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var auth = { | |
user: "USERNAME", | |
pass: "PASSWORD" | |
}; | |
function ParallelRunner (list, func, runners) { | |
function createSlots(runners) { | |
var slots = []; | |
for (var i = 0; i < runners; i++) { | |
slots.push( objReturner() ); | |
} | |
return slots; | |
} | |
function objReturner() { | |
return { free: true }; | |
} | |
function toArray(obj) { | |
return Array.prototype.slice.call(obj); | |
} | |
function bind(scope, fn /*, variadic args to curry */ ) { | |
var args = Array.prototype.slice.call(arguments, 2); | |
return function() { | |
return fn.apply(scope, args.concat(toArray(arguments))); | |
}; | |
} | |
function _next() { | |
var slot = this.getFreeSlot(); | |
if (slot !== null) { | |
slot.free = false; | |
var cb = function() { | |
slot.free = true; | |
}; | |
if (list.length !== 0) { | |
var next = list.pop(); | |
func(next, cb); | |
} else {} | |
} | |
} | |
this.start = function() { | |
interval = setInterval(this.next, 100); | |
}; | |
this.stop = function(){ | |
clearInterval(interval); | |
}; | |
function _getFreeSlot() { | |
for (var i = slots.length - 1; i >= 0; i--) { | |
if (slots[i].free) { | |
return slots[i]; | |
} | |
} | |
return null; | |
} | |
runners = runners || 3; | |
var slots = createSlots(runners), | |
interval; | |
// Something weird happens with objects returned by PhantomJS page.evaluate() | |
// Fix em by rebuilding the objects. ONLY WORKS w/ SIMPLE OBJECTS!!! | |
list = JSON.parse(JSON.stringify(list)); | |
this.next = bind(this, _next); | |
this.getFreeSlot = bind(this, _getFreeSlot); | |
} | |
function trackActivity(name) { | |
var np = require("webpage").create(); | |
np.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
// Open their profile page | |
np.open(name.href, function(status) { | |
if (status === "success") { | |
np.evaluate(function() { | |
function click(el) { | |
var ev = document.createEvent("MouseEvent"); | |
ev.initMouseEvent( | |
"click", | |
true /* bubble */ , true /* cancelable */ , | |
window, null, | |
0, 0, 0, 0, /* coordinates */ | |
false, false, false, false, /* modifier keys */ | |
0 /*left*/ , null | |
); | |
el.dispatchEvent(ev); | |
} | |
var name = document.querySelector(".full-name"); | |
if (name !== null) { | |
console.log("Scraping Activity From " + name.textContent); | |
} | |
setTimeout(function() { | |
var activity = document.querySelector(".pv-recent-activity-activities__list-container--two-columns"); | |
if (activity !== null) { | |
console.log("Activity Link Grabbed " + name.textContent); | |
// TODO: Count the number of activities in the past 7 days | |
// Rank connection based on activity level | |
// Loop back to connections to look at similar connections | |
} else { | |
console.log("No Activity Feed Found"); | |
} | |
}, 3000); | |
}); | |
window.setTimeout(function() { | |
np.close(); | |
cb(); | |
}, 6000); | |
} | |
}); | |
} | |
var page = require("webpage").create(); | |
var names = []; | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
page.open("https://www.linkedin.com/people/connections", function(status) { | |
if (status === "success") { | |
page.evaluate(function() { | |
function click(el) { | |
var ev = document.createEvent("MouseEvent"); | |
ev.initMouseEvent( | |
"click", | |
true /* bubble */ , true /* cancelable */ , | |
window, null, | |
0, 0, 0, 0, /* coordinates */ | |
false, false, false, false, /* modifier keys */ | |
0 /*left*/ , null | |
); | |
el.dispatchEvent(ev); | |
} | |
var button = document.querySelector("#register-custom-nav a"); | |
click(button); | |
}); | |
window.setTimeout(function() { | |
page.evaluate(function(auth) { | |
document.querySelector("#session_key-login").value = auth.user; | |
document.querySelector("#session_password-login").value = auth.pass; | |
document.querySelector("#login").submit(); | |
console.log("Login submitted!"); | |
}, auth); | |
}, 1400); | |
/* Logged In Now */ | |
window.setTimeout(function() { | |
console.log("Get names"); | |
names = page.evaluate(function() { | |
var all = document.querySelectorAll(".conx-list li"); | |
var list = []; | |
// Get a list of all the names and loop through them to visit their profile | |
for (var i = all.length - 1; i >= 0; i--) { | |
var item = all[i]; | |
if (item.hasOwnProperty("id")) { | |
var nameInputs = item.getElementsByTagName("input"); | |
if (nameInputs.length > 0) { | |
//console.log(nameInputs[0].value, item.id); | |
list.push({ | |
name: nameInputs[0].value, | |
href: "http://www.linkedin.com/profile/view?id=" + item.id // working inconsistently | |
}); | |
} | |
} | |
} | |
console.log("Got " + list.length + " names"); | |
return list; | |
}); | |
var a = new ParallelRunner(names, trackActivity); | |
a.start(); | |
}, 6000); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment