Created
June 11, 2013 17:51
-
-
Save metalaureate/5759105 to your computer and use it in GitHub Desktop.
Get gender prediction, but be nice about it.
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
// server.js - extracts gender using igender api | |
var _ = require('underscore'); | |
var pg = require('pg'); //Postgres integration pg_ctl -D /usr/local/var/postgres -l logfile start | |
var request = require('request'); | |
var async = require('async'); | |
//Connect to database | |
var conString = "tcp://shill:@localhost:5432/swoon"; | |
var dbClient = new pg.Client(conString); | |
dbClient.connect(); | |
async.series([ | |
function (callback) { | |
var sql = "SELECT name from names_gender WHERE gender='' ORDER BY count DESC LIMIT 1"; | |
dbClient.query(sql, function (err, result) { | |
if (err) throw err; | |
async.each(result.rows, function (row, cb) { | |
console.log(row.name); | |
request.post({ | |
url: 'http://www.i-gender.com/ai?name=' + row.name, | |
json: true | |
}, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var sql = "UPDATE names_gender SET gender='" + body.gender + "', confidence=" + body.confidence + " WHERE name=E'" + escape(row.name) + "'"; | |
console.log(sql); | |
dbClient.query(sql, function (err, result) { | |
if (err) throw err; | |
}); | |
//WE NEED TO WAIT 3 SECONDS HERE!! | |
cb(null); | |
} else { | |
console.log("Error",err); | |
callback(error); | |
} | |
}); | |
}, function (err) { //final callback | |
callback(); | |
}); | |
}); | |
}, function (callback) { | |
console.log("Finished"); | |
callback(); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment