-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-twitter-sample.js
85 lines (63 loc) · 2.24 KB
/
01-twitter-sample.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Twitter Sample
//
// Using the Twitter Streaming API. In this example, we retrieve the
// sample of the statuses in real time.
// Import node modules
var Twit = require('twit'), // Twitter API Client
config = require('./credentials.js'); // Twitter Credentials
// Configure the Twit object with the application credentials
var T = new Twit(config);
// Subscribe to the sample stream and begin listening
var stream = T.stream('statuses/sample');
// Event Listeners
// ===============
// Connection
// ----------
// The stream tries to connect to the Twitter Streaming API
stream.on('connect', function(request) {
console.log('Connection attempt.');
});
// The 'connected' event is triggered when the connection is successful.
stream.on('connected', function(response) {
console.log('Connection successful');
});
// The 'reconnect' event is triggered when a reconnection is scheduled.
stream.on('reconnect', function(req, res, interval) {
console.log('Reconnecting in ' + (interval / 1e3) + ' seconds.');
});
// Limits and Warnings
// -------------------
// The 'warning' event is triggered if the client is not processing the
// tweets fast enough.
stream.on('warning', function(warningMessage) {
console.warning('Twitter warning message:');
console.warning(warningMessage);
});
stream.on('limit', function(limitMessage) {
console.log('Twitter limit message:');
console.log(limitMessage)
})
// The 'disconnect' event is triggered when a disconnect message comes from
// Twitter.
stream.on('disconnect', function(msg) {
console.log('Twitter disconnection message');
console.log(msg);
});
// Tweets
// ------
// The callback will be invoked on each tweet. Here, we print the username
// and the text of the tweet in the screen.
stream.on('tweet', function(tweet) {
// Displays the tweet in the console
console.log('[@' + tweet.user.screen_name + ']: ' + tweet.text);
});
// A tweet deletion message comes in the stream
stream.on('delete', function(deleteMessage) {
console.log('delete message');
// console.log(deleteMessage);
});
// A location deletion message arrives
stream.on('scrub_geo', function(scrubGeoMessage) {
console.log('scrub_geo message');
// console.log(scrubGeoMessage);
});