linkedin-crawler
Version:
Automated crawling and messaging for LinkedIn
128 lines (110 loc) • 4.64 kB
JavaScript
const express = require('express')
const app = express()
const port = 3333
var path = require('path');
const fs = require('fs');
let updates = []
let currentFile = ""
let child
function fileData(username, password, message, search){
return `
from linkedin_api import Linkedin
mess = "${message}"
# Authenticate using any Linkedin account credentials
api = Linkedin('${username}', '${password}')
# Search People by term
profiles = api.search_people(keywords="${search}")
# Start Sending Messages
for i in range(len(profiles)):
mess.replace('<<firstname>>',(profiles[i].name.partition(" ")[0], profiles[i].name.partition(" ")[0])[len(profiles[i].name.partition(" ")[2]) > 1] )
mess.replace('<<lastname>>',(profiles[i].name.partition(" ")[0], profiles[i].name.partition(" ")[2])[len(profiles[i].name.partition(" ")[2]) > 1])
mess.replace('<<location>>',profiles[i].location)
mess.replace('<<currentjob>>',profiles[i].jobtitle)
# successBool = api.add_connection(profile_public_id = profiles[i].public_id, message='message')
successBool = api.add_connection(profiles[i].public_id, mess)
print(f'Sent Connection: {profiles[i].profile_public_id} - {successBool}')
`
}
function writeFile(fileString){
const fileStr = Date.now()
currentFile = fileStr
fs.writeFileSync(__dirname + `\\${fileStr}.py`, fileString)
}
function launchAndMonitor(){
child = require('child_process').spawn('python ' + __dirname + "\\" + currentFile + '.py',{shell:true});
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) {
updates.push(data.toString())
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
updates.push(data.toString())
updates.push('Broken')
});
child.on('close', function(code) {
updates.push('Finished')
})
}
app.use(express.json({
type: ['application/json', 'text/plain']
}))
app.get('/', (req, res) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
res.status(200).sendFile(path.join(__dirname + '/index.js'));
})
app.post('/writeFile',async (req,res) => {
try{
const str = fileData(req.body.username, req.body.password, req.body.message,req.body.search)
writeFile(str)
launchAndMonitor()
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
res.status(201).json({message:"wroteFile"})
} catch(err){
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
res.status(500).json({message:"error", err})
}
})
app.get('/updates', (req,res)=>{
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
try {
let arrToBeSent = []
if(updates.length>0){
for(let i=0;i<updates.length;i++){
arrToBeSent.push(updates[i])
}
updates = [...updates.slice(arrToBeSent.length)]
res.status(200).json(JSON.stringify(arrToBeSent)).end()
} else {
res.status(200).json(['noUpdates'])
}
} catch(err){
res.status(500).json({message:'update err'})
}
})
app.get('/pkill', (req,res)=>{
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
try{
fs.unlink(`${currentFile}.py`, (err)=>{if(err){console.log('failed to delete file, oops')}})
if(child.exitCode==0){
res.status(200).json({message:"child killed successfully"})
} else {
child.kill()
res.status(200).json({message:"child killed successfully"})
}
}catch(err){
res.status(200).json({message:"problem killing child"})
}
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})