author | Dominique Hazaël-Massieux <dom@w3.org> |
Wed, 31 Aug 2011 18:12:38 +0200 | |
changeset 186 | b05a161d3ad3 |
parent 183 | ff93e10d4604 |
child 187 | 2054c111a2fb |
permissions | -rw-r--r-- |
182
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
1 |
var http = require('http'); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
2 |
var EventEmitter = require('events').EventEmitter; |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
3 |
|
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
4 |
function loadTwitterListPage(owner, slug, cursor, users, callback) { |
183
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
5 |
// Rate limited to 150 / hour, beware! |
182
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
6 |
var request = http.get({host: 'api.twitter.com', path:'/1/lists/members.json?slug=' + slug + '&owner_screen_name=' + owner + '&skip_status=1&cursor=' + cursor}, function (response) { |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
7 |
response.setEncoding('utf8'); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
8 |
var twitterDataJSON = "", twitterData; |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
9 |
response.on('data', function (chunk) { |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
10 |
twitterDataJSON = twitterDataJSON + chunk; |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
11 |
}); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
12 |
response.on('end', function () { |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
13 |
console.log(response.statusCode + JSON.stringify(response.headers)); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
14 |
twitterData = JSON.parse(twitterDataJSON); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
15 |
users = users.concat(twitterData.users); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
16 |
if (twitterData.next_cursor) { |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
17 |
loadTwitterListPage(owner, slug, twitterData.next_cursor, users, callback); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
18 |
} else { |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
19 |
callback(users); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
20 |
} |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
21 |
}); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
22 |
}); |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
23 |
} |
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
24 |
|
183
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
25 |
exports.listTwitterIds = function(list_owner, list_slug, callback) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
26 |
loadTwitterListPage( |
186
b05a161d3ad3
fixing variable names
Dominique Hazaël-Massieux <dom@w3.org>
parents:
183
diff
changeset
|
27 |
list_owner, |
b05a161d3ad3
fixing variable names
Dominique Hazaël-Massieux <dom@w3.org>
parents:
183
diff
changeset
|
28 |
list_slug, |
183
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
29 |
-1, |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
30 |
[], |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
31 |
function (users) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
32 |
var twitterIds = []; |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
33 |
console.log(users); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
34 |
for (u in users) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
35 |
twitterIds.push(users[u].id); |
182
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
36 |
} |
183
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
37 |
callback(twitterIds); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
38 |
|
182
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
39 |
}); |
183
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
40 |
}; |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
41 |
|
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
42 |
exports.listenToTweets = function(twitter_ids, twitter_auth) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
43 |
var emitter = new EventEmitter(); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
44 |
var stream = http.request( |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
45 |
{host: 'stream.twitter.com', path:'/1/statuses/filter.json', 'method': 'POST'}, |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
46 |
function (res) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
47 |
res.setEncoding('utf8'); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
48 |
var incompleteChunk = ""; |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
49 |
res.on( |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
50 |
'data', |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
51 |
function (chunk) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
52 |
incompleteChunk += chunk; |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
53 |
incompleteChunk = incompleteChunk.trim(); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
54 |
if (incompleteChunk) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
55 |
try { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
56 |
var tweet = JSON.parse(incompleteChunk); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
57 |
emitter.emit("tweet", tweet); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
58 |
incompleteChunk = ""; |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
59 |
} catch (err) { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
60 |
console.log(err); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
61 |
} |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
62 |
} |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
63 |
}); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
64 |
res.on( |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
65 |
'end', |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
66 |
function () { |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
67 |
console.log("Twitter stream terminated with error " + res.statusCode); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
68 |
}); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
69 |
} |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
70 |
); |
182
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
71 |
stream.setHeader("Content-Type", "application/x-www-form-urlencoded"); |
183
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
72 |
stream.setHeader("Authorization", 'Basic ' + new Buffer(options.twitter_auth.name + ':' + options.twitter_auth.password).toString('base64')); |
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
73 |
stream.write("follow=" + twitter_ids.join(",")); |
182
659a8ba1c9f5
experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff
changeset
|
74 |
stream.end(); |
183
ff93e10d4604
reorganized as a module
Dominique Hazaël-Massieux <dom@w3.org>
parents:
182
diff
changeset
|
75 |
}; |