Created
August 16, 2019 14:24
-
-
Save tristankirkpatrick/c8cb74546c00cd00402b9d6634e6a3b1 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
var Twit = require('twit') | |
var twitter = new Twit({ | |
consumer_key: 'xxx', | |
consumer_secret: 'xxx', | |
access_token: 'xxx', | |
access_token_secret: 'xxx', | |
timeout_ms: 60 * 1000, // optional HTTP request timeout to apply to all requests. | |
strictSSL: true, // optional - requires SSL certificates to be valid. | |
}) | |
var GoogleSheets = require('google-drive-sheets'); | |
var mySheet = new GoogleSheets('xxx'); | |
const MongoClient = require('mongodb').MongoClient; | |
const uri = "mongodb+srv://xxx:xxx@edutwitter-1-jkgue.mongodb.net/test?retryWrites=true&w=majority"; | |
const client = new MongoClient(uri, { useNewUrlParser: true }); | |
client.connect(err => { | |
mySheet.getRows(1, function(err, rowData) { | |
console.log('Pulled in ' + rowData.length + ' rows from Google Sheets'); | |
for (i = 0; i < rowData.length; i++) { | |
let specialism = rowData[i].specialism | |
let handle = rowData[i].twitterhandle | |
const collection = client.db("teachers_test").collection("teachers_test"); | |
collection.findOne({screen_name: handle}, (err, data) => { | |
if(data === null){ | |
twitter.get('users/show', {screen_name: handle}, function(err, data, response) { | |
if (typeof data.screen_name === "undefined") { | |
console.log(handle + " - This user does not exist on twitter") | |
} else { | |
Object.assign(data, {specialism: specialism}); | |
key = {'_id':data.id_str} | |
collection.update(key, data, {upsert:true}) | |
console.log(handle + " added to the DB.") | |
} | |
}) | |
} else { | |
console.log("This user is already on the DB") | |
} | |
}) | |
} | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment