forked from HackingTV/DefectorBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
110 lines (92 loc) · 2.83 KB
/
api.js
File metadata and controls
110 lines (92 loc) · 2.83 KB
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
const axios = require('axios')
const dbPromise = require('./db')
const includes = require('array-includes')
const flatten = require('array-flatten')
const myTwitchID = process.env.TWITCH_ID
const subscribeToFollowerHook = async () => {
return axios.post('https://api.twitch.tv/helix/webhooks/hub', {
"hub.mode": "subscribe",
"hub.topic": `https://api.twitch.tv/helix/users/follows?first=1&to_id=${myTwitchID}`,
"hub.callback": "https://www.whentokens.com/callback",
"hub.lease_seconds": 864000
},{
headers: {
'Client-ID': process.env.CLIENT_ID
}
})
}
const getFollowers = (cursor) => {
let params = {
to_id: myTwitchID,
first: 100
}
if (cursor) params.after = cursor
return axios.get('https://api.twitch.tv/helix/users/follows', {
params,
headers: {
'Client-ID': process.env.CLIENT_ID
}
})
.then(res => res.data)
.catch(err => console.log(err))
}
const getUsersFromFollowers = async (cursor) => {
let followers = await getFollowers(cursor)
if (followers.data.length === 0) {
return []
}
let usernames = await getUsernamesForIds(followers.data.map(follower => follower.from_id))
return usernames.concat(await getUsersFromFollowers(followers.pagination.cursor))
}
const getUsernamesForIds = (ids) => {
return axios.get('https://api.twitch.tv/helix/users', {
params: {
id: ids
},
headers: {
'Client-ID': process.env.CLIENT_ID
}
})
.then(res => res.data.data)
.catch(err => console.log(err))
}
const saveUser = async (user) => {
const db = await dbPromise
return await db.exec(`INSERT INTO users VALUES ("${user.id}","${user.username}")`)
}
const saveDefector = async (user) => {
const db = await dbPromise
return await db.exec(`INSERT INTO defectors VALUES ("${user.id}","${user.display_name}", UTC_TIMESTAMP()`)
}
const getFollowersInDatabase = async () => {
const db = await dbPromise
return await db.all('SELECT * FROM users')
}
const getDefectors = async () => {
const db = await dbPromise
// get followers from database
let dbFollowers = await db.all('SELECT id FROM users')
// Get followers from twitch
let twitchFollowers = await getUsersFromFollowers()
twitchFollowers = twitchFollowers.map(follower => follower.id)
// twitchFollowers.splice(0, 2);
// defectors are users in database and who are not in twitch
let unfollowers = dbFollowers
.map(follower => follower.id.toString())
.filter(followerId => !includes(twitchFollowers, followerId))
.map(async id => {
return await getUsernamesForIds(id)
})
return Promise.all(unfollowers)
.then(res => flatten(res))
.then(res => res.map(obj => obj.display_name))
}
module.exports = {
subscribeToFollowerHook,
getFollowers,
getUsersFromFollowers,
getUsernamesForIds,
getFollowersInDatabase,
saveUser,
getDefectors
}