-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
194 lines (176 loc) · 7.08 KB
/
server.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
186
187
188
189
190
191
192
193
194
const express = require("express");
const fs = require("fs");
const path = require("path");
const app = express();
const categories_DONT_USE = ["anime", "real"]; /* USED BY RANDOM ENDPOINT */
const categories = ["anime", "real", "random"];
app.get("/api/img/anime", sendRandomImage("/img/anime/"));
app.get("/api/img/real", sendRandomImage("/img/real/"));
app.get('/api/img/random', (req, res) => {
const randomCategory = categories_DONT_USE[Math.floor(Math.random() * categories_DONT_USE.length)];
const images = getImagesFromCategory(randomCategory);
const randomImage = images[Math.floor(Math.random() * images.length)];
res.sendFile(randomImage);
});
function getImagesFromCategory(category) {
const dirPath = path.join(__dirname, `/img/${category}`);
const files = fs.readdirSync(dirPath);
return files.map(file => path.join(dirPath, file));
}
app.get("/", (req, res) => {
res.send(`
<html>
<!-- If you're on mobile, then it looks like shit. Simple fix: get a computer. -->
<head>
<title>Penguin API</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap" rel="stylesheet">
<style>
.background {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: url(/api/img/random) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
filter: blur(8px);
-webkit-filter: blur(8px);
z-index: -1;
}
.title {
position: absolute;
top: 10%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 5em;
font-weight: bold;
color: white;
text-align: center;
font-family: 'Roboto', sans-serif;
text-shadow: 2px 2px 20px rgba(0, 0, 0, 1);
}
.instructions {
position: absolute;
top: 75%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 2em;
color: white;
text-align: center;
font-family: 'Roboto', sans-serif;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 20px;
padding: 20px;
}
.disclaimer { /* not trying to get sued lol */
position: absolute;
top: 97%;
left: 21%; /* why 21%? idk */
transform: translate(-50%, -50%);s
color: white;
font-size: 0.9em;
font-family: 'Roboto', sans-serif;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 20px;
padding: 5px;
}
.highlight {
color: #b3d8ff; /* Love this blue */
}
.highlight a {
color: inherit;
text-decoration: none;
}
#overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 9999;
display: none;
}
#overlay.show {
display: block;
pointer-events: auto;
}
#mobile-popup {
z-index: 10000;
font-size: 4em;
}
#continue-button {
font-size: 2em;
}
</style>
</head>
<body>
<div id="overlay" style="display: none;">
<div class="background"></div>
<div class="title">Penguin API</div>
<div class="instructions">
How to use: <br><br>
http://127.0.0.1:5292/api/img/<span class="highlight">category</span>/
<br><br>
Categories:
<div id="category-list">
${categories.map(category => `<span class="highlight"><a href="http://127.0.0.1:5292/api/img/${category}/">${category}</a></span>`).join(', ')}
</div>
</div>
<div class="disclaimer">
*all these images are licensed under the <span class="highlight"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a></span>
</div>
</div>
<div id="mobile-popup" style="display: none;">
Whoa, this isn't meant for mobile, but you can try to load it anyway. If you're on a computer, try making the window larger. Once clicking this, it won't show up again for 5 minutes.
<button id="continue-button">Continue to page</button>
</div>
</body>
<script>
window.onload = function() {
var overlay = document.getElementById('overlay');
var mobilePopup = document.getElementById('mobile-popup');
var continueButton = document.getElementById('continue-button');
var lastClickTime = localStorage.getItem('lastClickTime');
console.log(Date.now() - lastClickTime + " which is " + (Date.now() - lastClickTime)/1000 + " seconds or " + (Date.now() - lastClickTime)/60000 + " minutes.");
if (window.innerWidth <= 1000) {
if (!lastClickTime || Date.now() - lastClickTime > 300000) {
overlay.classList.add('show');
mobilePopup.style.display = 'block';
}else{
overlay.classList.remove('show');
overlay.style.display = 'block';
}
}else{
overlay.classList.remove('show');
overlay.style.display = 'block';
}
continueButton.onclick = function() {
overlay.classList.remove('show');
overlay.style.display = 'block';
mobilePopup.style.display = 'none';
localStorage.setItem('lastClickTime', Date.now());
};
};
</script>
</html>
`);
});
function sendRandomImage(dir) {
return (req, res) => {
const dirPath = path.join(__dirname, dir);
fs.readdir(dirPath, (err, files) => {
if (err) {
res.status(500).send("Server error");
return;
}
const file = files[Math.floor(Math.random() * files.length)];
res.sendFile(path.join(dirPath, file));
});
};
}
const port = process.env.PORT || 5292;
app.listen(port, () => console.log(`Server running on port ${port}`));