-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (39 loc) · 1.34 KB
/
index.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
'use strict'
require('now-env')
const Twit = require('twit')
const Task = require('data.task')
const bot = new Twit({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
timeout_ms: 60 * 1000
})
module.exports = () => {
const diff = xs => xy => xs.filter(x => !xy.includes(x))
const sleep = ms => new Task((rej, res) => setTimeout(res, ms))
const get = (url, opts = { count: 200 }) =>
new Task((rej, res) =>
bot.get(url, opts, (err, data, response) => (err ? rej(err) : res(data)))
)
const notify = users =>
users.map(user =>
bot.post('direct_messages/new', {
screen_name: process.env.TWITTER_HANDLER,
text: `User @${user} stopped following you!`
})
)
const getFollowers = (all = [], cursor = 0) =>
get('followers/list', { count: 200, cursor }).chain(
({ users, next_cursor_str: nextCursor }) =>
nextCursor && nextCursor === '0'
? Task.of(all.concat(users).map(user => user.screen_name))
: getFollowers(all.concat(users), nextCursor)
)
const main = () =>
Task.of(diff)
.ap(getFollowers())
.ap(sleep(15 * 60 * 1000).chain(getFollowers))
.fork(console.error, notify)
setInterval(main, 15 * 60 * 1000)
}