Compare commits
No commits in common. '981d62c6f055b4bfb476db96fa6394071e6ba528' and 'db78339b02eb3bd3f7b135829cede255cb3cd586' have entirely different histories.
981d62c6f0
...
db78339b02
@ -1,16 +1,39 @@
|
|||||||
|
import {MastodonHandler} from "./mastodonHandler.js";
|
||||||
import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
|
import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
|
||||||
import {mastodonMemeBot} from "./mastodonMemeBot.js";
|
import {MemeHandler} from "./memeHandler.js";
|
||||||
|
import {getStat} from "./JSONdataStore.js";
|
||||||
|
|
||||||
const config = {
|
const baseUrl = "https://social.cybre.town";
|
||||||
baseUrl: "https://social.cybre.town",
|
const memeAPI = "http://redditapi.adb.sh/api/v1/";
|
||||||
memeAPI: "http://redditapi.adb.sh/api/v1/",
|
const subreddits = ["r/programmerhumor/random"];
|
||||||
subreddits: ["r/programmerhumor/random"],
|
const allowedMedia = ["jpg", "jpeg", "gif", "png"];
|
||||||
allowedMedia: ["jpg", "jpeg", "gif", "png"]
|
const interval = 1000*60*60;
|
||||||
};
|
|
||||||
|
|
||||||
let tokenHandler = new MastodonTokenHandler(config.baseUrl);
|
let tokenHandler = new MastodonTokenHandler(baseUrl);
|
||||||
tokenHandler.getAccessToken((accessToken) => {
|
await tokenHandler.getAccessToken((accessToken) => {
|
||||||
config.accessToken = accessToken;
|
let client = new MastodonHandler({
|
||||||
let bot = new mastodonMemeBot(config);
|
api_url: `${baseUrl}/api/v1/`,
|
||||||
bot.setFullInterval(60);
|
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);
|
||||||
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue