mastodonHandler extends Mastodon

master
adb 4 years ago
parent baf1e2cc48
commit 50d1dd7e17

@ -6,13 +6,16 @@ const baseUrl = "https://social.cybre.town";
const memeAPI = "http://redditapi.adb.sh/api/v1/"; const memeAPI = "http://redditapi.adb.sh/api/v1/";
const subreddits = ["r/programmerhumor/random"]; const subreddits = ["r/programmerhumor/random"];
const allowedMedia = ["jpg", "jpeg", "gif", "png"]; const allowedMedia = ["jpg", "jpeg", "gif", "png"];
const interval = 1000*60; const interval = 1000*60*60;
let tokenHandler = new MastodonTokenHandler(baseUrl); let tokenHandler = new MastodonTokenHandler(baseUrl);
tokenHandler.getAccessToken((accessToken) => { 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); let memeHandler = new MemeHandler(memeAPI, subreddits, allowedMedia);
setInterval(() => { executeAndSetInterval(() => {
memeHandler.getRandomMeme(sub => { memeHandler.getRandomMeme(sub => {
let status = `"${sub.title}"\n${sub.text}\nby ${sub.author}`; let status = `"${sub.title}"\n${sub.text}\nby ${sub.author}`;
if (allowedMedia.find(type => type === sub.url.split(/[.]+/).pop())){ if (allowedMedia.find(type => type === sub.url.split(/[.]+/).pop())){
@ -26,3 +29,8 @@ tokenHandler.getAccessToken((accessToken) => {
}) })
}, interval); }, interval);
}); });
function executeAndSetInterval(handler, timout){
handler();
setInterval(handler, timout);
}

@ -1,20 +1,18 @@
import Mastodon from "mastodon-api"; import Mastodon from "mastodon-api";
import fs from "fs"; import fs from "fs";
export class MastodonHandler{ export class MastodonHandler extends Mastodon{
constructor(baseUrl, token) {
this.client = new Mastodon({
api_url: `${baseUrl}/api/v1/`,
access_token: token,
})
}
postStatus(status){ 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){ 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; 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));
}); });
} }
} }
Loading…
Cancel
Save