random-bunny/index.js

121 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2021-01-29 18:26:48 +00:00
// Required Modules
2020-12-20 19:39:03 +00:00
const fetch = require('node-fetch');
2016-04-27 20:06:24 +01:00
2021-01-29 18:26:48 +00:00
// Valid sortBy names
2021-01-13 21:08:47 +00:00
const sortable = [
2020-12-20 19:39:03 +00:00
'new',
'hot',
'top'
]
2016-04-27 20:06:24 +01:00
2021-01-29 18:26:48 +00:00
// Main function
2020-12-20 19:39:03 +00:00
function randomBunny(subreddit, sortBy, cb) {
2021-01-29 18:26:48 +00:00
// If the sortable list doesn't include sortBy, default to 'hot'
2020-12-20 19:39:03 +00:00
if (!sortable.includes(sortBy)) sortBy = 'hot';
2016-04-27 20:06:24 +01:00
2021-01-29 18:26:48 +00:00
// Fetch the json from reddit
// For example, if you're getting a random image from r/rabbits, sorted by new:
// https://www.reddit.com/r/rabbits/new.json
2020-12-20 19:39:03 +00:00
fetch(`https://www.reddit.com/r/${subreddit}/${sortBy}.json`).then(res => {
res.json().then(res => {
2021-01-29 18:26:48 +00:00
// Get the part of the json string which the data comes from
2020-12-20 19:39:03 +00:00
const data = res.data.children;
const size = data.length;
2016-04-27 20:06:24 +01:00
2021-01-29 18:26:48 +00:00
// Found is used for the while loop in order to break out of the loop.
// We need to loop as the json string will contain invalid data for what we need
// Specifically videos.
2020-12-20 19:39:03 +00:00
let found = false;
2016-04-27 20:06:24 +01:00
2021-01-29 18:26:48 +00:00
// Loop until a valid image post is found
2020-12-20 19:39:03 +00:00
while (!found) {
2021-01-29 18:26:48 +00:00
// Generate random number
2020-12-20 19:39:03 +00:00
const random = getRandom(0, size - 1);
2016-04-27 20:06:24 +01:00
2021-01-29 18:26:48 +00:00
// Get variables from json to pass back
2021-03-01 18:15:51 +00:00
const randomSelect = data[random].data;
// The json string to send back
const json = {
archived: randomSelect['archived'],
downs: randomSelect['downs'],
hidden: randomSelect['hidden'],
permalink: randomSelect['permalink'],
subreddit: randomSelect['subreddit'],
subredditSubscribers: randomSelect['subreddit_subscribers'],
title: randomSelect['title'],
ups: randomSelect['ups'],
url: randomSelect['url']
};
2016-04-28 16:31:41 +01:00
2021-01-29 18:26:48 +00:00
// If the post is a .jpg, send back the data and stop looping
2021-03-01 18:15:51 +00:00
if (json.url.includes('.jpg')) {
2020-12-20 19:39:03 +00:00
found = true;
2021-03-01 18:15:51 +00:00
cb(json);
2020-12-20 19:39:03 +00:00
}
2016-04-29 03:23:21 +01:00
}
});
2020-12-20 19:39:03 +00:00
});
2016-04-29 03:23:21 +01:00
}
function promise(subreddit, sortBy) {
return new Promise(resolve => {
// If the sortable list doesn't include sortBy, default to 'hot'
if (!sortable.includes(sortBy)) sortBy = 'hot';
// Fetch the json from reddit
// For example, if you're getting a random image from r/rabbits, sorted by new:
// https://www.reddit.com/r/rabbits/new.json
fetch(`https://www.reddit.com/r/${subreddit}/${sortBy}.json`).then(res => {
res.json().then(res => {
// Get the part of the json string which the data comes from
const data = res.data.children;
const size = data.length;
// Found is used for the while loop in order to break out of the loop.
// We need to loop as the json string will contain invalid data for what we need
// Specifically videos.
let found = false;
// Loop until a valid image post is found
while (!found) {
// Generate random number
const random = getRandom(0, size - 1);
// Get variables from json to pass back
const randomSelect = data[random].data;
// The json string to send back
const json = {
archived: randomSelect['archived'],
downs: randomSelect['downs'],
hidden: randomSelect['hidden'],
permalink: randomSelect['permalink'],
subreddit: randomSelect['subreddit'],
subredditSubscribers: randomSelect['subreddit_subscribers'],
title: randomSelect['title'],
ups: randomSelect['ups'],
url: randomSelect['url']
};
// If the post is a .jpg, send back the data and stop looping
if (json.url.includes('.jpg')) {
found = true;
resolve(json);
}
}
});
});
})
}
2021-01-29 18:26:48 +00:00
// Generate a random number
2020-12-20 19:39:03 +00:00
function getRandom(min, max) {
return Math.floor((Math.random() * max) + min);
2016-04-28 16:31:41 +01:00
}
2021-01-29 18:26:48 +00:00
// Export Functions
module.exports = {
randomBunny,
promise,
};