Created
October 15, 2015 20:26
-
-
Save grgaortiz/13c0d717c359d9429703 to your computer and use it in GitHub Desktop.
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
/** | |
* Libraries | |
*/ | |
// Default | |
var request = require('request'); | |
var fs = require('fs'); | |
var async = require('async'); | |
// CSV | |
var json2csv = require('json2csv'); | |
var csv = require('csv'); | |
/** | |
* Google API | |
*/ | |
// Libraries | |
var google = require('googleapis'); | |
var customsearch = google.customsearch('v1'); | |
// Credentials | |
const CX = 'INSERT CUSTOM SEARCH ENGINE ID'; | |
const API_KEY = 'INSERT CUSTOM SEARCH API'; | |
const LOCATION = 'San Francisco'; | |
/** | |
* Params | |
*/ | |
// Data | |
var functionAsync = []; | |
var searchRequests = []; | |
var csvData = []; | |
var csvFields = ['EmployeeLinkedInUrl']; | |
var data = ['Google','Apple']; | |
// Counters | |
var requestCount = 0; | |
/** | |
* Create Async Functions | |
*/ | |
for (var i = 0; i < data.length; i++) { | |
var item = data[i]; | |
var query = '-intitle:directory (' + item + ') "current * * ' + item + '" "' + LOCATION + '"'; | |
searchRequests.push(query); | |
functionAsync.push(function (callback) { | |
var searchRequest = searchRequests[requestCount]; | |
customsearch.cse.list({cx: CX, q: searchRequest, auth: API_KEY}, function (err, resp) { | |
if (err) { | |
console.log('An error occured', err); | |
callback(); | |
} | |
if (resp.items && resp.items.length > 0) { | |
for (var i = 0; i < resp.items.length; i++) { | |
csvData.push({ | |
'EmployeeLinkedInUrl': resp.items[i].link | |
}); | |
} | |
callback(); | |
} | |
}); | |
requestCount++; | |
}); | |
} | |
async.parallel( | |
functionAsync, | |
function (err, results) { | |
json2csv({data: csvData, fields: csvFields}, function (err, csv) { | |
if (err) console.log(err); | |
fs.writeFile('path/to/EmployeeLinkedInUrls.csv', csv, function (err) { | |
if (err) throw err; | |
console.log('file saved'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment