-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
41 lines (35 loc) · 1.23 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
const bluebird = require("bluebird");
const Twitter = require("twitter");
const moment = require("moment-timezone");
const twitterClient = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
bearer_token: process.env.TWITTER_BEARER_TOKEN,
});
module.exports = {
getWinners(number) {
const date = moment().tz("Europe/London").format("YYYY-MM-DD");
return new bluebird((resolve, reject) => {
twitterClient.get(
"search/tweets",
{ q: `#leedsjs since:${date} -filter:retweets` },
function (error, tweets, response) {
const ignoredUsers = ["@codefoodpixels", "@leedsjs"];
let entrants = tweets.statuses
.map((tweet) => `@${tweet.user.screen_name}`)
.filter((name) => {
return ignoredUsers.indexOf(name.toLowerCase()) === -1;
});
const winners = [];
for (let i = 0; i < number; i++) {
const winner =
entrants[Math.floor(Math.random() * entrants.length)];
winners.push(winner);
entrants = entrants.filter((entrant) => entrant !== winner);
}
resolve(winners);
}
);
});
},
};