-
Notifications
You must be signed in to change notification settings - Fork 0
/
francoREST.js
130 lines (117 loc) · 3.84 KB
/
francoREST.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
#!/usr/bin/env/node
console.log("Hey James Franco");
var env = require('node-env-file');
var Twit = require('twit');
var Q = require('q');
var Mailgun = require('mailgun').Mailgun;
var mgRecipients = require('./mailgun-recipients');
env(__dirname + '/.env');
// credentials stored in local, uncommitted .env file
var T = new Twit({
consumer_key: process.env.TWITTER_CONSUMER_KEY
, consumer_secret: process.env.TWITTER_CONSUMER_SECRET
, access_token: process.env.TWITTER_ACCESS_TOKEN
, access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
var mg = new Mailgun(process.env.MAILGUN_API_KEY);
// search twitter for (count) number of tweets to @jamesfrancotv. returns promise that fullfills with array of tweets
function getMentions(count) {
return Q.Promise(function (resolve, reject) {
T.get('search/tweets', { q: "@jamesfrancotv", count: count }, function(error, data, response) {
if (error) reject(error);
else resolve(data.statuses);
});
});
}
// retweets tweet with tweetID, asynchronously returns response via callback.
function reTweetID(tweetID) {
return Q.Promise(function (fullfill, reject) {
T.post('statuses/retweet/:id', { id: tweetID.toString() }, function(error, data, response) {
if (error) reject(error.allErrors);
else fullfill(data);
});
});
}
function favoriteTweetID(tweetID) {
return Q.Promise(function (fullfill, reject) {
T.post('favorites/create', { id: tweetID.toString() }, function(error, data, response) {
if (error) reject(error.message);
else fullfill(data);
});
});
}
// retweets (count) number of replies to @jamesfrancotv's tweets
function sayHeyToJames(count) {
var favoriteCount = 0;
var retweetCount = 0;
var favoriteErrorCount = 0;
var retweetErrorCount = 0;
// get promise for tweet array of mentions
getMentions(count)
.then(function (tweets) {
// create array of promises to favorite all the tweets
var favorites = tweets.map(function(tweet) {
return favoriteTweetID(tweet.id_str)
.then(function (data) {
// count the successful favorites
favoriteCount ++;
})
.catch(function (reject) {
// count and error log the favorite failures
favoriteErrorCount ++;
console.error('favorite error: ' + reject);
});
});
// create array of promises to retweet all the tweets
var retweets = tweets.map(function(tweet) {
return reTweetID(tweet.id_str)
.then(function (data) {
// count successful retweets
retweetCount ++;
})
.catch(function (reject) {
// count and error log the retweet failures
retweetErrorCount ++;
console.error(Date() + 'retweet error:' + reject);
});
});
// combine both promise arrays
var both = favorites.concat(retweets);
// return a promise for the whole fulfilled array that will wait for all promises to either fulfill or reject
return Q.allSettled(both);
})
.then(function() {
// log final results
var results = '\n' + Date()
+ '\n retweeted: ' + retweetCount
+ '\n favorited: ' + favoriteCount
+ '\n retweet errors: ' + retweetErrorCount
+ '\n favorite errors: ' + favoriteErrorCount;
// mailgun update to mgRecipients
mg.sendText('[email protected]', //sender
mgRecipients, //recipient(s)
"franco update", //subject
results, // text
null,
null,
function(error) { //callback
if (error) {
console.error(Date() + 'mailgun error' + error);
}
});
console.log(results);
});
}
if(require.main == module) {
console.error(Date() + 'Invoked at command line.');
var args = process.argv;
// can take positive numerical argument to say hey to james that many times
if (args.length != 3 || Number.isNaN(args[2]) || args[2] < 1) {
sayHeyToJames(1);
} else {
sayHeyToJames(args[2]);
}
} else {
console.error(Date() + 'Invoked via library call');
}
exports.sayHeyToJames = sayHeyToJames;