Comment Code
This commit is contained in:
parent
ddd9d728c1
commit
cfe7b5d78b
3 changed files with 20 additions and 3 deletions
17
index.js
17
index.js
|
@ -1,27 +1,42 @@
|
||||||
|
// Required Modules
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
|
// Valid sortBy names
|
||||||
const sortable = [
|
const sortable = [
|
||||||
'new',
|
'new',
|
||||||
'hot',
|
'hot',
|
||||||
'top'
|
'top'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// Main function
|
||||||
function randomBunny(subreddit, sortBy, cb) {
|
function randomBunny(subreddit, sortBy, cb) {
|
||||||
|
// If the sortable list doesn't include sortBy, default to 'hot'
|
||||||
if (!sortable.includes(sortBy)) sortBy = '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 => {
|
fetch(`https://www.reddit.com/r/${subreddit}/${sortBy}.json`).then(res => {
|
||||||
res.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 data = res.data.children;
|
||||||
const size = data.length;
|
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;
|
let found = false;
|
||||||
|
|
||||||
|
// Loop until a valid image post is found
|
||||||
while (!found) {
|
while (!found) {
|
||||||
|
// Generate random number
|
||||||
const random = getRandom(0, size - 1);
|
const random = getRandom(0, size - 1);
|
||||||
|
|
||||||
|
// Get variables from json to pass back
|
||||||
const image = data[random].data['url'];
|
const image = data[random].data['url'];
|
||||||
const title = data[random].data['title'];
|
const title = data[random].data['title'];
|
||||||
|
|
||||||
|
// If the post is a .jpg, send back the data and stop looping
|
||||||
if (image.includes('.jpg')) {
|
if (image.includes('.jpg')) {
|
||||||
found = true;
|
found = true;
|
||||||
cb(image, title);
|
cb(image, title);
|
||||||
|
@ -31,8 +46,10 @@ function randomBunny(subreddit, sortBy, cb) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate a random number
|
||||||
function getRandom(min, max) {
|
function getRandom(min, max) {
|
||||||
return Math.floor((Math.random() * max) + min);
|
return Math.floor((Math.random() * max) + min);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Export Functions
|
||||||
module.exports = randomBunny;
|
module.exports = randomBunny;
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "random-bunny",
|
"name": "random-bunny",
|
||||||
"version": "1.0.0",
|
"version": "20.0.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "random-bunny",
|
"name": "random-bunny",
|
||||||
"version": "1.0.0",
|
"version": "20.0.0",
|
||||||
"description": "Get a random subreddit image url",
|
"description": "Get a random subreddit image url",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"author": "Vylpes",
|
"author": "Vylpes",
|
||||||
|
|
Loading…
Reference in a new issue