2023-09-15 15:18:57 +01:00
|
|
|
import IReturnResult from "./contracts/IReturnResult";
|
|
|
|
import IRedditResult from "./contracts/IRedditResult";
|
2023-04-22 13:20:30 +01:00
|
|
|
import fetch from "got-cjs";
|
2022-12-18 15:27:03 +00:00
|
|
|
import { List } from 'linqts';
|
2023-09-15 15:18:57 +01:00
|
|
|
import IFetchResult from "./contracts/IFetchResult";
|
|
|
|
import { ErrorCode } from "./constants/ErrorCode";
|
|
|
|
import ErrorMessages from "./constants/ErrorMessages";
|
2024-06-22 10:09:55 +01:00
|
|
|
import ImageHelper from "./helpers/imageHelper";
|
2021-12-01 20:32:20 +00:00
|
|
|
|
2024-07-26 18:23:19 +01:00
|
|
|
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot', limit: number = 100): Promise<IReturnResult> {
|
|
|
|
if (limit < 1 || limit > 100) {
|
|
|
|
return {
|
|
|
|
IsSuccess: false,
|
|
|
|
Query: {
|
|
|
|
subreddit: subreddit,
|
|
|
|
sortBy: sortBy,
|
|
|
|
limit: limit,
|
|
|
|
},
|
|
|
|
Error: {
|
|
|
|
Code: ErrorCode.LimitOutOfRange,
|
|
|
|
Message: ErrorMessages.LimitOutOfRange,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=${limit}`)
|
2023-10-20 17:31:38 +01:00
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return null;
|
|
|
|
});
|
2021-12-01 20:32:20 +00:00
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return {
|
2023-09-15 15:18:57 +01:00
|
|
|
IsSuccess: false,
|
2023-12-08 17:10:00 +00:00
|
|
|
Query: {
|
|
|
|
subreddit: subreddit,
|
|
|
|
sortBy: sortBy,
|
2024-07-26 18:23:19 +01:00
|
|
|
limit: limit,
|
2023-12-08 17:10:00 +00:00
|
|
|
},
|
2023-09-15 15:18:57 +01:00
|
|
|
Error: {
|
|
|
|
Code: ErrorCode.FailedToFetchReddit,
|
|
|
|
Message: ErrorMessages.FailedToFetchReddit,
|
|
|
|
},
|
2021-12-01 20:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-09-15 15:18:57 +01:00
|
|
|
IsSuccess: false,
|
2023-12-08 17:10:00 +00:00
|
|
|
Query: {
|
|
|
|
subreddit: subreddit,
|
|
|
|
sortBy: sortBy,
|
2024-07-26 18:23:19 +01:00
|
|
|
limit: limit,
|
2023-12-08 17:10:00 +00:00
|
|
|
},
|
2023-09-15 15:18:57 +01:00
|
|
|
Error: {
|
|
|
|
Code: ErrorCode.UnableToParseJSON,
|
|
|
|
Message: ErrorMessages.UnableToParseJSON,
|
|
|
|
},
|
2021-12-01 20:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 15:27:03 +00:00
|
|
|
const data: IFetchResult[] = json.data.children;
|
2023-02-22 18:18:50 +00:00
|
|
|
|
2022-12-18 15:27:03 +00:00
|
|
|
const dataWithImages = new List<IFetchResult>(data)
|
2024-04-07 11:03:20 +01:00
|
|
|
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png') || x!.data.url.includes("/gallery/"))
|
2022-12-18 15:27:03 +00:00
|
|
|
.ToArray();
|
2021-12-01 20:32:20 +00:00
|
|
|
|
2023-04-22 13:20:30 +01:00
|
|
|
let random = 0;
|
2021-12-01 20:32:20 +00:00
|
|
|
|
2023-04-22 13:20:30 +01:00
|
|
|
if (dataWithImages.length == 0) {
|
2022-12-18 15:27:03 +00:00
|
|
|
return {
|
2023-02-22 18:18:50 +00:00
|
|
|
IsSuccess: false,
|
2023-12-08 17:10:00 +00:00
|
|
|
Query: {
|
|
|
|
subreddit: subreddit,
|
|
|
|
sortBy: sortBy,
|
2024-07-26 18:23:19 +01:00
|
|
|
limit: limit,
|
2023-12-08 17:10:00 +00:00
|
|
|
},
|
2023-09-15 15:18:57 +01:00
|
|
|
Error: {
|
|
|
|
Code: ErrorCode.NoImageResultsFound,
|
|
|
|
Message: ErrorMessages.NoImageResultsFound,
|
|
|
|
},
|
2022-12-18 15:27:03 +00:00
|
|
|
};
|
2023-05-29 16:48:01 +01:00
|
|
|
} else {
|
|
|
|
random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1)
|
2023-04-22 13:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const randomSelect = dataWithImages[random];
|
2023-02-22 18:18:50 +00:00
|
|
|
|
|
|
|
const randomData = randomSelect.data;
|
|
|
|
|
2024-04-07 11:03:20 +01:00
|
|
|
let url: string;
|
|
|
|
|
|
|
|
if (randomData.url.includes("/gallery")) {
|
|
|
|
const galleryImage = await ImageHelper.FetchImageFromRedditGallery(randomData.url);
|
|
|
|
|
|
|
|
if (!galleryImage) {
|
|
|
|
return {
|
|
|
|
IsSuccess: false,
|
2024-04-12 18:05:00 +01:00
|
|
|
Query: {
|
|
|
|
subreddit: subreddit,
|
|
|
|
sortBy: sortBy,
|
2024-07-26 18:23:19 +01:00
|
|
|
limit: limit,
|
2024-04-12 18:05:00 +01:00
|
|
|
},
|
|
|
|
Error: {
|
|
|
|
Code: ErrorCode.NoImageResultsFound,
|
|
|
|
Message: ErrorMessages.NoImageResultsFound,
|
|
|
|
},
|
2024-04-07 11:03:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url = galleryImage;
|
|
|
|
} else {
|
|
|
|
url = randomData.url;
|
|
|
|
}
|
|
|
|
|
2023-02-22 18:18:50 +00:00
|
|
|
const redditResult: IRedditResult = {
|
2024-08-02 17:47:47 +01:00
|
|
|
Author: randomData['author'],
|
2023-02-22 18:18:50 +00:00
|
|
|
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'],
|
2024-04-07 11:03:20 +01:00
|
|
|
Url: url,
|
2023-02-22 18:18:50 +00:00
|
|
|
};
|
2021-12-01 20:32:20 +00:00
|
|
|
|
|
|
|
return {
|
2023-02-22 18:18:50 +00:00
|
|
|
IsSuccess: true,
|
2023-12-08 17:10:00 +00:00
|
|
|
Query: {
|
|
|
|
subreddit: subreddit,
|
|
|
|
sortBy: sortBy,
|
2024-07-26 18:23:19 +01:00
|
|
|
limit: limit,
|
2023-12-08 17:10:00 +00:00
|
|
|
},
|
2023-02-22 18:18:50 +00:00
|
|
|
Result: redditResult
|
|
|
|
};
|
2021-12-01 20:32:20 +00:00
|
|
|
}
|