diff --git a/README.md b/README.md index edc9da8..73882e3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # scooterquotebot +npm install +Create .TOKEN with your access token, and npm start. \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..15a6eff --- /dev/null +++ b/app.js @@ -0,0 +1,56 @@ +var Mastodon = require('mastodon'); +var Schedule = require('node-schedule'); +var fs = require('fs'); +var https = require('https'); +var m; +var ts; + +fs.readFile('.TOKEN', 'utf-8', function(err,data){ + if (err) { + console.log(err); + process.exit(1); + } + + m = new Mastodon({ access_token : data, api_url: 'https://botsin.space/api/v1/' }); + + console.log('Tooting Scooter quutes!'); + ts = Schedule.scheduleJob('1 8 * * 0,3', doQuote); + ts = Schedule.scheduleJob('6 13 * * 2,5', doQuote); + ts = Schedule.scheduleJob('19 18 * * 1,4', doQuote); + ts = Schedule.scheduleJob('31 20 * * 6', doQuote); +}); + +function doQuote() { + https.get('https://howmuchisthe.fish/json/random', (res) => { + var dat = ''; + + res.setEncoding('utf8'); + + res.on('data', (d) => { + dat += d; + }); + + res.on('end', () => { + try { + var js = JSON.parse(dat); + tootQuote( js ); + } catch (err) { + console.log(err); + } + }); + }).on('error', (e) => { + console.log(e); + }); +} + +function tootQuote(obj) { + if ( obj && + obj.quote && + obj.quote.text ) + { + console.log( 'Tooting: ' + obj.quote.text ); + var cw = obj.quote.text; + var message = "from '" + obj.quote.track + "' on the album '" + obj.quote.album + "'\n" + obj.quote.track_master; + m.post('statuses', { status: message, spoiler_text: cw, visibility: 'public' }); + } +} \ No newline at end of file