-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.tsx
More file actions
369 lines (320 loc) · 8.78 KB
/
Copy pathserver.tsx
File metadata and controls
369 lines (320 loc) · 8.78 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import {bundle} from '@remotion/bundler';
import {
getCompositions,
renderFrames,
stitchFramesToVideo,
} from '@remotion/renderer';
import express, {Request, Response} from 'express';
import fs from 'fs';
import path from 'path';
import axios from 'axios';
import moment from 'moment';
import {JSDOM} from 'jsdom';
import {CronJob} from 'cron';
import MarkdownIt from 'markdown-it';
import dotenv from 'dotenv';
dotenv.config();
const clc = require('cli-color');
const md = new MarkdownIt();
const app = express();
const port = process.env.PORT || 8080;
const compositionId = 'Food';
export const info = clc.bold.blue('[INFO] ');
export const success = clc.bold.green('[SUCCESS] ');
export const warning = clc.bold.yellow('[WARNING] ');
export const error = clc.bold.red('[ERROR] ');
const url = 'https://www.mayk.fi/tietoa-meista/ruokailu/';
interface IFood {
date: string[];
normal: string[];
vege: string[];
}
let busy = false;
const fetchData = () =>
new Promise<IFood | undefined>((resolve) => {
const start = Date.now();
console.info(info + `Fetching data...`);
axios(url)
.then((response) => {
const dom = new JSDOM(response.data);
const ruokaMenu = Array.from(
dom.window.document.querySelectorAll('.ruoka-template-header')
) as HTMLElement[];
const food: IFood = {
date: [],
normal: [],
vege: [],
};
ruokaMenu.map((element) => {
const ruokaPvm =
element.querySelector('.ruoka-header-pvm')?.textContent;
const ruoka = element.querySelector(
'.ruoka-header-ruoka'
)?.textContent;
const kasvisruoka = element.querySelector(
'.ruoka-header-kasvisruoka'
)?.textContent;
if (ruokaPvm && ruoka && kasvisruoka) {
food.date.push(ruokaPvm.replace(/\s+/g, '')); // ruokaPvm.replace(/\s+/g, '');
food.normal.push(ruoka); // ruoka
food.vege.push(kasvisruoka.replace(/ {2}Kasvisruoka /g, '')); // kasvisruoka.replace(/ {2}Kasvisruoka/g, '')
}
});
console.log(
info + `Fetched data in`,
Date.now() - start,
`ms at ${moment().format('MMMM Do YYYY, h:mm:ss a')}`
);
resolve(food);
})
.catch((err) => {
console.warn('Failed to fetch', err);
resolve(undefined);
});
});
const getTodayDateNum = () => {
if (moment().locale('fi').isoWeekday() - 1 >= 5) {
return 4;
} else {
return moment().locale('fi').isoWeekday() - 1;
}
};
const createVideo = async (inputProps?: object | undefined) => {
const tmpDir = path.resolve(process.env.VIDEO_DIR || 'videos');
await fs.promises.mkdir(tmpDir, {recursive: true});
const key = moment(new Date()).format('YYYYMMDD') + '.mp4';
const finalOutput = path.join(tmpDir, 'ruoka' + key);
const start = Date.now();
if (fs.existsSync(finalOutput)) {
return finalOutput;
} else {
const bundled = await bundle(path.join(__dirname, './src/index.tsx'));
const comps = await getCompositions(bundled, {inputProps});
const video = comps.find((c) => c.id === compositionId);
if (!video) {
throw new Error(`No video called ${compositionId}`);
}
const {assetsInfo} = await renderFrames({
config: video,
timeoutInMilliseconds: 100000,
webpackBundle: bundled,
onStart: () => {
console.log(info + 'Rendering frames...');
},
onFrameUpdate: (f) => {
if (f % 10 === 0) {
process.stdout.write(info + `Rendered frame ${clc.bold(f)}...\r`);
}
},
parallelism: null,
outputDir: tmpDir,
inputProps,
compositionId,
imageFormat: 'jpeg',
envVariables: {
SHA512_KEY:
'sha512-KUoB3bZ1XRBYj1QcH4BHCQjurAZnCO3WdrswyLDtp7BMwCw7dPZngSLqILf68SGgvnWHTD5pPaYrXi6wiRJ65g==',
API_URL: 'http://localhost:8080',
},
});
await stitchFramesToVideo({
dir: tmpDir,
force: true,
fps: video.fps,
height: video.height,
width: video.width,
outputLocation: finalOutput,
imageFormat: 'jpeg',
assetsInfo,
});
fs.readdirSync('./videos').map((file) => {
if (file.endsWith('.jpeg')) {
fs.unlinkSync('./videos/' + file);
}
if (file.startsWith('ruoka') && !file.endsWith(key)) {
fs.unlinkSync('./videos/' + file);
}
});
}
console.log(success + 'Created video in', Date.now() - start, 'ms');
return finalOutput;
};
const cacheVideo = async (req: Request, res: Response) => {
const start = Date.now();
const sendFile = (file: string) => {
fs.createReadStream(file)
.pipe(res)
.on('close', () => {
res.end();
});
};
try {
res.set('content-type', 'video/mp4');
if (busy) {
console.info(info + `The video is already being rendered!`);
} else {
console.info(info + `Creating & sending the video...`);
const finalOutput = await createVideo();
sendFile(finalOutput);
console.log(success + 'Sent video in', Date.now() - start, 'ms');
}
} catch (err) {
console.log(error + 'An error has occured...');
console.error(err);
res.json({
error: err,
});
}
};
const initMain = async () => {
food = await fetchData();
fs.writeFileSync(
`./videos/${moment(new Date()).format('YYYYMMDD')}.json`,
JSON.stringify(food)
);
console.info(info + `Creating & sending the video...`);
await createVideo();
console.info(success + 'Sent video!');
const continent = process.env.CRON_CONT || 'Europe';
const city = process.env.CRON_CITY || 'Helsinki';
const job = new CronJob(
'0 7 * * *',
async () => {
await fetchData();
if (busy) {
console.info(info + `The video is already being rendered!`);
} else {
console.info(info + `Creating & sending the video...`);
await createVideo();
console.info(success + 'Sent video!');
}
},
null,
true,
`${continent + '/' + city}`
);
job.start();
};
let food: IFood | undefined = undefined;
app.get('/', (req, res) => {
const file = fs.readFileSync('./README.md');
const head = `<title>API Documentation</title>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.1.0/github-markdown.min.css"
integrity="${process.env.SHA512_KEY}"
crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
@font-face {
font-family: 'Muli';
src: url('https://fonts.gstatic.com/s/muli/v26/7Aulp_0qiz-aVz7u3PJLcUMYOFnOkEk30eg.woff2')
format('woff2');
font-style: normal;
font-weight: 400;
unicode-range: U+0-FF, U+131, U+152-153, U+2BB-2BC, U+2C6, U+2DA, U+2DC,
U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF,
U+FFFD;
}
@font-face {
font-family: 'Muli';
src: url('https://fonts.gstatic.com/s/muli/v26/7Aulp_0qiz-aVz7u3PJLcUMYOFkpl0k30eg.woff2')
format('woff2');
font-style: normal;
font-weight: 700;
unicode-range: U+0-FF, U+131, U+152-153, U+2BB-2BC, U+2C6, U+2DA, U+2DC,
U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF,
U+FFFD;
}
@media (prefers-color-scheme: dark) {
body {
background: #0d1117;
}
}
body {
font-family: "Muli", -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";
}
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
font-family: "Muli";
}
@media (max-width: 800px) {
.markdown-body {
padding: 15px;
}
}
</style>`;
res.send(
head +
`<article class="markdown-body">
${md.render(file.toString())}
</article>`
);
});
app.get('/api', async (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
if (!food) {
food = await fetchData();
}
if (!food) {
return res.status(500).send({});
}
fs.writeFileSync(
`./videos/${moment(new Date()).format('YYYYMMDD')}.json`,
JSON.stringify(food)
);
fs.readdirSync('./videos').map(async (file) => {
if (
file.endsWith('.json') &&
file.replace('.json', '') !== moment(new Date()).format('YYYYMMDD')
) {
food = await fetchData();
fs.writeFileSync(
`./videos/${moment(new Date()).format('YYYYMMDD')}.json`,
JSON.stringify(food)
);
if (fs.existsSync('./videos/' + file)) {
fs.unlinkSync('./videos/' + file);
}
}
});
res.send({
status_code: res.statusCode,
status_message: res.statusMessage || null,
time_now: Date.now(),
url,
data: {
menu: {
food,
},
menu_today: {
food: {
num_date: getTodayDateNum(),
today_date_full: moment()
.locale('fi')
.format('MMMM Do YYYY, h:mm:ss a'),
today_date: moment().locale('fi').format('l'),
current_week: moment().locale('fi').format('w'),
date: food.date[getTodayDateNum()] || null,
normal: food.normal[getTodayDateNum()] || null,
vege: food.vege[getTodayDateNum()] || null,
},
},
},
});
});
app.get('/video', async (req, res) => {
cacheVideo(req, res);
});
if (require.main === module) {
app.listen(port, async () => {
await initMain();
});
}
console.info(success + `Server is running on http://localhost:${port}.`);
module.exports = {
app,
initMain,
};