Compare commits

...

2 Commits

@ -1,39 +1,16 @@
import {MastodonHandler} from "./mastodonHandler.js";
import {MastodonTokenHandler} from "./mastodonTokenHandler.js"; import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
import {MemeHandler} from "./memeHandler.js"; import {mastodonMemeBot} from "./mastodonMemeBot.js";
import {getStat} from "./JSONdataStore.js";
const baseUrl = "https://social.cybre.town"; const config = {
const memeAPI = "http://redditapi.adb.sh/api/v1/"; baseUrl: "https://social.cybre.town",
const subreddits = ["r/programmerhumor/random"]; memeAPI: "http://redditapi.adb.sh/api/v1/",
const allowedMedia = ["jpg", "jpeg", "gif", "png"]; subreddits: ["r/programmerhumor/random"],
const interval = 1000*60*60; allowedMedia: ["jpg", "jpeg", "gif", "png"]
};
let tokenHandler = new MastodonTokenHandler(baseUrl); let tokenHandler = new MastodonTokenHandler(config.baseUrl);
await tokenHandler.getAccessToken((accessToken) => { tokenHandler.getAccessToken((accessToken) => {
let client = new MastodonHandler({ config.accessToken = accessToken;
api_url: `${baseUrl}/api/v1/`, let bot = new mastodonMemeBot(config);
access_token: accessToken, 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); console.log(resp.data);
}); });
} }
postMedia(status, mediaPath, retry, onError){ postMedia(status, mediaPath, retry, onError, callback){
if (retry <= 0){ if (retry <= 0){
onError(); onError();
return; return;
@ -25,6 +25,7 @@ export class MastodonHandler extends Mastodon{
this.post('statuses', { status: status, media_ids: [id] }).then((resp) => { this.post('statuses', { status: status, media_ids: [id] }).then((resp) => {
console.log("media posted => "); console.log("media posted => ");
console.log(resp.data); 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)) callback(JSON.parse(data))
}) })
}) })
} }
randomFromArray(array){ randomFromArray(array){
return array[Math.floor(Math.random()*array.length)]; return array[Math.floor(Math.random()*array.length)];

Loading…
Cancel
Save