Add "Gallery" field to return multiple images in 1 post #242

Merged
Vylpes merged 5 commits from feature/161-image-array into develop 2024-10-26 22:00:55 +01:00
4 changed files with 19 additions and 5 deletions
Showing only changes of commit 4aac791c75 - Show all commits

View file

@ -8,5 +8,6 @@ export default interface IRedditResult {
SubredditSubscribers: number, SubredditSubscribers: number,
Title: string, Title: string,
Ups: number, Ups: number,
Url: string Url: string,
Gallery: string[],
} }

View file

@ -2,7 +2,7 @@ import fetch from "got-cjs";
import * as htmlparser from "htmlparser2"; import * as htmlparser from "htmlparser2";
export default class ImageHelper { export default class ImageHelper {
public static async FetchImageFromRedditGallery(url: string): Promise<string | undefined> { public static async FetchImageFromRedditGallery(url: string): Promise<string[] | undefined> {
const fetched = await fetch(url); const fetched = await fetch(url);
if (!fetched || fetched.errored || fetched.statusCode != 200) { if (!fetched || fetched.errored || fetched.statusCode != 200) {
@ -10,9 +10,15 @@ export default class ImageHelper {
} }
const dom = htmlparser.parseDocument(fetched.body); 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; if (!img) {
return [];
}
const imgSrc = img
.flatMap(x => x.attributes.find(x => x.name == "src")?.value)
.filter(x => x != undefined);
return imgSrc; return imgSrc;
} }

View file

@ -22,6 +22,10 @@ export default class OutputHelper {
outputLines.push(`Upvotes = ${result.Ups}`); outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`); outputLines.push(`Url = ${result.Url}`);
if (result.Gallery.length > 1) {
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
}
if (options.queryMetadata != null) { if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`); outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);

View file

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