Skip to content

Instantly share code, notes, and snippets.

@trevorstarick
Created April 10, 2013 19:03
Show Gist options
  • Save trevorstarick/5357474 to your computer and use it in GitHub Desktop.
Save trevorstarick/5357474 to your computer and use it in GitHub Desktop.
var github = require("github");
var request = require('request');
var mysql = require('mysql');
var city, username, longitude, latitude;
var connection = mysql.createConnection({
host : "localhost",
user : "mysql",
password : "batman",
database : "geolocation"
});
var github = new github({version: "3.0.0"});
github.authenticate({
type: "oauth",
token: "a1fcf3751e2c17657a5f50705cf6650f7b611c4c"
});
function main(){
github.events.get({page: 1, per_page: 1},function(err, res){
username = res[0]['actor']['login'];
github.user.getFrom({user: username}, function(err, res){
city = res['location'];
getlocation(city);
});
});
}
function timestamp(){return Math.round((new Date()).getTime() / 1000);}
function getlocation(city){
request('http://maps.googleapis.com/maps/api/geocode/json?address='+city+'&sensor=false', function(err,res,body){
body = JSON.parse(body);
if (body['status'] == "OK") {
latitude = body["results"][0]["geometry"]["location"]['lat'];
longitude = body["results"][0]["geometry"]["location"]['lng'];
if (city) {
connection.query("INSERT INTO location (city,longitude,latitude,username,timestamp) VALUES('"+city+"','"+longitude+"','"+latitude+"','"+username+"','"+timestamp()+"');", function(err,rows,fields){});
}
console.log(username+', '+city+', '+latitude+', '+longitude);
//io.sockets.emit('data', {username: username, city: city, latitude: latitude, longitude: longitude});
}
});
}
setInterval(main, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment