Skip to content

Instantly share code, notes, and snippets.

@jed
Created Apr 2, 2011
Embed
What would you like to do?
a script that lets you know when your friends sign up for new ATND.org events

ATNDNT.js

Copyright (c) 2011 Jed Schmidt

ATNDNT.jsは第0回 node.jsハッカソンのために 作った、ATND.org通知サービスです。

ATND.orgで共通のイベントで参加している仲間が新しいイベントに参加したら教えてくれます。

下記のように使います。

node ATNDNT.js username [check_interval || 300]

処理の内容でいうと、

  1. 自分が参加したイベントを調べて、そのイベントの参加者を集める
  2. 共通イベントの数で参加をグループする
  3. 最も共通参加が多い方の20人を仲間として記憶する
  4. 仲間が参加している、現在から3ヶ月分のイベントを調べる
  5. check_interval毎(初期値:5分)にイベントを更新する
  6. 新しいイベントが出たら表示してブラウザで開く

です。

これから注目されつつあるnode.jsの人気イベントの参加に役に立つように!

質問や文句は@jedschmidtまで。

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 )
})
}
)
}
$ node atndnt.js jedschmidt 5
finding your events...
you have attended 6 events:
- 1 with 242 people,
- 2 with 33 people,
- 3 with 12 people,
- 4 with 5 people,
- and 5 with 2 people.
your twenty closest friends are:
- kawaz
- Jxck
- nsyee
- masahiroh
- meso
- akymrk
- altnight
- atomer_
- fu_sato
- ferrylikeboy
- ngs
- rabe
- nakat.el
- takaesu0
- norry_gogo
- Yamashiro0217
- vanx2
- 8maki_
- shinout
- shootaroo
your friends are attending 10 events in the next 3 months.
events will be updated in 5 seconds.
updating your friends' events...
no events found at Sat Apr 02 2011 19:18:15 GMT+0900 (JST).
events will be updated in 5 seconds.
updating your friends' events...
no events found at Sat Apr 02 2011 19:18:21 GMT+0900 (JST).
events will be updated in 5 seconds.
updating your friends' events...
your friends are attending a new event!
- title: demo
- url: http://atnd.org/events/14354
events will be updated in 5 seconds.
updating your friends' events...
no events found at Sat Apr 02 2011 19:18:34 GMT+0900 (JST).
events will be updated in 5 seconds.
updating your friends' events...
no events found at Sat Apr 02 2011 19:18:40 GMT+0900 (JST).
events will be updated in 5 seconds.
updating your friends' events...
no events found at Sat Apr 02 2011 19:18:46 GMT+0900 (JST).
events will be updated in 5 seconds.
updating your friends' events...
no events found at Sat Apr 02 2011 19:18:51 GMT+0900 (JST).
events will be updated in 5 seconds.
updating your friends' events...
no events found at Sat Apr 02 2011 19:18:56 GMT+0900 (JST).
events will be updated in 5 seconds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment