diff --git a/config/config.json b/config/config.json index 688727e..7ad56c9 100644 --- a/config/config.json +++ b/config/config.json @@ -3,6 +3,6 @@ "app": { "name": "bkkraindarbot", "callback": "http://localhost:4000/callback", - "permission": "write:notes,write:following,read:drive" + "permission": "write:notes,write:following,write:drive" } } \ No newline at end of file diff --git a/data/current_radar.jpg b/data/current_radar.jpg new file mode 100644 index 0000000..4ba0eb0 Binary files /dev/null and b/data/current_radar.jpg differ diff --git a/package-lock.json b/package-lock.json index a5adaf0..5bf65e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,9 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "axios": "^1.7.2", "express": "^4.19.2", + "formdata-node": "^6.0.3", "misskey-js": "^2024.5.0", "node-fetch": "^3.3.2", "uuid": "^10.0.0" @@ -34,6 +36,23 @@ "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", "license": "MIT" }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" + }, + "node_modules/axios": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz", + "integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/body-parser": { "version": "1.20.2", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", @@ -86,6 +105,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -157,6 +188,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -316,6 +356,49 @@ "node": ">= 0.8" } }, + "node_modules/follow-redirects": { + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/formdata-node": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/formdata-node/-/formdata-node-6.0.3.tgz", + "integrity": "sha512-8e1++BCiTzUno9v5IZ2J6bv4RU+3UKDmqWUQD0MIMVCd9AdhWkO1gw57oo1mNEX1dMq2EGI+FbWz4B92pscSQg==", + "license": "MIT", + "engines": { + "node": ">= 18" + } + }, "node_modules/formdata-polyfill": { "version": "4.0.10", "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", @@ -648,6 +731,12 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", diff --git a/package.json b/package.json index 6dcf90b..25d3c1f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,9 @@ "license": "ISC", "type": "module", "dependencies": { + "axios": "^1.7.2", "express": "^4.19.2", + "formdata-node": "^6.0.3", "misskey-js": "^2024.5.0", "node-fetch": "^3.3.2", "uuid": "^10.0.0" diff --git a/src/index.js b/src/index.js index a894b9f..9ef17df 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,19 @@ import { v4 as uuidv4 } from 'uuid'; -import fs from 'fs'; +import fs, { createReadStream } from 'node:fs'; import path from 'node:path'; import fetch from 'node-fetch'; import express from 'express'; +import axios from 'axios'; +import { fileFromPath } from 'formdata-node/file-from-path'; import * as Misskey from 'misskey-js'; +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; -const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json"))); let host = config.host; let app_name = config.app.name; @@ -60,14 +65,30 @@ async function get_access_token(host, session_id) { if(data.ok == true) { return data.token; } else { - return false; // error occured + return false; // error occurred } }; +async function create_image(access_token, file_path) { + axios.post(`${host}/api/drive/files/create`, { + file: fs.createReadStream(file_path) + }, { + headers: { + 'Content-Type': 'multipart/form-data', + 'Authorization': `Bearer ${access_token}` + } + }).then((data) => { + return data; + }); + + // return info: + // https://misskey.winscloud.net/api-doc#tag/drive/operation/drive___files___create +}; + (async () => { - let access_token = 0; - const token_file = JSON.parse(fs.readFileSync(path.resolve("config", "token.json"))); - if(!token_file.token || token_file.token == "" || token_file.token == false) { // if existing token not exist + let access_token = false; + + if(!token_file.token) { // if existing token not exist await check_auth_status() access_token = await get_access_token(host, session_id); @@ -88,10 +109,8 @@ async function get_access_token(host, session_id) { credential: access_token }); - const meta = await cli.request('notes/create', { - text: "hi from misskey api!" - }); - console.log(meta); + let static_image_path = path.join(__dirname, '/data/current_radar.jpg'); + create_image(access_token, static_image_path); })(); app.listen(port);