Merge branch 'develop' into feature/181-q-json-flags

This commit is contained in:
Ethan Lane 2024-11-27 18:39:43 +00:00
commit f06ff77bcb
8 changed files with 52 additions and 21 deletions

View file

@ -2,11 +2,12 @@ export default interface IRedditResult {
Archived: boolean,
Author: string,
Downs: number,
Gallery: string[],
Hidden: boolean,
Permalink: string,
Subreddit: string,
SubredditSubscribers: number,
Title: string,
Ups: number,
Url: string
Url: string,
}

View file

@ -2,17 +2,19 @@ import fetch from "got-cjs";
import * as htmlparser from "htmlparser2";
export default class ImageHelper {
public static async FetchImageFromRedditGallery(url: string): Promise<string | undefined> {
public static async FetchImageFromRedditGallery(url: string): Promise<string[]> {
const fetched = await fetch(url);
if (!fetched || fetched.errored || fetched.statusCode != 200) {
return undefined;
return [];
}
const dom = htmlparser.parseDocument(fetched.body);
const img = htmlparser.DomUtils.findOne((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children, true);
const img = htmlparser.DomUtils.findAll((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children);
const imgSrc = img?.attributes.find(x => x.name == "src")?.value;
const imgSrc = img
.flatMap(x => x.attributes.find(x => x.name == "src")?.value)
.filter(x => x != undefined);
return imgSrc;
}

View file

@ -93,6 +93,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
const randomData = randomSelect.data;
let url: string;
let gallery: string[];
if (randomData.url.includes("/gallery")) {
const galleryImage = await ImageHelper.FetchImageFromRedditGallery(randomData.url);
@ -112,9 +113,11 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
}
}
url = galleryImage;
url = galleryImage[0];
gallery = galleryImage;
} else {
url = randomData.url;
gallery = [randomData.url];
}
const redditResult: IRedditResult = {
@ -128,6 +131,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
Title: randomData['title'],
Ups: randomData['ups'],
Url: url,
Gallery: gallery,
};
return {