Vylpes
06485e0a88
All checks were successful
continuous-integration/drone/push Build is passing
- Setup Jest - Add test scripts - Configure Drone Fixes #1 Co-authored-by: Ethan Lane <ethan@vylpes.com> Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/57
68 lines
No EOL
1.8 KiB
TypeScript
68 lines
No EOL
1.8 KiB
TypeScript
import IReturnResult from "./contracts/IReturnResult.js";
|
|
import IRedditResult from "./contracts/IRedditResult.js";
|
|
import fetch from "got-cjs";
|
|
import { List } from 'linqts';
|
|
import IFetchResult from "./contracts/IFetchResult.js";
|
|
|
|
const sortable = [
|
|
'new',
|
|
'hot',
|
|
'top'
|
|
];
|
|
|
|
export default async function randomBunny(subreddit: string, sortBy?: string): Promise<IReturnResult> {
|
|
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
|
|
|
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
|
|
|
if (!result) {
|
|
return {
|
|
IsSuccess: false
|
|
}
|
|
}
|
|
|
|
const json = JSON.parse(result.body);
|
|
|
|
if (!json) {
|
|
return {
|
|
IsSuccess: false
|
|
}
|
|
}
|
|
|
|
const data: IFetchResult[] = json.data.children;
|
|
|
|
const dataWithImages = new List<IFetchResult>(data)
|
|
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
|
|
.ToArray();
|
|
|
|
let random = 0;
|
|
|
|
if (dataWithImages.length == 0) {
|
|
return {
|
|
IsSuccess: false,
|
|
};
|
|
} else if (dataWithImages.length > 1) {
|
|
random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
|
|
}
|
|
|
|
const randomSelect = dataWithImages[random];
|
|
|
|
const randomData = randomSelect.data;
|
|
|
|
const redditResult: IRedditResult = {
|
|
Archived: randomData['archived'],
|
|
Downs: randomData['downs'],
|
|
Hidden: randomData['hidden'],
|
|
Permalink: randomData['permalink'],
|
|
Subreddit: randomData['subreddit'],
|
|
SubredditSubscribers: randomData['subreddit_subscribers'],
|
|
Title: randomData['title'],
|
|
Ups: randomData['ups'],
|
|
Url: randomData['url']
|
|
};
|
|
|
|
return {
|
|
IsSuccess: true,
|
|
Result: redditResult
|
|
};
|
|
} |