|
|
|
import {MastodonHandler} from "./mastodonHandler.js";
|
|
|
|
import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
|
|
|
|
import {MemeHandler} from "./memeHandler.js";
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|