-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwitter-fixed-async.js
68 lines (59 loc) · 2.26 KB
/
twitter-fixed-async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
/*
* Update: This example is based on the depracated Twitter 1.0 API. To make
* This work with the 1.1 API, you should just need to add authentication,
* and updated the API requests to use the new URL structure, such as:
https://api.twitter.com/1.1/search/tweets.json?q=node.js
* /end update
*
* A working example to illustrate callback management patterns. This code
* calls the twitter search API for recent posts on node.js, then pulls the
* username from each tweet, calls the twitter user lookup api to get the
* real name and number of followers of each twitter user, and prints this
* data to the console.
*
* This example shows the user of the async.js waterfall control flow to
* create a much more manageable code structure.
*/
var http = require('http'),
async = require('async');
async.waterfall([
// Twitter search for recent node.js posts
function(callback) {
http.get('http://search.twitter.com/search.json?q=node.js', function(res) {
tweets = '';
res.on('data', function (chunk) {
tweets += chunk;
});
res.on('end', function() {
var jtweets = JSON.parse(tweets);
callback(null, jtweets);
});
});
},
// parse username of each Twitter user
function(jtweets, callback) {
for(var tweet in jtweets['results']) {
//for(var tweet = 0;tweet < 1;tweet++) { // single iteration, for testing to avoid rate limit
var username = jtweets['results'][tweet]['from_user']
callback(null, username);
}
},
// Query Twitter API for user info, get name and followers
function(username, callback) {
http.get('http://api.twitter.com/1/users/lookup.json?screen_name=' + username, function(res) {
user_info = ''
res.on('data', function(chunk) {
user_info += chunk;
});
res.on('end', function() {
user = JSON.parse(user_info)[0];
callback(null, user['name'], user['followers_count']);
});
});
}],
// callback
function (err, name, followers) {
console.log('Name: ' + user['name'] + ' -- Followers: ' + user['followers_count']);
}
);