Compare commits
2 Commits
db78339b02
...
981d62c6f0
Author | SHA1 | Date |
---|---|---|
adb | 981d62c6f0 | 4 years ago |
adb | 39a1cb2518 | 4 years ago |
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue