From c420610541fa60520cdab643d7b4cd507f194cfb Mon Sep 17 00:00:00 2001 From: Win Date: Fri, 12 Jul 2024 13:25:47 +0700 Subject: [PATCH] confusion --- src/api/fetch.cjs | 27 +++++++++++++++++++++++++++ src/api/post.cjs | 10 +++------- src/index.js | 38 ++++++++++++++++++++++++++------------ 3 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 src/api/fetch.cjs diff --git a/src/api/fetch.cjs b/src/api/fetch.cjs new file mode 100644 index 0000000..40df321 --- /dev/null +++ b/src/api/fetch.cjs @@ -0,0 +1,27 @@ +async function get_radar_image() { + try { + let radar_src = "https://weather.bangkok.go.th/Images/Radar/radarh.jpg" + const radar_image = await axios.get(radar_src, { + responseType: "stream" + }); + + const radar_image_size = radar_image.headers['content-length']; + console.log("Got Radar Data"); + return [radar_image, radar_image_size]; + } catch (error) { + console.error(error); + return [error]; + } +} + +async function get_radar_caption() { + const response = await axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx'); + + let root = parse(response.data); + console.log("Got Radar Caption") + return root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText; +} + + +module.exports.get_radar_image = get_radar_image; +module.exports.get_radar_caption = get_radar_caption; \ No newline at end of file diff --git a/src/api/post.cjs b/src/api/post.cjs index 06b18bc..693eea2 100644 --- a/src/api/post.cjs +++ b/src/api/post.cjs @@ -11,13 +11,11 @@ async function create_image(host, access_token, file_path) { } }); - if(create.status == 200) - return create; }; async function create_note(host, access_token, text, media_ids, visibility) { - axios.post(`${host}/api/notes/create`, { + const create = axios.post(`${host}/api/notes/create`, { text: text, mediaIds: media_ids, visibility: visibility @@ -25,11 +23,9 @@ async function create_note(host, access_token, text, media_ids, visibility) { headers: { 'Authorization': `Bearer ${access_token}` } - }).then((response) => { - let data = response.data; - - return data; }); + + return create; } module.exports.create_image = create_image; diff --git a/src/index.js b/src/index.js index dfeb43f..574caf8 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import fs from 'node:fs'; import path from 'node:path'; import express from 'express'; +import { pipeline } from 'node:stream/promises'; import * as Post from './api/post.cjs'; import * as Auth from './api/auth.cjs'; @@ -51,19 +52,32 @@ app.get('/callback', function(req, res) { access_token = token_file.token; } - let static_radar_path = path.join(__dirname, '/data/current_radar.jpg'); - const static_radar_image = await Post.create_image(host, access_token, static_radar_path); - const static_radar_image__id = static_radar_image.id; - console.log(static_radar_image); + /* setInterval(async function post() { + let static_radar_path = path.join(__dirname, '/data/current_radar.jpg'); + const static_radar_image = await Post.create_image(host, access_token, static_radar_path); + const static_radar_image__id = static_radar_image.data.id; + console.log(static_radar_image__id); - /* let note_text = "hello fediverse"; - const post_note = await Post.create_note( - host, - access_token, - note_text, - ["" + static_radar_image__id], - "public" - ) */ + return ; + }, 60000); */ + + /* setInterval(async () => { + let date = new Date().toLocaleString('th-TH'); + /* let curr_image_path = path.join(__dirname, '/data/' + date + '.jpg'); + + let static_radar_path = path.join(__dirname, '/data/current_radar.jpg'); + const static_radar_image = Post.create_image(host, access_token, name, static_radar_path); + const static_radar_image__id = static_radar_image.data.id; + + let note_caption = `# Raindar Update\n## ${date}\n${radar_caption}\n\nLeft: Current Observation\nRight: 3 hour forecast` + const post_note = Post.create_note( + host, + access_token, + note_text, + [static_radar_image__id], + "public" + ) + }, 60000); */ })(); app.listen(port);