add memeHandler and post every hour
parent
771974202e
commit
81c437ce16
@ -1,9 +1,28 @@
|
|||||||
import {MastodonHandler} from "./mastodonHandler.js";
|
import {MastodonHandler} from "./mastodonHandler.js";
|
||||||
import {MastodonTokenHandler} from "./mastodonTokenHandler.js"
|
import {MastodonTokenHandler} from "./mastodonTokenHandler.js";
|
||||||
|
import {MemeHandler} from "./memeHandler.js";
|
||||||
|
|
||||||
const baseUrl = "https://social.cybre.town";
|
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);
|
let tokenHandler = new MastodonTokenHandler(baseUrl);
|
||||||
tokenHandler.getAccessToken((accessToken) => {
|
tokenHandler.getAccessToken((accessToken) => {
|
||||||
let client = new MastodonHandler(baseUrl, accessToken);
|
let client = new MastodonHandler(baseUrl, accessToken);
|
||||||
|
let memeHandler = new MemeHandler(memeAPI, subreddits, allowedMedia);
|
||||||
|
setInterval(() => {
|
||||||
|
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);
|
||||||
});
|
});
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
import http from "http";
|
||||||
|
import https from "https";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
export class MemeHandler{
|
||||||
|
constructor(memeAPI, subreddits, allowedMedia){
|
||||||
|
this.memeAPI = memeAPI;
|
||||||
|
this.subreddits = subreddits;
|
||||||
|
this.allowedMedia = allowedMedia;
|
||||||
|
}
|
||||||
|
getRandomMeme(callback){
|
||||||
|
this.request(this.memeAPI+this.randomFromArray(this.subreddits), (res) => {
|
||||||
|
let sub = this.randomFromArray(res.subs);
|
||||||
|
callback(sub);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
downloadMedia(url, filepath, callback){
|
||||||
|
const request = https.get(url, (res) => {
|
||||||
|
res.pipe(fs.createWriteStream(filepath));
|
||||||
|
res.on('end', () => {
|
||||||
|
callback();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
request(url, callback){
|
||||||
|
http.get(url, res => {
|
||||||
|
let data = "";
|
||||||
|
res.on('data', chunk => {
|
||||||
|
data += chunk;
|
||||||
|
})
|
||||||
|
res.on('end', () => {
|
||||||
|
callback(JSON.parse(data))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
randomFromArray(array){
|
||||||
|
return array[Math.floor(Math.random()*array.length)];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue