Update script to include a Gallery option if there is more than 1 of a url
This commit is contained in:
parent
db35dee5f9
commit
4aac791c75
4 changed files with 19 additions and 5 deletions
|
@ -8,5 +8,6 @@ export default interface IRedditResult {
|
|||
SubredditSubscribers: number,
|
||||
Title: string,
|
||||
Ups: number,
|
||||
Url: string
|
||||
Url: string,
|
||||
Gallery: string[],
|
||||
}
|
|
@ -2,7 +2,7 @@ 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[] | undefined> {
|
||||
const fetched = await fetch(url);
|
||||
|
||||
if (!fetched || fetched.errored || fetched.statusCode != 200) {
|
||||
|
@ -10,9 +10,15 @@ export default class ImageHelper {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,10 @@ export default class OutputHelper {
|
|||
outputLines.push(`Upvotes = ${result.Ups}`);
|
||||
outputLines.push(`Url = ${result.Url}`);
|
||||
|
||||
if (result.Gallery.length > 1) {
|
||||
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
|
||||
}
|
||||
|
||||
if (options.queryMetadata != null) {
|
||||
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
||||
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;
|
||||
|
||||
let url: string;
|
||||
let gallery: string[] = [];
|
||||
|
||||
if (randomData.url.includes("/gallery")) {
|
||||
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 {
|
||||
url = randomData.url;
|
||||
}
|
||||
|
@ -128,6 +130,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
|||
Title: randomData['title'],
|
||||
Ups: randomData['ups'],
|
||||
Url: url,
|
||||
Gallery: gallery,
|
||||
};
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue