-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 1.72 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
const { get } = require('https');
const emojis = {
emojis: require('./data/emoji/collection/big.json'),
cats: require('./data/emoji/cat.json'),
foods: require('./data/emoji/food.json'),
hearts: require('./data/emoji/heart.json'),
circles: require('./data/emoji/circle.json'),
squares: require('./data/emoji/square.json'),
unicode: require('./data/emoji/collection/single.json'),
};
const endpoints = require('./data/endpoints.json');
const { version } = require('./package.json');
const httpOptions = {
method: 'GET',
headers: {
'User-Agent': `Mozilla/5.0 (compatible; random-emoji/${version}; +https://github.com/sefinek24/random-emoji)`,
'Accept': 'application/json',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'DNT': '1',
'Pragma': 'no-cache',
'Referrer-Policy': 'strict-origin-when-cross-origin',
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'X-XSS-Protection': '1; mode=block',
},
};
async function getContent(url) {
const res = await new Promise((resolve, reject) => {
const req = get(url, httpOptions, resolve);
req.on('error', reject);
req.end();
});
if (res.statusCode !== 200) {
throw new Error(`Request failed with status code ${res.statusCode}`);
}
res.setEncoding('utf8');
let rawData = '';
for await (const chunk of res) {
rawData += chunk;
}
return JSON.parse(rawData);
}
class SefinekAPI {
constructor() {
Object.entries(endpoints).forEach(([key, endpoint]) => {
this[key] = () => getContent(`https://api.sefinek.net/api/v2/random/${endpoint}`);
});
}
}
module.exports = {
...Object.fromEntries(Object.entries(emojis).map(([key, value]) => [
key, () => value[Math.floor(Math.random() * value.length)],
])),
Kaomojis: SefinekAPI,
version,
};