Compare commits

..

No commits in common. "81437ede419f761eeb389f0bac31ddccb53a796d" and "db35dee5f91cc8aa13ef9206b9636e76fbec4c84" have entirely different histories.

7 changed files with 20 additions and 39 deletions

View file

@ -8,6 +8,5 @@ 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,23 +2,17 @@ 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[]> { 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) {
return []; return undefined;
} }
const dom = htmlparser.parseDocument(fetched.body); const dom = htmlparser.parseDocument(fetched.body);
const img = htmlparser.DomUtils.findAll((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children); const img = htmlparser.DomUtils.findOne((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children, true);
if (!img) { const imgSrc = img?.attributes.find(x => x.name == "src")?.value;
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,10 +22,6 @@ 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,7 +93,6 @@ 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);
@ -113,11 +112,9 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
} }
} }
url = galleryImage[0]; url = galleryImage;
gallery = galleryImage;
} else { } else {
url = randomData.url; url = randomData.url;
gallery = [randomData.url];
} }
const redditResult: IRedditResult = { const redditResult: IRedditResult = {
@ -131,7 +128,6 @@ 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 {

View file

@ -13,7 +13,7 @@ Upvotes = 17
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
`; `;
exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d","Gallery":["https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"]}"`; exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"}"`;
exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = ` exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = `
"Archived = false "Archived = false

View file

@ -17,19 +17,18 @@ describe("FetchImageFromRedditGallery", () => {
expect(fetchMock).toHaveBeenCalledTimes(1); expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith("https://redd.it/gallery/image"); expect(fetchMock).toHaveBeenCalledWith("https://redd.it/gallery/image");
expect(result.length).toBe(1); expect(result).toBe("https://preview.redd.it/image.png");
expect(result[0]).toBe("https://preview.redd.it/image.png");
}); });
test("GIVEN fetch is unable to return data, EXPECT empty array returned", async () => { test("GIVEN fetch is unable to return data, EXPECT undefined returned", async () => {
fetchMock.mockResolvedValue(null); fetchMock.mockResolvedValue(null);
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
expect(result.length).toBe(0); expect(result).toBeUndefined();
}); });
test("GIVEN fetch is an error, EXPECT empty array returned", async () => { test("GIVEN fetch is an error, EXPECT undefined returned", async () => {
fetchMock.mockResolvedValue({ fetchMock.mockResolvedValue({
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>", body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
errored: "Error", errored: "Error",
@ -38,10 +37,10 @@ describe("FetchImageFromRedditGallery", () => {
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
expect(result.length).toBe(0); expect(result).toBeUndefined();
}); });
test("GIVEN fetch is not status code of 200, EXPECT empty array returned", async () => { test("GIVEN fetch is not status code of 200, EXPECT undefined returned", async () => {
fetchMock.mockResolvedValue({ fetchMock.mockResolvedValue({
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>", body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
errored: undefined, errored: undefined,
@ -50,10 +49,10 @@ describe("FetchImageFromRedditGallery", () => {
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
expect(result.length).toBe(0); expect(result).toBeUndefined();
}); });
test("GIVEN image tag is not found, EXPECT empty array returned", async () => { test("GIVEN image tag is not found, EXPECT undefined returned", async () => {
fetchMock.mockResolvedValue({ fetchMock.mockResolvedValue({
body: "<html><body></body></html>", body: "<html><body></body></html>",
errored: undefined, errored: undefined,
@ -62,10 +61,10 @@ describe("FetchImageFromRedditGallery", () => {
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
expect(result.length).toBe(0); expect(result).toBeUndefined();
}); });
test("GIVEN image source attribute is not found, EXPECT empty array returned", async () => { test("GIVEN image source attribute is not found, EXPECT undefined returned", async () => {
fetchMock.mockResolvedValue({ fetchMock.mockResolvedValue({
body: "<html><body><img /></body></html>", body: "<html><body><img /></body></html>",
errored: undefined, errored: undefined,
@ -74,10 +73,10 @@ describe("FetchImageFromRedditGallery", () => {
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
expect(result.length).toBe(0); expect(result).toBeUndefined();
}); });
test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT empty array returned", async () => { test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT undefined returned", async () => {
fetchMock.mockResolvedValue({ fetchMock.mockResolvedValue({
body: "<html><body><img src='main.png' /></body></html>", body: "<html><body><img src='main.png' /></body></html>",
errored: undefined, errored: undefined,
@ -86,6 +85,6 @@ describe("FetchImageFromRedditGallery", () => {
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
expect(result.length).toBe(0); expect(result).toBeUndefined();
}); });
}); });

View file

@ -23,7 +23,6 @@ describe("GenerateOutput", () => {
Title: "This is my Ms Bear!", Title: "This is my Ms Bear!",
Ups: 17, Ups: 17,
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d", Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
Gallery: ["https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"],
}, },
} as IReturnResult; } as IReturnResult;
@ -56,7 +55,6 @@ describe("GenerateOutput", () => {
Title: "This is my Ms Bear!", Title: "This is my Ms Bear!",
Ups: 17, Ups: 17,
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d", Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
Gallery: ["https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"],
}, },
} as IReturnResult; } as IReturnResult;
@ -91,7 +89,6 @@ describe("GenerateOutput", () => {
Title: "This is my Ms Bear!", Title: "This is my Ms Bear!",
Ups: 17, Ups: 17,
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d", Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
Gallery: ["https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"],
}, },
} as IReturnResult; } as IReturnResult;