-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tpp-getusers.js
85 lines (66 loc) · 2.57 KB
/
tpp-getusers.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
// define configuration options
const fetch = require('node-fetch')
const imghash = require("imghash")
const tmi = require('tmi.js')
const fs = require('fs')
// create a client with our options
const client = new tmi.Client({
identity: { username: "justinfan1986", password: "kappa" },
channels: [ "twitchplayspokemon" ],
})
// throttle queries. we don't want to thrash the servers too much.
const queue = []
let previousName = ""
let timer
function addToQueue(name) {
if (queue.includes(name) || name == previousName || name == "tpp" || name == "tppsimulator") return
else queue.push(name)
if (!timer) timer = setInterval(() => { if (queue.length > 0) getUserData(queue.splice(0, 1)[0]) }, 500)
}
// gather the goods
if (!fs.existsSync("user_avatars")) fs.mkdirSync("user_avatars")
function getUserData(name) {
previousName = name
fetch(`https://api.ivr.fi/v2/twitch/user?login=${name}`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'User-Agent': 'github.com/ravendwyr/tpp-scripts' } })
.then(user => user.json())
.then(user => {
if (!user || user.length != 1) return
// download the user's data
if (user[0].logo.includes("user-default-pictures")) return
// download the user's profile pic
fetch(user[0].logo)
.then(response => response.buffer())
.then(buffer => {
if (!buffer) return
imghash.hash(buffer, 16)
.then(hash => fs.writeFile(`user_avatars/${name}-${hash}.png`, buffer, err => { if (err) throw err }))
.catch(err => printMessage(`error saving avatar for "${name}" -- ${err}`))
})
.catch(err => printMessage(`error fetching avatar for "${name}" -- ${err}`))
})
.catch(err => printMessage(`error fetching data for "${name}" -- ${err}`))
if (queue.length == 0) timer = clearInterval(timer)
}
// our pretty printer
function printMessage(message) {
console.log(new Date().toLocaleTimeString(), message)
}
// event handlers
function onMessageHandler(channel, userdata, message, self) {
addToQueue(userdata.username)
}
function onJoinHandler(channel, name, self) {
addToQueue(name)
}
function onPartHandler(channel, name, self) {
addToQueue(name)
}
function onNamesHandler(channel, names) {
names.forEach((name) => addToQueue(name))
}
// engage
client.on('join', onJoinHandler)
client.on('part', onPartHandler)
client.on('names', onNamesHandler)
client.on('message', onMessageHandler)
client.connect().catch(() => printMessage(`Unable to connect to chat. Please confirm your oauth token is correct.`))