-
Notifications
You must be signed in to change notification settings - Fork 6
/
twitter.js
89 lines (67 loc) · 2.87 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
/****************************************************************************
Copyright (c) 2014, Scott Ganyo <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
****************************************************************************/
'use strict';
// define our Twitter functionality
// including setting and retrieving values from the cache as needed
module.exports = function Twitter(config) {
// ensure we've set up the config file
checkConfig(config);
// import the Twit module to make the network calls to Twitter
var Twit = require('twit');
// import our cache functions to cache and retrieve our cached results
var Cache = require('./cache');
// create the connection to Twitter
this.twit = new Twit(config.twitter);
// create the Cache
this.cache = new Cache(config.redis);
// a function that queries Twitter without going through the cache
this.queryNoCache = function(query, callback) {
var twitQuery = { q: process.argv[2], count: 10 };
this.twit.get('search/tweets', twitQuery, callback);
};
// a function that queries Twitter if nothing found in the cache
this.cachedQuery = function(query, callback) {
var self = this;
var cacheKey = makeCacheKey(query);
// retrieve results from cache
self.cache.get(cacheKey, function(err, results) {
if (err || results) {
console.log('result from cache');
return callback(err, results);
}
// retrieve results from twitter
self.queryNoCache(query, function(err, results) {
if (err || !results) { return callback(err); }
console.log('result direct from twitter');
// set the results in the cache
self.cache.set(cacheKey, results, function(err) {
callback(err, results);
});
});
});
};
// export cachedQuery function as query
this.query = this.cachedQuery;
// print an error an exit if config hasn't been setup
function checkConfig(config) {
if (config.twitter.consumer_key === '') {
console.log('Please set your keys in config.js before running.');
process.exit(1);
}
}
// create a compound key for the Cache using the Query
function makeCacheKey(query) {
return 'twitter:cache:' + query;
}
};