|
var http = require( "http" ) |
|
, url = require( "url" ) |
|
, exec = require( "child_process" ).exec |
|
, username = process.argv[ 2 ] |
|
, seconds = +process.argv[ 3 ] || 300 |
|
|
|
, friends |
|
, events = {} |
|
|
|
if ( !username ) { |
|
console.log( "no user specified." ) |
|
return |
|
} |
|
|
|
console.log( "\nfinding your events...\n" ) |
|
|
|
getEvents( username, function( data ) { |
|
var participants = {} |
|
, buckets = [] |
|
|
|
console.log( "you have attended " + data.length + " events:" ) |
|
|
|
data.forEach( function( event ) { |
|
event.users.forEach( function( user ) { |
|
if ( user.nickname == username ) return |
|
|
|
user = participants[ user.nickname ] || ( participants[ user.nickname ] = user ) |
|
user.common || ( user.common = 0 ) |
|
user.common++ |
|
}) |
|
}) |
|
|
|
for ( var name in participants ) { |
|
var user = participants[ name ] |
|
, bucket = buckets[ user.common ] || ( buckets[ user.common ] = [] ) |
|
|
|
bucket.push( user ) |
|
} |
|
|
|
buckets.forEach( function( bucket, i ) { |
|
console.log( i < buckets.length - 1 |
|
? "- " + i + " with " + bucket.length + " people," |
|
: "- and " + i + " with " + bucket.length + " people.\n" |
|
) |
|
}) |
|
|
|
friends = buckets |
|
.concat.apply( [], buckets ) |
|
.slice( -20 ) |
|
.reverse() |
|
|
|
console.log( "your twenty closest friends are:" ) |
|
|
|
friends.forEach( function( x ) { |
|
console.log( "- " + x.nickname ) |
|
}) |
|
|
|
getFriendEvents( function( data ) { |
|
console.log( |
|
"\nyour friends are attending " + data.length + |
|
" events in the next 3 months.\n" |
|
) |
|
|
|
data.forEach( function( event ) { |
|
events[ event.event_id ] = event |
|
}) |
|
|
|
console.log( "events will be updated in " + seconds + " seconds.\n" ) |
|
|
|
setTimeout( updateFriendEvents, seconds * 1000 ) |
|
}) |
|
}) |
|
|
|
function updateFriendEvents() { |
|
console.log( "updating your friends' events..." ) |
|
|
|
getFriendEvents( function( data ) { |
|
var found = false |
|
|
|
data.forEach( function( event ) { |
|
if ( event.event_id in events ) return |
|
|
|
events[ event.event_id ] = event |
|
|
|
found = true |
|
|
|
console.log( "\nyour friends are attending a new event!" ) |
|
console.log( "- title: " + event.title ) |
|
console.log( "- url: " + event.event_url ) |
|
|
|
setTimeout( function() { |
|
exec( "open " + event.event_url ) |
|
}, 3000 ) |
|
}) |
|
|
|
found || console.log( "no events found at " + new Date + "." ) |
|
|
|
console.log( "events will be updated in " + seconds + " seconds.\n" ) |
|
|
|
setTimeout( updateFriendEvents, seconds * 1000 ) |
|
}) |
|
} |
|
|
|
function getFriendEvents( cb ) { |
|
var now = Date.now() |
|
, months = [ |
|
new Date, |
|
new Date( now + 1000 * 60 * 60 * 24 * 30 * 1 ), |
|
new Date( now + 1000 * 60 * 60 * 24 * 30 * 2 ) |
|
].map( function( x ) { |
|
return x.getFullYear() + x.getMonth().toString().replace( /^\d$/, "0$&" ) |
|
}) |
|
|
|
http.get( |
|
{ |
|
host: "api.atnd.org", |
|
path: "/events/?format=json&ym=" + months + "&nickname=" + |
|
friends |
|
.map( function( x ){ return x.nickname } ) |
|
.join( "," ) |
|
}, |
|
|
|
function( res ) { |
|
var data = "" |
|
|
|
res.on( "data", function( chunk ){ data += chunk } ) |
|
res.on( "end", function() { |
|
cb( JSON.parse( data ).events ) |
|
}) |
|
} |
|
) |
|
} |
|
|
|
function getEvents( user, cb ) { |
|
http.get( |
|
{ |
|
host: "api.atnd.org", |
|
path: "/events/users/?format=json&nickname=" + user |
|
}, |
|
|
|
function( res ) { |
|
var data = "" |
|
|
|
res.on( "data", function( chunk ){ data += chunk } ) |
|
res.on( "end", function() { |
|
cb( JSON.parse( data ).events ) |
|
}) |
|
} |
|
) |
|
} |