-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
243 lines (191 loc) · 5.88 KB
/
Copy pathindex.js
File metadata and controls
243 lines (191 loc) · 5.88 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var es = require('event-stream')
, util = require('util')
, request = require('request')
, zlib = require('zlib')
;
function merge (defaults) {
for (var i = 1; i < arguments.length; i++) {
for (var opt in arguments[i]) {
defaults[opt] = arguments[i][opt];
}
}
return defaults;
};
function TweetPipe (oauth, options) {
// oauth should look like:
// { consumer_key: 'abc',
// consumer_secret: 'def',
// token: 'ghi',
// token_secret: 'jkl' }
var defaults = {
stream_base: 'https://stream.twitter.com/1.1',
user_stream_base: 'https://userstream.twitter.com/1.1',
site_stream_base: 'https://sitestream.twitter.com/1.1',
gzip: true, // use twitter's gzipped stream
headers: {
'Accept': '*/*',
'User-Agent': 'peeinears/tweet-streamer'
}
};
this.options = merge(defaults, options);
this.options.oauth = oauth;
if (this.options.gzip) {
this.options.headers['Accept-Encoding'] = 'deflate, gzip';
} else {
this.options.headers['Connection'] = 'close';
}
}
// returns the request stream
// just the raw, [un-inflated,] unparsed data
TweetPipe.prototype.raw_stream = function (method, params, callback) {
if (typeof params === 'function') {
callback = params;
params = null;
}
// Iterate on params properties, if any property is an array, convert it to comma-delimited string
if (params) {
Object.keys(params).forEach(function (item) {
if (util.isArray(params[item])) {
params[item] = params[item].join(',');
}
});
}
var stream_base = this.options.stream_base;
var http_method = 'GET';
// Stream type customizations
switch (method) {
case 'user':
stream_base = this.options.user_stream_base;
break;
case 'site':
stream_base = this.options.site_stream_base;
break;
case 'statuses/filter':
http_method = 'POST';
break;
}
var url = stream_base + '/' + escape(method) + '.json';
var req = request({
url: url,
method: http_method,
oauth: this.options.oauth,
headers: this.options.headers,
form: (http_method === 'POST' ? params : false)
});
if ( typeof callback === 'function' ) callback(req);
return req;
};
// returns a through stream that takes json and emits desired twitter messages
TweetPipe.prototype.filter = function (data_events) {
if (typeof data_events === 'undefined') {
data_events = ['tweet'];
}
// don't emit anything as 'data' if data_events is null or falsy
if (!data_events) data_events = [];
// don't allow 'all' and other events
if (data_events.indexOf('all') >= 0) data_events = ['all'];
// helper method for emitting data according to data_events
var emit = function (event, data) {
this.emit(event, data);
if (data_events.indexOf(event) >= 0) this.emit('data', data);
};
var filter = es.through(function (data) {
// https://dev.twitter.com/docs/streaming-apis/messages
// to catch all
emit.call(this, 'all', data);
// Public stream
if (data['delete']) {
emit.call(this, 'delete', data['delete']);
} else if (data['limit']) {
emit.call(this, 'limit', data['limit']);
} else if (data['scrub_geo']) {
emit.call(this, 'scrub_geo', data['scrub_geo']);
} else if (data['status_withheld']) {
emit.call(this, 'status_withheld', data['status_withheld']);
} else if (data['user_withheld']) {
emit.call(this, 'user_withheld', data['user_withheld']);
// User stream
} else if (data['friends']) {
emit.call(this, 'friends', data['friends']);
} else if (data['event']) {
emit.call(this, 'event', data['event']);
// TODO: support site stream messages
} else {
// must be a tweet, right?
emit.call(this, 'tweet', data);
}
this.resume();
});
return filter;
};
TweetPipe.prototype.stream = function (method, params, data_events, callback) {
// handle optional arguments
// params is an object, data_events is an array, callback is a function
[params, data_events, callback].forEach(function (arg) {
if (typeof arg === 'function') {
callback = arg;
} else if (typeof arg === 'object') {
params = arg;
} else {
data_events = arg;
}
});
var req = this.raw_stream(method, params);
var filter = this.filter(data_events);
var _end = filter.end;
filter.end = function (data) {
if (filter.timeout) clearTimeout(filter.timeout);
req.abort();
process.nextTick(function () {
if (data)
_end.call(filter, data);
else
_end.call(filter);
});
};
// convenience method for stopping streams after duration
filter.timeout = function (ms) {
filter.timer = setTimeout(function () {
filter.end();
}, ms);
};
req.on('error', function (error) {
filter.emit('error', error);
});
req.on('response', function (response) {
// any response code greater then 200 from stream API is an error
if (response.statusCode > 200) {
filter.emit('error', 'HTTP ' + response.statusCode);
}
response.on('error', function (error) {
filter.emit('error', error);
});
});
filter.on('error', function (error) {
console.log('error:', error)
});
// allow user to catch emitted events
if (typeof callback === 'function') callback(filter);
var stream = this.options.gzip ? req
.pipe(this.unzip())
.pipe(this.parse())
.pipe(filter)
: req
.pipe(this.parse())
.pipe(filter)
;
return stream;
};
// convenienve method for deflating gzipped streams
TweetPipe.prototype.inflate = TweetPipe.prototype.unzip = zlib.createUnzip;
TweetPipe.prototype.gzip = zlib.createGzip;
// convenienve method for converting to JSON
TweetPipe.prototype.parse = function () {
return es.pipeline(
es.split(),
es.parse()
);
};
// convenienve method for stringifying JSON
TweetPipe.prototype.stringify = es.stringify;
module.exports = TweetPipe;