Merge branch 'develop' into feature/181-q-json-flags
This commit is contained in:
commit
f06ff77bcb
8 changed files with 52 additions and 21 deletions
|
@ -26,6 +26,7 @@ $ random-bunny
|
|||
Archived = false
|
||||
Author = Rabbit_Owner
|
||||
Downvotes = 0
|
||||
Gallery = https://i.redd.it/sfz0srdrimjc1.png, https://i.redd.it/sfz0srdrimjc1.png
|
||||
Hidden = false
|
||||
Permalink = /r/Rabbits/comments/1av1rg9/cute_baby_bun/
|
||||
Subreddit = Rabbits
|
||||
|
@ -35,6 +36,8 @@ Upvotes = 211
|
|||
Url = https://i.redd.it/sfz0srdrimjc1.png
|
||||
```
|
||||
|
||||
- The `Gallery` field is only shown when there is more than 1 image returned, which then the `Url` field is the first image of that list.
|
||||
|
||||
## Help
|
||||
|
||||
The command also includes a help option in case you are stuck.
|
||||
|
@ -72,9 +75,11 @@ $ random-bunny --json
|
|||
|
||||
$ randon-bunny -j
|
||||
|
||||
{"Archived":false,"Author":"Rabbit_Owner","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1av1rg9/cute_baby_bun/","Subreddit":"Rabbits","SubredditSubscribers":486085,"Title":"Cute baby bun","Ups":210,"Url":"https://i.redd.it/sfz0srdrimjc1.png"}
|
||||
{"Archived":false,"Author":"Rabbit_Owner","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1av1rg9/cute_baby_bun/","Subreddit":"Rabbits","SubredditSubscribers":486085,"Title":"Cute baby bun","Ups":210,"Url":"https://i.redd.it/sfz0srdrimjc1.png","Gallery":["https://i.redd.it/sfz0srdrimjc1.png"]}
|
||||
```
|
||||
|
||||
- The `Url` field is the first image of the `Gallery` array
|
||||
|
||||
## Sort
|
||||
|
||||
You can also choose the sorting option which reddit will use to return the available posts to randomise from.
|
||||
|
|
|
@ -39,6 +39,7 @@ The json string which gets returned consists of:
|
|||
- archived
|
||||
- author
|
||||
- downs
|
||||
- gallery
|
||||
- hidden
|
||||
- permalink
|
||||
- subreddit
|
||||
|
|
|
@ -2,11 +2,12 @@ export default interface IRedditResult {
|
|||
Archived: boolean,
|
||||
Author: string,
|
||||
Downs: number,
|
||||
Gallery: string[],
|
||||
Hidden: boolean,
|
||||
Permalink: string,
|
||||
Subreddit: string,
|
||||
SubredditSubscribers: number,
|
||||
Title: string,
|
||||
Ups: number,
|
||||
Url: string
|
||||
Url: string,
|
||||
}
|
|
@ -2,17 +2,19 @@ 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[]> {
|
||||
const fetched = await fetch(url);
|
||||
|
||||
if (!fetched || fetched.errored || fetched.statusCode != 200) {
|
||||
return undefined;
|
||||
return [];
|
||||
}
|
||||
|
||||
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;
|
||||
const imgSrc = img
|
||||
.flatMap(x => x.attributes.find(x => x.name == "src")?.value)
|
||||
.filter(x => x != undefined);
|
||||
|
||||
return imgSrc;
|
||||
}
|
||||
|
|
|
@ -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,9 +113,11 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
|||
}
|
||||
}
|
||||
|
||||
url = galleryImage;
|
||||
url = galleryImage[0];
|
||||
gallery = galleryImage;
|
||||
} else {
|
||||
url = randomData.url;
|
||||
gallery = [randomData.url];
|
||||
}
|
||||
|
||||
const redditResult: IRedditResult = {
|
||||
|
@ -128,6 +131,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
|
|||
Title: randomData['title'],
|
||||
Ups: randomData['ups'],
|
||||
Url: url,
|
||||
Gallery: gallery,
|
||||
};
|
||||
|
||||
return {
|
||||
|
|
|
@ -13,7 +13,7 @@ Ups = 17
|
|||
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"}"`;
|
||||
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.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format 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","subreddit":"rabbits","sortBy":"hot","limit":100}"`;
|
||||
|
||||
|
@ -32,3 +32,17 @@ subreddit = rabbits
|
|||
sortBy = hot
|
||||
limit = 100"
|
||||
`;
|
||||
|
||||
exports[`GenerateOutput GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added 1`] = `
|
||||
"Archived = false
|
||||
Author = author
|
||||
Downvotes = 0
|
||||
Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d, https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
|
||||
Hidden = false
|
||||
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
|
||||
Subreddit = Rabbits
|
||||
Subreddit Subscribers = 654751
|
||||
Title = This is my Ms Bear!
|
||||
Upvotes = 17
|
||||
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
|
||||
`;
|
||||
|
|
|
@ -17,18 +17,19 @@ describe("FetchImageFromRedditGallery", () => {
|
|||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
expect(fetchMock).toHaveBeenCalledWith("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBe("https://preview.redd.it/image.png");
|
||||
expect(result.length).toBe(1);
|
||||
expect(result[0]).toBe("https://preview.redd.it/image.png");
|
||||
});
|
||||
|
||||
test("GIVEN fetch is unable to return data, EXPECT undefined returned", async () => {
|
||||
test("GIVEN fetch is unable to return data, EXPECT empty array returned", async () => {
|
||||
fetchMock.mockResolvedValue(null);
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
|
||||
test("GIVEN fetch is an error, EXPECT undefined returned", async () => {
|
||||
test("GIVEN fetch is an error, EXPECT empty array returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
||||
errored: "Error",
|
||||
|
@ -37,10 +38,10 @@ describe("FetchImageFromRedditGallery", () => {
|
|||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
|
||||
test("GIVEN fetch is not status code of 200, EXPECT undefined returned", async () => {
|
||||
test("GIVEN fetch is not status code of 200, EXPECT empty array returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
||||
errored: undefined,
|
||||
|
@ -49,10 +50,10 @@ describe("FetchImageFromRedditGallery", () => {
|
|||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
|
||||
test("GIVEN image tag is not found, EXPECT undefined returned", async () => {
|
||||
test("GIVEN image tag is not found, EXPECT empty array returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body></body></html>",
|
||||
errored: undefined,
|
||||
|
@ -61,10 +62,10 @@ describe("FetchImageFromRedditGallery", () => {
|
|||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
|
||||
test("GIVEN image source attribute is not found, EXPECT undefined returned", async () => {
|
||||
test("GIVEN image source attribute is not found, EXPECT empty array returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img /></body></html>",
|
||||
errored: undefined,
|
||||
|
@ -73,10 +74,10 @@ describe("FetchImageFromRedditGallery", () => {
|
|||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
|
||||
test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT undefined returned", async () => {
|
||||
test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT empty array returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img src='main.png' /></body></html>",
|
||||
errored: undefined,
|
||||
|
@ -85,6 +86,6 @@ describe("FetchImageFromRedditGallery", () => {
|
|||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
});
|
|
@ -23,6 +23,7 @@ describe("GenerateOutput", () => {
|
|||
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"],
|
||||
},
|
||||
} as IReturnResult;
|
||||
|
||||
|
@ -55,6 +56,7 @@ describe("GenerateOutput", () => {
|
|||
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"],
|
||||
},
|
||||
} as IReturnResult;
|
||||
|
||||
|
@ -89,6 +91,7 @@ describe("GenerateOutput", () => {
|
|||
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"],
|
||||
},
|
||||
} as IReturnResult;
|
||||
|
||||
|
|
Loading…
Reference in a new issue