fetching images

This commit is contained in:
Win 2024-07-18 14:21:07 +07:00
parent 6cf9b5cbbd
commit 6cb3098c06
2 changed files with 29 additions and 2 deletions

View File

@ -14,6 +14,20 @@ async function get_radar_image() {
} }
} }
async function get_image(url) {
try {
const image = await axios.get(url, {
responseType: "stream"
});
const image_size = image.headers['content-length'];
console.log("Got Radar Data");
return [image, image_size];
} catch (error) {
}
}
async function get_radar_caption() { async function get_radar_caption() {
const response = await axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx'); const response = await axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx');

View File

@ -6,6 +6,7 @@ import { pipeline } from 'node:stream/promises';
import * as Post from './api/post.cjs'; import * as Post from './api/post.cjs';
import * as Auth from './api/auth.cjs'; import * as Auth from './api/auth.cjs';
import * as Fetch from './api/fetch.cjs';
const __dirname = path.resolve(); const __dirname = path.resolve();
const credentials_file_path = path.join(__dirname, '/config/credentials.json'); const credentials_file_path = path.join(__dirname, '/config/credentials.json');
@ -75,9 +76,21 @@ app.get('/callback', function(req, res) {
(async function post() { (async function post() {
let raw_date = new Date(); let raw_date = new Date();
/* let curr_image_path = path.join(__dirname, '/data/' + date + '.jpg'); */ let static_radar_path = path.join(__dirname, '/data/static_radar.jpg');
let animated_radar_path = path.join(__dirname, '/data/animated_radar.gif');
let fetch_static_image = await Fetch.get_image("https://weather.bangkok.go.th/Images/Radar/radarh.jpg");
await pipeline(
fetch_static_image[0],
fs.createWriteStream(static_radar_path)
);
let fetch_animated_image = await Fetch.get_image("https://dds.bangkok.go.th/Radar/radar_rain.gif");
await pipeline(
fetch_animated_image[0],
fs.createWriteStream(animated_radar_path)
);
let static_radar_path = path.join(__dirname, '/data/current_radar.jpg');
let static_radar_image_name = `${raw_date.toISOString()}.jpg`; let static_radar_image_name = `${raw_date.toISOString()}.jpg`;
const upload_radar_image = await Post.create_image(host, access_token, static_radar_image_name, static_radar_path); const upload_radar_image = await Post.create_image(host, access_token, static_radar_image_name, static_radar_path);