Add "Gallery" field to return multiple images in 1 post #242
4 changed files with 19 additions and 5 deletions
|
@ -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[],
|
||||||
}
|
}
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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}`);
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue