You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.5 KiB
JavaScript

import {MastodonHandler} from "./mastodonHandler.js";
import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
import {MemeHandler} from "./memeHandler.js";
4 years ago
import {getStat} from "./JSONdataStore.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;
let tokenHandler = new MastodonTokenHandler(baseUrl);
4 years ago
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);
});
function executeAndSetInterval(handler, timout){
handler();
setInterval(handler, timout);
}