67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
import { v4 as uuidv4 } from 'uuid';
|
|
import fs, { createReadStream } from 'node:fs';
|
|
import path from 'node:path';
|
|
import express from 'express';
|
|
import axios from 'axios';
|
|
import { fileFromPath } from 'formdata-node/file-from-path';
|
|
import * as Misskey from 'misskey-js';
|
|
|
|
import * as Post from './api/post.cjs';
|
|
import * as Auth from './api/auth.cjs';
|
|
|
|
const __dirname = path.resolve();
|
|
const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json")));
|
|
const token_file = JSON.parse(fs.readFileSync(path.resolve("config", "token.json")));
|
|
|
|
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) {
|
|
res.send({
|
|
"status": 200,
|
|
"success": true,
|
|
})
|
|
|
|
Auth.verify_auth_status();
|
|
});
|
|
|
|
(async () => {
|
|
let access_token = false;
|
|
|
|
if(!token_file.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(
|
|
path.resolve("config", "token.json"),
|
|
JSON.stringify(json_data)
|
|
);
|
|
} else {
|
|
access_token = token_file.token;
|
|
}
|
|
|
|
const cli = new Misskey.api.APIClient({
|
|
origin: host,
|
|
credential: access_token
|
|
});
|
|
|
|
let static_image_path = path.join(__dirname, '/data/current_radar.jpg');
|
|
Post.create_image(host, access_token, static_image_path);
|
|
})();
|
|
|
|
app.listen(port);
|
|
console.log(`Callback URL: http://localhost:${port}/callback`); |