|
|
|
import {MastodonHandler} from "./mastodonHandler.js";
|
|
|
|
import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
|
|
|
|
import {MemeHandler} from "./memeHandler.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);
|
|
|
|
tokenHandler.getAccessToken((accessToken) => {
|
|
|
|
let client = new MastodonHandler({
|
|
|
|
api_url: `${baseUrl}/api/v1/`,
|
|
|
|
access_token: accessToken,
|
|
|
|
});
|
|
|
|
let memeHandler = new MemeHandler(memeAPI, subreddits, allowedMedia);
|
|
|
|
executeAndSetInterval(() => {
|
|
|
|
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()}`;
|
|
|
|
memeHandler.downloadMedia(sub.url, filepath, () => {
|
|
|
|
client.postMedia(status, filepath);
|
|
|
|
})
|
|
|
|
}else{
|
|
|
|
client.postStatus(status);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, interval);
|
|
|
|
});
|
|
|
|
|
|
|
|
function executeAndSetInterval(handler, timout){
|
|
|
|
handler();
|
|
|
|
setInterval(handler, timout);
|
|
|
|
}
|