add class mastodonMemeBot

master
adb 4 years ago
parent 53bb5a66ae
commit 39a1cb2518

@ -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 tokenHandler = new MastodonTokenHandler(config.baseUrl);
tokenHandler.getAccessToken((accessToken) => {
config.accessToken = accessToken;
let bot = new mastodonMemeBot(config);
bot.setFullInterval(60);
});
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);
});
function executeAndSetInterval(handler, timout){
handler();
setInterval(handler, timout);
}

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

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

@ -35,7 +35,6 @@ export class MemeHandler{
callback(JSON.parse(data))
})
})
}
randomFromArray(array){
return array[Math.floor(Math.random()*array.length)];

Loading…
Cancel
Save