diff --git a/.gitignore b/.gitignore index e69de29..251198f 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +./accessToken.json* \ No newline at end of file diff --git a/JSONdataStore.js b/JSONdataStore.js new file mode 100644 index 0000000..138172f --- /dev/null +++ b/JSONdataStore.js @@ -0,0 +1,17 @@ +import fs from "fs"; + +export function storeData(data, path) { + try { + fs.writeFileSync(path, JSON.stringify(data)); + } catch (err) { + console.error(err); + } +} +export function loadData(path) { + try { + return JSON.parse(fs.readFileSync(path, 'utf8')) + } catch (err) { + console.error(err) + return false + } +} \ No newline at end of file diff --git a/main.js b/main.js index e69de29..dd754d3 100644 --- a/main.js +++ b/main.js @@ -0,0 +1,9 @@ +import {MastodonHandler} from "./mastodonHandler.js"; +import {MastodonTokenHandler} from "./mastodonTokenHandler.js" + +const baseUrl = "https://social.cybre.town"; + +let tokenHandler = new MastodonTokenHandler(baseUrl); +tokenHandler.getAccessToken((accessToken) => { + let client = new MastodonHandler(baseUrl, accessToken); +}); diff --git a/mastodonHandler.js b/mastodonHandler.js new file mode 100644 index 0000000..355256d --- /dev/null +++ b/mastodonHandler.js @@ -0,0 +1,10 @@ +import Mastodon from "mastodon-api"; + +export class MastodonHandler{ + constructor(baseUrl, token) { + this.client = new Mastodon({ + api_url: `${baseUrl}/api/v1/`, + access_token: token, + }) + } +} \ No newline at end of file diff --git a/mastodonTokenHandler.js b/mastodonTokenHandler.js new file mode 100644 index 0000000..23fafb7 --- /dev/null +++ b/mastodonTokenHandler.js @@ -0,0 +1,53 @@ +import Mastodon from 'mastodon-api'; +import readline from 'readline'; +import {storeData, loadData} from './JSONdataStore.js'; + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) + +export class MastodonTokenHandler { + constructor(baseUrl) { + this.baseUrl = baseUrl; + this.clientId; + this.clientSecret; + this.accessToken = loadData("./accessToken.json"); + } + getAccessToken(_callback){ + if (!this.accessToken) this.newAccessToken(_callback); + else _callback(this.accessToken); + return this.accessToken; + } + newAccessToken(_callback){ + Mastodon.createOAuthApp(`${this.baseUrl}/api/v1/apps`) + .catch((err) => console.error(err)) + .then((res) => { + console.log('Please save \'id\', \'client_id\' and \'client_secret\' in your program and use it from now on!') + console.log(res) + + this.clientId = res.client_id + this.clientSecret = res.client_secret + + return Mastodon.getAuthorizationUrl(this.clientId, this.clientSecret, this.baseUrl) + }) + .then((url) => { + console.log('This is the authorization URL. Open it in your browser and authorize with your account!') + console.log(url) + return new Promise((resolve) => { + rl.question('Please enter the code from the website: ', (code) => { + resolve(code) + rl.close() + }) + }) + }) + .then((code) => Mastodon.getAccessToken(this.clientId, this.clientSecret, code, this.baseUrl)) + .catch((err) => console.error(err)) + .then((token) => { + this.accessToken = token; + console.log(`This is the access token. Save it!\n${this.accessToken}`) + storeData(this.accessToken, "./accessToken.json"); + _callback(this.accessToken); + }) + } +} \ No newline at end of file diff --git a/package.json b/package.json index 175298e..3a9be0e 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,15 @@ "name": "mastodon_meme_bot", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "main.js", + "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", - "license": "ISC" + "license": "ISC", + "dependencies": { + "mastodon-api": "^1.3.0" + } }