-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (159 loc) · 5.68 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const express = require('express');
const got = require('got');
const app = express();
const port = 2050;
app.use('/static', express.static(`${__dirname}/static`));
app.set('views', `${__dirname}/views`);
app.set('view engine', 'ejs');
async function getInfo(user) {
const { body, statusCode } = await got(`https://api.ivr.fi/v2/twitch/user?login=${user}`, {
throwHttpErrors: false,
responseType: 'json',
headers: { 'User-Agent': 'Emote Stats by ZonianMidian' },
});
if (statusCode < 200 || statusCode > 299 || !body.length) return null;
const displayName = body[0].displayName.toLowerCase() === user ? body[0].displayName : user;
return { name: displayName, avatar: body[0].logo, id: body[0].id };
}
function emoteLink(id, extension) {
switch (extension) {
case 'BTTV':
return `https://cdn.betterttv.net/emote/${id}/2x`;
case 'FFZ':
return `https://cdn.frankerfacez.com/emoticon/${id}/2`;
case 'Twitch':
return `https://static-cdn.jtvnw.net/emoticons/v2/${id}/default/dark/2.0`;
}
}
async function parseEmotes(emotes, extension) {
switch (extension) {
case 'BTTV':
case 'FFZ':
case 'Twitch': {
const emoteArray = [];
emotes.map((emote) => {
const emoteData = {
name: emote.emote,
id: emote.id,
link: emoteLink(emote.id, extension),
usage: emote.amount ?? 0,
};
emoteArray.push(emoteData);
});
return emoteArray;
}
case '7TV': {
const emoteArray = [];
emotes.map((emote) => {
const emoteData = {
name: emote.name,
alias: emote.alias,
id: emote.id,
link: `https://cdn.7tv.app/emote/${emote.id}/2x.webp`,
usage: emote.count ?? 0,
};
if (emoteData.usage > 0) emoteArray.push(emoteData);
});
return emoteArray;
}
}
}
async function getChannel(channel) {
const { body: SE_Stats, statusCode: SE_Status } = await got(
`https://api.streamelements.com/kappa/v2/chatstats/${channel}/stats`,
{
throwHttpErrors: false,
responseType: 'json',
headers: { 'User-Agent': 'Emote Stats by ZonianMidian' },
},
);
const SE =
SE_Status < 200 || SE_Status > 299
? null
: {
BTTV: await parseEmotes(SE_Stats.bttvEmotes, 'BTTV'),
FFZ: await parseEmotes(SE_Stats.ffzEmotes, 'FFZ'),
Twitch: await parseEmotes(SE_Stats.twitchEmotes, 'Twitch'),
};
const { body: K_Stats, statusCode: K_Status } = await got(`https://7tv.markzynk.com/c/${channel}`, {
throwHttpErrors: false,
responseType: 'json',
headers: { 'User-Agent': 'Emote Stats by ZonianMidian' },
});
const K = K_Status < 200 || K_Status > 299 ? null : { STV: await parseEmotes(K_Stats.emotes, '7TV') };
if (!SE) return K;
if (!SE && !K) return null;
return Object.assign(SE, K);
}
async function getGlobals() {
const { body: SE_Stats } = await got(`https://api.streamelements.com/kappa/v2/chatstats/global/stats`, {
throwHttpErrors: false,
responseType: 'json',
headers: { 'User-Agent': 'Emote Stats by ZonianMidian' },
});
const { body: K_Stats } = await got(`https://7tv.markzynk.com/top`, {
throwHttpErrors: false,
responseType: 'json',
headers: { 'User-Agent': 'Emote Stats by ZonianMidian' },
});
return {
BTTV: await parseEmotes(SE_Stats.bttvEmotes, 'BTTV'),
FFZ: await parseEmotes(SE_Stats.ffzEmotes, 'FFZ'),
Twitch: await parseEmotes(SE_Stats.twitchEmotes, 'Twitch'),
STV: await parseEmotes(K_Stats.emotes, '7TV'),
};
}
app.get('/', (req, res) => {
res.render('index');
});
app.get('/global', async (req, res) => {
const globalData = await getGlobals();
res.render('global', { data: globalData });
});
app.get('/faq', (req, res) => {
res.render('faq');
});
app.get('/contact', async (req, res) => {
const userInfo = await getInfo('zonianmidian');
res.render('contact', userInfo);
});
app.get('/channel', (req, res) => {
res.redirect('/global');
});
app.get('/c', (req, res) => {
res.redirect('/global');
});
app.get('/channel/:name', (req, res) => {
res.redirect(`/c/${req.params.name}`);
});
app.get('/c/:name', async (req, res) => {
const user = req.params.name.toLowerCase();
if (user === 'global') {
const globalData = await getGlobals();
res.render('global', { data: globalData });
} else {
if (!new RegExp(/^[a-z0-9]\w{0,24}$/i).exec(user))
return res.render('error', { error: 'Invalid username', code: '' });
const userInfo = await getInfo(user);
if (!userInfo) return res.render('error', { error: 'User not found', code: '' });
const channelData = await getChannel(user);
if (!channelData)
return res.render('error', {
error: 'No statistics available',
code: '',
});
res.render('channel', { data: channelData, user: userInfo });
}
});
app.use(function (req, res, next) {
const err = new Error('Not found');
err.status = 404;
next(err);
});
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', { error: err.message, code: `${err.status} - ` });
});
app.listen(port, () => {
console.log(`Statistics website listening on ${port}`);
});