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 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);
}

@ -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));
});
}
}
Loading…
Cancel
Save