This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
TwitterAdapter.js
69 lines (57 loc) · 1.95 KB
/
TwitterAdapter.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
/*---------------------
:: TwitterAdapter
-> adapter
---------------------*/
// var request = require('request');
var Twit = require('twit');
module.exports = {
connections: {},
registerCollection: function (collection, cb) {
this.connections[collection.identity] = {
api: new Twit({
consumer_key: collection.config.consumerKey,
consumer_secret: collection.config.consumerSecret,
access_token: collection.config.accessToken,
access_token_secret: collection.config.accessTokenSecret
})
};
cb();
},
find: function (collectionName, options, cb) {
// for now, only use the "where" part of the criteria set
var criteria = options.where || {};
switch (collectionName) {
case 'location' : return this.trendingPlaces(collectionName, criteria, afterwards);
case 'trend' : return this.trends(collectionName, criteria, afterwards);
case 'tweet' : return this.searchTweets(collectionName, criteria, afterwards);
default: return afterwards('Unknown usage of find() with model ('+collectionName+') ');
}
function afterwards (err, results) {
if (err) return cb(err);
if (options.limit) return cb(null, _.first(results,options.limit));
return cb(err,results);
}
},
searchTweets: function (collectionName, criteria, cb) {
this.connections[collectionName].api.get('search/tweets', criteria, function (err, result) {
if (err) return cb(err);
if (!(result && result.statuses) ) return cb(result);
cb(err, result.statuses);
});
},
trends: function (collectionName, criteria, cb) {
this.connections[collectionName].api.get('trends/place', {
id: criteria.id || 1
}, function (err, result) {
if (err) return cb(err);
if (!(result[0] && result[0].trends) ) return cb(result);
cb(err, result[0].trends);
});
},
trendingPlaces: function (collectionName, criteria, cb) {
this.connections[collectionName].api.get('trends/closest', {
lat: criteria.lat || 0,
long: criteria.long || 0
}, cb);
}
};