-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterClient.js
55 lines (48 loc) · 1.57 KB
/
twitterClient.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
var Twitter = require( 'twitter');
var apiConfig = require( './config.json');
var url = require( 'url');
var client = new Twitter({
consumer_key: apiConfig.cKey,
consumer_secret: apiConfig.cSecret,
access_token_key: apiConfig.atKey,
access_token_secret: apiConfig.atSecret,
});
function queryPrefix(){
return 'search/tweets.json';
//return 'statuses/filter.json';
};
module.exports = {
requestHandler: function( req, res ) {
var jsonStr;
var reqURL = url.parse( req.url, true );
// console.log( "URL: " + req.url );
console.log( "in twitterClient.requestHandler" );
client.get( queryPrefix(), { q : reqURL.query.query }, function(error, tweets, response){
// console.log( "Tweets Count: " + tweets.statuses.length );
jsonStr = JSON.stringify( tweets.statuses.map( function(tweet) {
//console.log( tweet.text );
return { 'text': tweet.text };
}));
res.writeHead( 200, {'Content-Type': 'application/json'});
res.end( jsonStr );
if (!error) {
console.log(tweets.statuses[0]);
};
});
/*console.log( 'ReqURL: ' + reqURL.query.query );
client.stream( 'statuses/filter.json', {track : reqURL.query.query }, function(stream) {
res.writeHead( 200, {'Content-Type': 'text/plain' } );
stream.on('data', function(tweet) {
console.log(tweet.text);
res.write( tweet.text );
});
console.log( stream );
//stream.pipe( res );
stream.on('error', function(error) {
console.log( 'error');
throw error;
});
});*/
}
};
//exports.requestHandler = requestHandler;