forked from jdub/node-twitter
-
Notifications
You must be signed in to change notification settings - Fork 238
/
twitter.js
254 lines (213 loc) · 5.9 KB
/
twitter.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
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
244
245
246
247
248
249
250
251
252
253
254
'use strict';
/**
* Module dependencies
*/
var url = require('url');
var Streamparser = require('./parser');
var request = require('request');
var extend = require('deep-extend');
// Package version
var VERSION = require('../package.json').version;
function Twitter(options) {
if (!(this instanceof Twitter)) { return new Twitter(options) }
this.VERSION = VERSION;
// Merge the default options with the client submitted options
this.options = extend({
consumer_key: null,
consumer_secret: null,
access_token_key: null,
access_token_secret: null,
bearer_token: null,
rest_base: 'https://api.twitter.com/1.1',
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',
media_base: 'https://upload.twitter.com/1.1',
request_options: {
headers: {
Accept: '*/*',
Connection: 'close',
'User-Agent': 'node-twitter/' + VERSION
}
}
}, options);
// Default to user authentication
var authentication_options = {
oauth: {
consumer_key: this.options.consumer_key,
consumer_secret: this.options.consumer_secret,
token: this.options.access_token_key,
token_secret: this.options.access_token_secret
}
};
// Check to see if we are going to use User Authentication or Application Authetication
if (this.options.bearer_token) {
authentication_options = {
headers: {
Authorization: 'Bearer ' + this.options.bearer_token
}
};
}
// Configure default request options
this.request = request.defaults(
extend(
this.options.request_options,
authentication_options
)
);
}
Twitter.prototype.__buildEndpoint = function(path, base) {
var bases = {
'rest': this.options.rest_base,
'stream': this.options.stream_base,
'user_stream': this.options.user_stream_base,
'site_stream': this.options.site_stream_base,
'media': this.options.media_base
};
var endpoint = (bases.hasOwnProperty(base)) ? bases[base] : bases.rest;
if (url.parse(path).protocol) {
endpoint = path;
}
else {
// If the path begins with media or /media
if (path.match(/^(\/)?media/)) {
endpoint = bases.media;
}
endpoint += (path.charAt(0) === '/') ? path : '/' + path;
}
// Remove trailing slash
endpoint = endpoint.replace(/\/$/, '');
// Add json extension if not provided in call
endpoint += (path.split('.').pop() !== 'json') ? '.json' : '';
return endpoint;
};
Twitter.prototype.__request = function(method, path, params, callback) {
var base = 'rest';
// Set the callback if no params are passed
if (typeof params === 'function') {
callback = params;
params = {};
}
// Set API base
if (typeof params.base !== 'undefined') {
base = params.base;
delete params.base;
}
// Build the options to pass to our custom request object
var options = {
method: method.toLowerCase(), // Request method - get || post
url: this.__buildEndpoint(path, base) // Generate url
};
// Pass url parameters if get
if (method === 'get') {
options.qs = params;
}
// Pass form data if post
if (method === 'post') {
var formKey = 'form';
if (typeof params.media !== 'undefined') {
formKey = 'formData';
}
options[formKey] = params;
}
this.request(options, function(error, response, data) {
// request error
if (error) {
return callback(error, data, response);
}
// JSON parse error or empty strings
try {
// An empty string is a valid response
if (data === '') {
data = {};
}
else {
data = JSON.parse(data);
}
}
catch(parseError) {
return callback(
new Error('JSON parseError with HTTP Status: ' + response.statusCode + ' ' + response.statusMessage),
data,
response
);
}
// response object errors
// This should return an error object not an array of errors
if (data.errors !== undefined) {
return callback(data.errors, data, response);
}
// status code errors
if(response.statusCode < 200 || response.statusCode > 299) {
return callback(
new Error('HTTP Error: ' + response.statusCode + ' ' + response.statusMessage),
data,
response
);
}
// no errors
callback(null, data, response);
});
};
/**
* GET
*/
Twitter.prototype.get = function(url, params, callback) {
return this.__request('get', url, params, callback);
};
/**
* POST
*/
Twitter.prototype.post = function(url, params, callback) {
return this.__request('post', url, params, callback);
};
/**
* STREAM
*/
Twitter.prototype.stream = function(method, params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}
var base = 'stream';
if (method === 'user' || method === 'site') {
base = method + '_' + base;
}
var url = this.__buildEndpoint(method, base);
var request = this.request({url: url, qs: params});
var stream = new Streamparser();
stream.destroy = function() {
// FIXME: should we emit end/close on explicit destroy?
if ( typeof request.abort === 'function' ) {
request.abort(); // node v0.4.0
}
else {
request.socket.destroy();
}
};
request.on('response', function(response) {
if(response.statusCode !== 200) {
stream.emit('error', new Error('Status Code: ' + response.statusCode));
}
response.on('data', function(chunk) {
stream.receive(chunk);
});
response.on('error', function(error) {
stream.emit('error', error);
});
response.on('end', function() {
stream.emit('end', response);
});
});
request.on('error', function(error) {
stream.emit('error', error);
});
request.end();
if (typeof callback === 'function') {
callback(stream);
}
else {
return stream;
}
};
module.exports = Twitter;