-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.archive.js
131 lines (107 loc) · 4.09 KB
/
index.archive.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
//
// Archive of original geta.dino.icu index.js in case something goes wrong with migration
//
const crypto = require('crypto');
const github = require('download-git-repo');
const express = require('express');
const fs = require('fs');
const home = `Welcome to Get-A-Dino! This website pulls dinosaurs from https://github.com/hackclub/dinosaurs.
GET /dinos - Get the ID and URL to a random dino
GET /dinos/all - Get all dinos
GET /dinos/:id - Serve a dino by its id
GET /info/:id - Get the info of a dino
GET /random - Redirect to a random dino
GET /dino.png - Serve a random dino as a raw PNG`;
function download () {
return new Promise((resolve, reject) => {
github('hackclub/dinosaurs', 'dinos', err => {
if (err) return reject(err);
const files = fs.readdirSync(__dirname + '/dinos').filter(file => {
let isPng = file.endsWith('.png');
return isPng;
}).map(file => 'dinos/' + file);
const dictionary = {};
files.forEach(file => {
dictionary[crypto.createHash('md5').update(file).digest('hex').substring(0, 10)] = file; // Really bad method of generating IDs but at least it is unlikely for duplicates and will be the same each time a dinos.json is generated
});
fs.writeFileSync(__dirname + '/dinos.json', JSON.stringify(dictionary, null, 4), 'utf8');
resolve(dictionary);
});
});
}
function random (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
let dinos = [];
const app = express();
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
app.get('/', (req, res) => {
res.type('text/plain');
res.send(home);
});
app.get('/dino.png', (req, res) => {
res.sendFile(__dirname + '/' + Object.values(dinos)[random(0, Object.values(dinos).length - 1)]);
});
app.get('/dinos', (req, res) => {
const id = Object.keys(dinos)[random(0, Object.keys(dinos).length - 1)];
const name = dinos[id].substring(6, dinos[id].length - 4);
const url = 'https://geta.dino.icu/dinos/' + id;
const info = 'https://geta.dino.icu/info/' + id;
const github = 'https://github.com/hackclub/dinosaurs/blob/main/' + name + '.png';
res.json({
id, name, url, info, github
});
});
app.get('/dinos/all', (req, res) => {
let output = [];
for (const id in dinos) {
const name = dinos[id].substring(6, dinos[id].length - 4);
const url = 'https://geta.dino.icu/dinos/' + id;
const info = 'https://geta.dino.icu/info/' + id;
const github = 'https://github.com/hackclub/dinosaurs/blob/main/' + name + '.png';
output.push({
id, name, url, info, github
});
}
res.json(output);
});
app.get('/dinos/all/images', (req, res) => {
let output = [];
for (const id in dinos) {
const url = 'https://geta.dino.icu/dinos/' + id;
output.push(url);
}
res.json(output);
});
app.get('/dinos/all/object', (req, res) => {
res.json(dinos);
});
app.get('/random', (req, res) => {
const id = Object.keys(dinos)[random(0, Object.keys(dinos).length - 1)];
const url = 'https://geta.dino.icu/dinos/' + id;
res.redirect(url);
});
app.get('/dinos/:id', ({ params: { id } }, res) => {
if (!fs.existsSync(__dirname + '/' + dinos[id])) return res.json({ error: 'Dino not found' });
res.sendFile(__dirname + '/' + dinos[id]);
});
app.get('/info/:id', ({ params: { id } }, res) => {
if (!fs.existsSync(__dirname + '/' + dinos[id])) return res.json({ error: 'Dino not found' });
const name = dinos[id].substring(6, dinos[id].length - 4);
const url = 'https://geta.dino.icu/dinos/' + id;
const info = 'https://geta.dino.icu/info/' + id;
const github = 'https://github.com/hackclub/dinosaurs/blob/main/' + name + '.png';
res.json({
id, name, url, info, github
});
});
download().then(downloaded => {
dinos = downloaded;
console.log('Downloaded. (' + Object.keys(dinos).length + ' dinos)');
app.listen(3000, _ => {
console.log('Ready! (:3000)');
});
});