hotfix/2.0.3 (#41)
- Bump minimatch from 3.0.4 to 3.1.2 - Update discord links Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/41 Reviewed-by: VylpesTester <tester@vylpes.com>
This commit is contained in:
parent
09cbd56493
commit
031a5455b5
10 changed files with 117 additions and 229 deletions
|
@ -1,6 +1,13 @@
|
|||
import IRedditResult from "./IRedditResult";
|
||||
|
||||
export default interface IFetchResult {
|
||||
IsSuccess: boolean;
|
||||
Result?: IRedditResult;
|
||||
data: {
|
||||
archived: boolean,
|
||||
downs: number,
|
||||
hidden: boolean,
|
||||
permalink: string,
|
||||
subreddit: string,
|
||||
subreddit_subscribers: number,
|
||||
title: string,
|
||||
ups: number,
|
||||
url: string
|
||||
}
|
||||
}
|
6
src/contracts/IReturnResult.ts
Normal file
6
src/contracts/IReturnResult.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import IRedditResult from "./IRedditResult";
|
||||
|
||||
export default interface IReturnResult {
|
||||
IsSuccess: boolean;
|
||||
Result?: IRedditResult;
|
||||
}
|
49
src/index.ts
49
src/index.ts
|
@ -1,6 +1,8 @@
|
|||
import IFetchResult from "./contracts/IFetchResult";
|
||||
import IReturnResult from "./contracts/IReturnResult";
|
||||
import IRedditResult from "./contracts/IRedditResult";
|
||||
import fetch from "got";
|
||||
import { List } from 'linqts';
|
||||
import IFetchResult from "./contracts/IFetchResult";
|
||||
|
||||
const sortable = [
|
||||
'new',
|
||||
|
@ -8,7 +10,7 @@ const sortable = [
|
|||
'top'
|
||||
];
|
||||
|
||||
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IFetchResult> {
|
||||
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IReturnResult> {
|
||||
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
||||
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
||||
|
@ -27,32 +29,37 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma
|
|||
}
|
||||
}
|
||||
|
||||
const data = json.data.children;
|
||||
const size = data.length;
|
||||
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();
|
||||
|
||||
for (let i = 0; i < maxTries; i++) {
|
||||
const random = Math.floor((Math.random() * size - 1) + 0); // Between 0 and (size - 1)
|
||||
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
|
||||
|
||||
const randomSelect = data[random].data;
|
||||
const randomSelect = dataWithImages[random];
|
||||
|
||||
if (!randomSelect) continue;
|
||||
|
||||
const randomData = randomSelect.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']
|
||||
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']
|
||||
};
|
||||
|
||||
if (redditResult.Url.includes('.jpg')) {
|
||||
return {
|
||||
IsSuccess: true,
|
||||
Result: redditResult
|
||||
};
|
||||
}
|
||||
return {
|
||||
IsSuccess: true,
|
||||
Result: redditResult
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue