diff --git a/main.js b/main.js index dd754d3..a360793 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,28 @@ 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 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); tokenHandler.getAccessToken((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); }); diff --git a/mastodonHandler.js b/mastodonHandler.js index 355256d..3968293 100644 --- a/mastodonHandler.js +++ b/mastodonHandler.js @@ -1,4 +1,5 @@ import Mastodon from "mastodon-api"; +import fs from "fs"; export class MastodonHandler{ constructor(baseUrl, token) { @@ -7,4 +8,13 @@ export class MastodonHandler{ 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)) + }); + } } \ No newline at end of file diff --git a/memeHandler.js b/memeHandler.js new file mode 100644 index 0000000..4e69bb8 --- /dev/null +++ b/memeHandler.js @@ -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)]; + } +} \ No newline at end of file diff --git a/package.json b/package.json index 3a9be0e..073f94d 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,8 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], - "author": "", - "license": "ISC", + "author": "adb", + "license": "MPLv2", "dependencies": { "mastodon-api": "^1.3.0" }