104 lines
3.1 KiB
JavaScript
104 lines
3.1 KiB
JavaScript
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';
|
|
|
|
const __dirname = path.resolve();
|
|
const credentials_file_path = path.join(__dirname, '/config/credentials.json');
|
|
|
|
if(!fs.existsSync(credentials_file_path)) {
|
|
fs.writeFileSync(
|
|
credentials_file_path,
|
|
JSON.stringify({
|
|
"token": ""
|
|
})
|
|
);
|
|
}
|
|
|
|
const config = JSON.parse(fs.readFileSync(path.join(__dirname, '/config/config.json')));
|
|
const credentials = JSON.parse(fs.readFileSync(credentials_file_path));
|
|
|
|
let app = express();
|
|
let port = process.env.PORT || 4000;
|
|
|
|
let host = config.host;
|
|
|
|
let app_name = config.app.name;
|
|
let app_callback = config.app.callback;
|
|
let app_permission = config.app.permission;
|
|
|
|
let session_id = uuidv4();
|
|
let auth_url = `${host}/miauth/${session_id}?name=${app_name}&callback=${app_callback}&permission=${app_permission}`;
|
|
|
|
app.get('/callback', function(req, res) {
|
|
if(req.query.session) {
|
|
res.send({
|
|
"status": 200,
|
|
"success": true,
|
|
})
|
|
|
|
Auth.verify_auth_status();
|
|
} else {
|
|
res.send({
|
|
"status": 400,
|
|
"success": false,
|
|
"message": "Invalid callback request"
|
|
})
|
|
}
|
|
|
|
});
|
|
|
|
(async () => {
|
|
let access_token = null;
|
|
|
|
if(!credentials.token) { // if existing token not exist
|
|
console.log(auth_url);
|
|
await Auth.check_auth_status();
|
|
access_token = await Auth.get_access_token(host, session_id);
|
|
|
|
let json_data = {
|
|
"token": access_token
|
|
}
|
|
|
|
fs.writeFileSync(
|
|
credentials_file_path,
|
|
JSON.stringify(json_data)
|
|
);
|
|
} else {
|
|
access_token = credentials.token;
|
|
}
|
|
|
|
/* 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);
|
|
|
|
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);
|
|
console.log(`Callback URL: http://localhost:${port}/callback`); |