From 39a1cb25181d78bd55cab73548cbb9b990f0e8cf Mon Sep 17 00:00:00 2001 From: adb Date: Sun, 28 Feb 2021 23:49:20 +0100 Subject: [PATCH] add class mastodonMemeBot --- node_app/main.js | 47 ++++++++++--------------------------- node_app/mastodonHandler.js | 3 ++- node_app/mastodonMemeBot.js | 41 ++++++++++++++++++++++++++++++++ node_app/memeHandler.js | 1 - 4 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 node_app/mastodonMemeBot.js diff --git a/node_app/main.js b/node_app/main.js index b7ef225..6031fb9 100644 --- a/node_app/main.js +++ b/node_app/main.js @@ -1,39 +1,16 @@ -import {MastodonHandler} from "./mastodonHandler.js"; import {MastodonTokenHandler} from "./mastodonTokenHandler.js"; -import {MemeHandler} from "./memeHandler.js"; -import {getStat} from "./JSONdataStore.js"; +import {mastodonMemeBot} from "./mastodonMemeBot.js"; -const baseUrl = "https://social.cybre.town"; -const memeAPI = "http://redditapi.adb.sh/api/v1/"; -const subreddits = ["r/programmerhumor/random"]; -const allowedMedia = ["jpg", "jpeg", "gif", "png"]; -const interval = 1000*60*60; +const config = { + baseUrl: "https://social.cybre.town", + memeAPI: "http://redditapi.adb.sh/api/v1/", + subreddits: ["r/programmerhumor/random"], + allowedMedia: ["jpg", "jpeg", "gif", "png"] +}; -let tokenHandler = new MastodonTokenHandler(baseUrl); -await tokenHandler.getAccessToken((accessToken) => { - let client = new MastodonHandler({ - api_url: `${baseUrl}/api/v1/`, - access_token: accessToken, - }); - let memeHandler = new MemeHandler(memeAPI, subreddits, allowedMedia); - executeAndSetInterval(function postRandomMeme(){ - memeHandler.getRandomMeme(sub => { - let status = `"${sub.title}"\n${sub.text}\nby ${sub.author}`; - if (allowedMedia.find(type => type === sub.url.split(/[.]+/).pop())){ - let filepath = `./media/${sub.url.split(/[/]+/).pop()}`; - if (getStat(filepath)===true){ postRandomMeme(); return; } - memeHandler.downloadMedia(sub.url, filepath, () => { - client.postMedia(status, filepath, 5, () => postRandomMeme()); - }) - }else{ - console.log("no valid media, fetching new sub") - postRandomMeme(); - } - }) - }, interval); +let tokenHandler = new MastodonTokenHandler(config.baseUrl); +tokenHandler.getAccessToken((accessToken) => { + config.accessToken = accessToken; + let bot = new mastodonMemeBot(config); + bot.setFullInterval(60); }); - -function executeAndSetInterval(handler, timout){ - handler(); - setInterval(handler, timout); -} diff --git a/node_app/mastodonHandler.js b/node_app/mastodonHandler.js index 2741696..d8329f7 100644 --- a/node_app/mastodonHandler.js +++ b/node_app/mastodonHandler.js @@ -8,7 +8,7 @@ export class MastodonHandler extends Mastodon{ console.log(resp.data); }); } - postMedia(status, mediaPath, retry, onError){ + postMedia(status, mediaPath, retry, onError, callback){ if (retry <= 0){ onError(); return; @@ -25,6 +25,7 @@ export class MastodonHandler extends Mastodon{ this.post('statuses', { status: status, media_ids: [id] }).then((resp) => { console.log("media posted => "); console.log(resp.data); + callback(); }); }); } diff --git a/node_app/mastodonMemeBot.js b/node_app/mastodonMemeBot.js new file mode 100644 index 0000000..50ac8da --- /dev/null +++ b/node_app/mastodonMemeBot.js @@ -0,0 +1,41 @@ +import {MastodonHandler} from "./mastodonHandler.js"; +import {MemeHandler} from "./memeHandler.js"; +import {getStat} from "./JSONdataStore.js"; + +export class mastodonMemeBot { + constructor(config) { + this.allowedMedia = config.allowedMedia; + this.memeHandler = new MemeHandler(config.memeAPI, config.subreddits, config.allowedMedia); + this.client = new MastodonHandler({ + api_url: `${config.baseUrl}/api/v1/`, + access_token: config.accessToken, + }); + } + postRandomMeme(callback = undefined) { + this.memeHandler.getRandomMeme(sub => { + let status = `"${sub.title}"\n${sub.text}\nby ${sub.author}`; + if (!this.allowedMedia.find(type => type === sub.url.split(/[.]+/).pop())) { + console.log("no valid media, fetching new sub"); + this.postRandomMeme(callback??undefined); + return; + } + let filepath = `./media/${sub.url.split(/[/]+/).pop()}`; + if (getStat(filepath) === true) { + console.log("post already exists, fetching new sub"); + this.postRandomMeme(callback??undefined); + return; + } + this.memeHandler.downloadMedia(sub.url, filepath, () => { + this.client.postMedia(status, filepath, 5, () => this.postRandomMeme(), callback??undefined); + }) + }) + } + setFullInterval(minutes) { + let currentTime = new Date(); + let firstTime = (minutes - currentTime.getMinutes() % minutes)*60 - currentTime.getSeconds(); + setTimeout( () => { + this.postRandomMeme(); + setInterval(() => this.postRandomMeme(), minutes * 60*1000); + }, firstTime * 1000); + } +} \ No newline at end of file diff --git a/node_app/memeHandler.js b/node_app/memeHandler.js index dc2e078..6a39f63 100644 --- a/node_app/memeHandler.js +++ b/node_app/memeHandler.js @@ -35,7 +35,6 @@ export class MemeHandler{ callback(JSON.parse(data)) }) }) - } randomFromArray(array){ return array[Math.floor(Math.random()*array.length)];