From b001e0215c50679fccbac2ee4cd423d47b3d4ad6 Mon Sep 17 00:00:00 2001 From: Win Parker Date: Wed, 17 Jul 2024 17:39:28 +0700 Subject: [PATCH] fixed post apis --- config/config.json | 3 +- config/config.json.example | 3 +- src/api/post.cjs | 63 +++++++++++++++++++++++++------------- src/index.js | 22 +++++++++++++ 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/config/config.json b/config/config.json index 7ad56c9..7779bef 100644 --- a/config/config.json +++ b/config/config.json @@ -4,5 +4,6 @@ "name": "bkkraindarbot", "callback": "http://localhost:4000/callback", "permission": "write:notes,write:following,write:drive" - } + }, + "timezone": "Asia/Bangkok" } \ No newline at end of file diff --git a/config/config.json.example b/config/config.json.example index 9c56f94..86aa2dc 100644 --- a/config/config.json.example +++ b/config/config.json.example @@ -4,5 +4,6 @@ "name": "", "callback": "http://localhost:4000/callback", "permission": "write:notes,write:following,write:drive" - } + }, + "timezone": "Asia/Bangkok" } \ No newline at end of file diff --git a/src/api/post.cjs b/src/api/post.cjs index 693eea2..74a2d1a 100644 --- a/src/api/post.cjs +++ b/src/api/post.cjs @@ -1,31 +1,52 @@ const axios = require('axios'); const fs = require('fs'); -async function create_image(host, access_token, file_path) { - const create = axios.post(`${host}/api/drive/files/create`, { - file: fs.createReadStream(file_path) - }, { - headers: { - 'Content-Type': 'multipart/form-data', - 'Authorization': `Bearer ${access_token}` - } - }); +async function create_image(host, access_token, file_name, file_path) { + try { + const create = await axios.post(`${host}/api/drive/files/create`, { + name: file_name, + file: fs.createReadStream(file_path) + }, { + headers: { + 'Content-Type': 'multipart/form-data', + 'Authorization': `Bearer ${access_token}` + } + }) - return create; -}; + return create.data; + } catch (error) { + if (error.response) { + return error.response.data; + } else if (error.request) { + return error.request; + } else { + return error.message; + } + } +} async function create_note(host, access_token, text, media_ids, visibility) { - const create = axios.post(`${host}/api/notes/create`, { - text: text, - mediaIds: media_ids, - visibility: visibility - }, { - headers: { - 'Authorization': `Bearer ${access_token}` + try { + const create = await axios.post(`${host}/api/notes/create`, { + text: text, + mediaIds: media_ids, + visibility: visibility + }, { + headers: { + 'Authorization': `Bearer ${access_token}` + } + }); + + return create.data; + } catch (error) { + if (error.response) { + return error.response.data; + } else if (error.request) { + return error.request; + } else { + return error.message; } - }); - - return create; + } } module.exports.create_image = create_image; diff --git a/src/index.js b/src/index.js index 6243036..a71d56e 100644 --- a/src/index.js +++ b/src/index.js @@ -24,6 +24,7 @@ const credentials = JSON.parse(fs.readFileSync(credentials_file_path)); let app = express(); let port = process.env.PORT || 4000; +process.env.TZ = config.timezone; let host = config.host; @@ -72,6 +73,27 @@ app.get('/callback', function(req, res) { access_token = credentials.token; } + (async function post() { + let raw_date = new Date(); + /* let curr_image_path = path.join(__dirname, '/data/' + date + '.jpg'); */ + + let static_radar_path = path.join(__dirname, '/data/current_radar.jpg'); + let static_radar_image_name = `${raw_date.toISOString}.jpg`; + console.log(static_radar_image_name) + const upload_radar_image = await Post.create_image(host, access_token, static_radar_image_name, static_radar_path); + + let note_caption = `# Raindar Update\n## ${raw_date.toLocaleString('th-TH')}\nLeft: Current Observation\nRight: 3 hour forecast` + const post_note = await Post.create_note( + host, + access_token, + note_caption, + [upload_radar_image.id], + "public" + ) + + // setInterval(post, 30000); + })(); + /* 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);