UNPKG

meme101

Version:

Meme package made by Justin2528!

79 lines (67 loc) 2.33 kB
var req = require('request'); var sett = require('./sett'); var meme101 = function(sr, callback) { var web = [ 'https://www.reddit.com/r/crappydesign/', 'https://www.reddit.com/r/dankmemes/', 'https://www.reddit.com/r/me_irl/', 'https://www.reddit.com/r/wholesomememes/', 'https://www.reddit.com/r/memeeconomy/', 'https://www.reddit.com/r/crappyoffbrands/' ]; var names = [ 'crappydesign', 'dankmemes', 'me_irl', 'wholesomememes', 'memeeconomy', 'crappyoffbrands' ]; var url; var ran = web[~~(Math.random() * web.length)]; var t; if (typeof sr === 'function') { callback = sr sr = {} url = `${ran}.json?sort=top&t=day&limit=100`; } else { if (!names.includes(sr.toLowerCase())) throw new Error('Invalid subreddit'); if (names.includes(sr.toLowerCase())) { var i; for (i = 0; i < names.length; i++) { if (sr.toLowerCase() === web[i].split('/')[4]) { t = web[i]; } } } url = `${t}.json?sort=top&t=day&limit=100`; } var obj = { 'title': [], 'url': [], 'author': [], 'subreddit': [], 'created': [], 'created_utc': [] }; req.get(url, function(err, res, body) { if (err || res.statusCode !== 200) { console.error(new Error('Try running the program again.')); return callback(err); } else if (!err && res.statusCode === 200) { body = JSON.parse(res.body); data = body.data.children; var rand = Math.floor(Math.random() * Math.floor(data.length)); names.title.push(data[rand].data.title); names.url.push(data[rand].data.url); names.author.push(data[rand].data.author); names.subreddit.push(data[rand].data.subreddit); names.created.push(sett.time(parseInt(data[rand].data.created))); names.created_utc.push(sett.time(parseInt(data[rand].data.created_utc))); return callback(names); } }) } module.exports = meme101;