From 50d1dd7e17a8d77b3cd20e32cb3f99dc8eb374bb Mon Sep 17 00:00:00 2001 From: adb Date: Sun, 21 Feb 2021 14:56:41 +0100 Subject: [PATCH] mastodonHandler extends Mastodon --- node_app/main.js | 14 +++++++++++--- node_app/mastodonHandler.js | 18 ++++++++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/node_app/main.js b/node_app/main.js index c713624..9055b59 100644 --- a/node_app/main.js +++ b/node_app/main.js @@ -6,13 +6,16 @@ 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; +const interval = 1000*60*60; let tokenHandler = new MastodonTokenHandler(baseUrl); tokenHandler.getAccessToken((accessToken) => { - let client = new MastodonHandler(baseUrl, accessToken); + let client = new MastodonHandler({ + api_url: `${baseUrl}/api/v1/`, + access_token: accessToken, + }); let memeHandler = new MemeHandler(memeAPI, subreddits, allowedMedia); - setInterval(() => { + executeAndSetInterval(() => { memeHandler.getRandomMeme(sub => { let status = `"${sub.title}"\n${sub.text}\nby ${sub.author}`; if (allowedMedia.find(type => type === sub.url.split(/[.]+/).pop())){ @@ -26,3 +29,8 @@ tokenHandler.getAccessToken((accessToken) => { }) }, interval); }); + +function executeAndSetInterval(handler, timout){ + handler(); + setInterval(handler, timout); +} diff --git a/node_app/mastodonHandler.js b/node_app/mastodonHandler.js index 3968293..7566ee4 100644 --- a/node_app/mastodonHandler.js +++ b/node_app/mastodonHandler.js @@ -1,20 +1,18 @@ import Mastodon from "mastodon-api"; import fs from "fs"; -export class MastodonHandler{ - constructor(baseUrl, token) { - this.client = new Mastodon({ - api_url: `${baseUrl}/api/v1/`, - access_token: token, - }) - } +export class MastodonHandler extends Mastodon{ postStatus(status){ - this.client.post('statuses', {status: status}).then((resp) => console.log(resp.data)) + this.post('statuses', {status: status}) + .then(console.log("status posted => ")) + .then((resp) => console.log(resp.data)); } postMedia(status, mediaPath){ - this.client.post('media', { file: fs.createReadStream(mediaPath) }).then(resp => { + this.post('media', { file: fs.createReadStream(mediaPath) }).then(resp => { let id = resp.data.id; - this.client.post('statuses', { status: status, media_ids: [id] }).then((resp) => console.log(resp.data)) + this.post('statuses', { status: status, media_ids: [id] }) + .then(console.log("media posted => ")) + .then((resp) => console.log(resp.data)); }); } } \ No newline at end of file