fixed post apis

This commit is contained in:
Win 2024-07-17 17:39:28 +07:00
parent 41ff6f3196
commit b001e0215c
4 changed files with 68 additions and 23 deletions

View File

@ -4,5 +4,6 @@
"name": "bkkraindarbot",
"callback": "http://localhost:4000/callback",
"permission": "write:notes,write:following,write:drive"
}
},
"timezone": "Asia/Bangkok"
}

View File

@ -4,5 +4,6 @@
"name": "<app name>",
"callback": "http://localhost:4000/callback",
"permission": "write:notes,write:following,write:drive"
}
},
"timezone": "Asia/Bangkok"
}

View File

@ -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;

View File

@ -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);