add memeHandler and post every hour

master
adb 4 years ago
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);
}); });

@ -1,4 +1,5 @@
import Mastodon from "mastodon-api"; import Mastodon from "mastodon-api";
import fs from "fs";
export class MastodonHandler{ export class MastodonHandler{
constructor(baseUrl, token) { constructor(baseUrl, token) {
@ -7,4 +8,13 @@ export class MastodonHandler{
access_token: token, access_token: token,
}) })
} }
postStatus(status){
this.client.post('statuses', {status: status}).then((resp) => console.log(resp.data))
}
postMedia(status, mediaPath){
this.client.post('media', { file: fs.createReadStream(mediaPath) }).then(resp => {
let id = resp.data.id;
this.client.post('statuses', { status: status, media_ids: [id] }).then((resp) => console.log(resp.data))
});
}
} }

@ -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)];
}
}

@ -8,8 +8,8 @@
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "adb",
"license": "ISC", "license": "MPLv2",
"dependencies": { "dependencies": {
"mastodon-api": "^1.3.0" "mastodon-api": "^1.3.0"
} }

Loading…
Cancel
Save