added possibility to download latest images

This commit is contained in:
Quentin Roussel
2024-08-18 00:57:29 +08:00
commit ab1d1669a6
6 changed files with 106 additions and 0 deletions

20
Dockerfile Normal file
View File

@@ -0,0 +1,20 @@
FROM node:18
# Set TZ to Singapore to help with syncronizing with singapore's website time
ENV TZ="Asia/Singapore"
RUN apt-get update && apt-get install -y ffmpeg && apt-get clean
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
CMD [ "npm", "start" ]

12
Makefile Normal file
View File

@@ -0,0 +1,12 @@
APP_NAME = "sg_rainradar_video"
build:
docker build . -t $(APP_NAME)
debug:build
docker run \
-it \
--mount type=bind,source=$(PWD)/images,target=/home/node/app/images \
$(APP_NAME)
.PHONY: build debug run

55
generate.mjs Normal file
View File

@@ -0,0 +1,55 @@
import fs from 'fs';
import client from 'https';
function getTimeString(count) {
let date = new Date(Date.now() - count * 60000 * 5);
return (
date.getFullYear().toString()
+ (date.getMonth() + 1).toString().padStart(2, "0")
+ date.getDate().toString().padStart(2, "0")
+ date.getHours().toString().padStart(2, "0")
+ (date.getMinutes() - date.getMinutes() % 5).toString().padStart(2, "0")
);
}
function downloadImage(time_string, filepath) {
return new Promise((resolve, reject) => {
let options = {
hostname: 'www.nea.gov.sg',
port: 443,
path: `/docs/default-source/rain-area/dpsri_70km_${time_string}0000dBR.dpsri.png`,
headers: {
'Host': 'www.nea.gov.sg',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0',
'Accept': 'image/avif,image/webp,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5',
'Accept-Language': 'en-US,en;q=0.5',
}
}
client.get(options, (res) => {
console.log(`Downloading ${time_string}`);
if (res.statusCode === 200) {
res.pipe(fs.createWriteStream(filepath))
.on('error', reject)
.once('close', () => resolve(filepath));
} else {
// Consume response data to free up memory
res.resume();
reject(new Error(`Request Failed With a Status Code: ${res.statusCode}`));
}
});
});
}
export async function initImages(count) {
for (let i = count; i >= 0; i--) {
let time_string = getTimeString(i);
let filename = `images/${time_string}.png`;
try {
await downloadImage(time_string, filename)
} catch (error) {
//Response 302, we've reached the latest available image
return;
}
}
}

1
images/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.png

6
index.mjs Normal file
View File

@@ -0,0 +1,6 @@
import { initImages } from './generate.mjs';
initImages(10).then(() => {
console.log('Downloaded images successfully');
});

12
package.json Normal file
View File

@@ -0,0 +1,12 @@
{
"name": "sg_rain_video",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.mjs"
},
"author": "",
"license": "ISC"
}