Compare commits

..

No commits in common. '981d62c6f055b4bfb476db96fa6394071e6ba528' and 'db78339b02eb3bd3f7b135829cede255cb3cd586' have entirely different histories.

@ -1,16 +1,39 @@
import {MastodonHandler} from "./mastodonHandler.js";
import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
import {mastodonMemeBot} from "./mastodonMemeBot.js";
import {MemeHandler} from "./memeHandler.js";
import {getStat} from "./JSONdataStore.js";
const config = {
baseUrl: "https://social.cybre.town",
memeAPI: "http://redditapi.adb.sh/api/v1/",
subreddits: ["r/programmerhumor/random"],
allowedMedia: ["jpg", "jpeg", "gif", "png"]
};
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(config.baseUrl);
tokenHandler.getAccessToken((accessToken) => {
config.accessToken = accessToken;
let bot = new mastodonMemeBot(config);
bot.setFullInterval(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);
}

@ -8,7 +8,7 @@ export class MastodonHandler extends Mastodon{
console.log(resp.data);
});
}
postMedia(status, mediaPath, retry, onError, callback){
postMedia(status, mediaPath, retry, onError){
if (retry <= 0){
onError();
return;
@ -25,7 +25,6 @@ export class MastodonHandler extends Mastodon{
this.post('statuses', { status: status, media_ids: [id] }).then((resp) => {
console.log("media posted => ");
console.log(resp.data);
callback();
});
});
}

@ -1,41 +0,0 @@
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,6 +35,7 @@ export class MemeHandler{
callback(JSON.parse(data))
})
})
}
randomFromArray(array){
return array[Math.floor(Math.random()*array.length)];

Loading…
Cancel
Save