random-bunny/src/index.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-12-01 20:32:20 +00:00
import IFetchResult from "./contracts/IFetchResult";
import IRedditResult from "./contracts/IRedditResult";
2021-12-01 20:39:02 +00:00
import fetch from "got";
2021-12-01 20:32:20 +00:00
const sortable = [
'new',
'hot',
'top'
];
export default async function randomBunny(subreddit: string, sortBy: string, maxTries = 100): Promise<IFetchResult> {
if (!sortable.includes(sortBy)) sortBy = 'hot';
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
if (!result) {
return {
IsSuccess: false
}
}
2021-12-01 20:39:02 +00:00
const json = JSON.parse(result.body);
2021-12-01 20:32:20 +00:00
if (!json) {
return {
IsSuccess: false
}
}
const data = json.data.children;
const size = data.length;
for (let i = 0; i < maxTries; i++) {
const random = Math.floor((Math.random() * size - 1) + 0); // Between 0 and (size - 1)
const randomSelect = data[random].data;
const redditResult: IRedditResult = {
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 (redditResult.Url.includes('.jpg')) {
return {
IsSuccess: true,
Result: redditResult
};
}
}
return {
IsSuccess: false
}
}