2024-04-07 11:03:20 +01:00
|
|
|
import fetch from "got-cjs";
|
|
|
|
import * as htmlparser from "htmlparser2";
|
|
|
|
|
|
|
|
export default class ImageHelper {
|
|
|
|
public static async FetchImageFromRedditGallery(url: string): Promise<string | undefined> {
|
|
|
|
const fetched = await fetch(url);
|
|
|
|
|
2024-04-12 17:54:12 +01:00
|
|
|
if (!fetched || fetched.errored || fetched.statusCode != 200) {
|
2024-04-07 11:03:20 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 imgSrc = img?.attributes.find(x => x.name == "src")?.value;
|
|
|
|
|
|
|
|
return imgSrc;
|
|
|
|
}
|
|
|
|
}
|