Random Bunny 1.1

This commit is contained in:
Vylpes 2021-03-01 18:15:51 +00:00
parent cfe7b5d78b
commit 0626ac59da
3 changed files with 1211 additions and 9 deletions

View file

@ -33,13 +33,25 @@ function randomBunny(subreddit, sortBy, cb) {
const random = getRandom(0, size - 1); const random = getRandom(0, size - 1);
// Get variables from json to pass back // Get variables from json to pass back
const image = data[random].data['url']; const randomSelect = data[random].data;
const title = data[random].data['title'];
// 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 the post is a .jpg, send back the data and stop looping
if (image.includes('.jpg')) { if (json.url.includes('.jpg')) {
found = true; found = true;
cb(image, title); cb(json);
} }
} }
}); });

1181
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -14,8 +14,8 @@ $ npm install --save random-bunny
```js ```js
const randomBunny = require('random-bunny'); const randomBunny = require('random-bunny');
randomBunny('rabbits', 'new', (image, title) => { randomBunny('rabbits', 'new', res => {
console.log(title + ": " + image); console.log(res.title + ": " + res.url);
}); });
``` ```
@ -24,7 +24,18 @@ randomBunny('rabbits', 'new', (image, title) => {
### `randomBunny()` ### `randomBunny()`
Returns a `url` and `title` for a random post to the `callback`. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `callback(image, title)` Returns a `json string` for a random post to the `callback`. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `callback(res)`
The json string which gets returned consists of:
- archived
- downs
- hidden
- permalink
- subreddit
- subredditSubscribers
- title
- ups
- url
## Notes ## Notes
@ -37,4 +48,4 @@ Returns a `url` and `title` for a random post to the `callback`. Accepts 3 argum
## License ## License
MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes); MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes)