Compare commits
6 commits
b188c71e35
...
b1e1a7ff65
Author | SHA1 | Date | |
---|---|---|---|
b1e1a7ff65 | |||
f7667fe3b7 | |||
5d0af43b6f | |||
8a726b386e | |||
9e25e5a0e0 | |||
00b51584b6 |
7 changed files with 261 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "random-bunny",
|
||||
"version": "2.1.5",
|
||||
"version": "2.1.6",
|
||||
"description": "Get a random subreddit image url",
|
||||
"license": "MIT",
|
||||
"author": "Vylpes",
|
||||
|
@ -21,6 +21,7 @@
|
|||
"commander": "^12.0.0",
|
||||
"glob-parent": "^6.0.0",
|
||||
"got-cjs": "^12.5.4",
|
||||
"htmlparser2": "^9.1.0",
|
||||
"linqts": "^1.14.4"
|
||||
},
|
||||
"scripts": {
|
||||
|
|
19
src/imageHelper.ts
Normal file
19
src/imageHelper.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
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);
|
||||
|
||||
if (!fetched || fetched.errored || fetched.statusCode != 200) {
|
||||
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;
|
||||
}
|
||||
}
|
29
src/index.ts
29
src/index.ts
|
@ -5,6 +5,7 @@ import { List } from 'linqts';
|
|||
import IFetchResult from "./contracts/IFetchResult";
|
||||
import { ErrorCode } from "./constants/ErrorCode";
|
||||
import ErrorMessages from "./constants/ErrorMessages";
|
||||
import ImageHelper from "./imageHelper";
|
||||
|
||||
const sortable = [
|
||||
'new',
|
||||
|
@ -56,7 +57,7 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
const data: IFetchResult[] = json.data.children;
|
||||
|
||||
const dataWithImages = new List<IFetchResult>(data)
|
||||
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
|
||||
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png') || x!.data.url.includes("/gallery/"))
|
||||
.ToArray();
|
||||
|
||||
let random = 0;
|
||||
|
@ -81,6 +82,30 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
|
||||
const randomData = randomSelect.data;
|
||||
|
||||
let url: string;
|
||||
|
||||
if (randomData.url.includes("/gallery")) {
|
||||
const galleryImage = await ImageHelper.FetchImageFromRedditGallery(randomData.url);
|
||||
|
||||
if (!galleryImage) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
Query: {
|
||||
subreddit: subreddit,
|
||||
sortBy: sortBy,
|
||||
},
|
||||
Error: {
|
||||
Code: ErrorCode.NoImageResultsFound,
|
||||
Message: ErrorMessages.NoImageResultsFound,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
url = galleryImage;
|
||||
} else {
|
||||
url = randomData.url;
|
||||
}
|
||||
|
||||
const redditResult: IRedditResult = {
|
||||
Archived: randomData['archived'],
|
||||
Downs: randomData['downs'],
|
||||
|
@ -90,7 +115,7 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
SubredditSubscribers: randomData['subreddit_subscribers'],
|
||||
Title: randomData['title'],
|
||||
Ups: randomData['ups'],
|
||||
Url: randomData['url']
|
||||
Url: url,
|
||||
};
|
||||
|
||||
return {
|
||||
|
|
|
@ -147,7 +147,7 @@ describe('query-metadata', () => {
|
|||
expect(query).toBeUndefined();
|
||||
}, 5000);
|
||||
|
||||
test('GIVEN --query-metadata is not supplied, EXPECT no query metadata returned', async () => {
|
||||
test('GIVEN --query-metadata is supplied, EXPECT query metadata returned', async () => {
|
||||
const result = await cli(['--query-metadata'], '.');
|
||||
|
||||
const query = result.stdout.split('\n')
|
||||
|
@ -156,7 +156,7 @@ describe('query-metadata', () => {
|
|||
expect(query).toBeDefined();
|
||||
}, 5000);
|
||||
|
||||
test('GIVEN -q is not supplied, EXPECT no query metadata returned', async () => {
|
||||
test('GIVEN -q is supplied, EXPECT query metadata returned', async () => {
|
||||
const result = await cli(['-q'], '.');
|
||||
|
||||
const query = result.stdout.split('\n')
|
||||
|
|
90
tests/imageHelper.test.ts
Normal file
90
tests/imageHelper.test.ts
Normal file
|
@ -0,0 +1,90 @@
|
|||
import ImageHelper from "../src/imageHelper";
|
||||
import fetch from "got-cjs";
|
||||
|
||||
jest.mock('got-cjs');
|
||||
const fetchMock = jest.mocked(fetch);
|
||||
|
||||
describe("FetchImageFromRedditGallery", () => {
|
||||
test("EXPECT image url to be returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
||||
errored: undefined,
|
||||
statusCode: 200,
|
||||
});
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
expect(fetchMock).toHaveBeenCalledWith("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBe("https://preview.redd.it/image.png");
|
||||
});
|
||||
|
||||
test("GIVEN fetch is unable to return data, EXPECT undefined returned", async () => {
|
||||
fetchMock.mockResolvedValue(null);
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
test("GIVEN fetch is an error, EXPECT undefined returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
||||
errored: "Error",
|
||||
statusCode: 200,
|
||||
});
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
test("GIVEN fetch is not status code of 200, EXPECT undefined returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
||||
errored: undefined,
|
||||
statusCode: 500,
|
||||
});
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
test("GIVEN image tag is not found, EXPECT undefined returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body></body></html>",
|
||||
errored: undefined,
|
||||
statusCode: 200,
|
||||
});
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
test("GIVEN image source attribute is not found, EXPECT undefined returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img /></body></html>",
|
||||
errored: undefined,
|
||||
statusCode: 200,
|
||||
});
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT undefined returned", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: "<html><body><img src='main.png' /></body></html>",
|
||||
errored: undefined,
|
||||
statusCode: 200,
|
||||
});
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
|
@ -1,5 +1,6 @@
|
|||
import { ErrorCode } from "../src/constants/ErrorCode";
|
||||
import ErrorMessages from "../src/constants/ErrorMessages";
|
||||
import ImageHelper from "../src/imageHelper";
|
||||
import randomBunny from "../src/index";
|
||||
import fetch from "got-cjs";
|
||||
|
||||
|
@ -189,5 +190,77 @@ describe('randomBunny', () => {
|
|||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
});
|
||||
|
||||
test("GIVEN data fetched is a gallery AND an image is returned from the helper, EXPECT this to be used", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: JSON.stringify({
|
||||
data: {
|
||||
children: [
|
||||
{
|
||||
data: {
|
||||
archived: false,
|
||||
downs: 0,
|
||||
hidden: false,
|
||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
||||
subreddit: 'Rabbits',
|
||||
subreddit_subscribers: 298713,
|
||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
||||
ups: 1208,
|
||||
url: 'https://i.redd.it/gallery/cr8xudsnkgua1',
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
ImageHelper.FetchImageFromRedditGallery = jest.fn().mockResolvedValue("https://i.redd.it/cr8xudsnkgua1.jpg")
|
||||
|
||||
const result = await randomBunny('rabbits', 'new');
|
||||
|
||||
expect(result.IsSuccess).toBeTruthy();
|
||||
expect(result.Result).toBeDefined();
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
|
||||
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1);
|
||||
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledWith("https://i.redd.it/gallery/cr8xudsnkgua1");
|
||||
});
|
||||
|
||||
test("GIVEN data fetched is a gallery AND an image is not returned from the helper, EXPECT error", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
body: JSON.stringify({
|
||||
data: {
|
||||
children: [
|
||||
{
|
||||
data: {
|
||||
archived: false,
|
||||
downs: 0,
|
||||
hidden: false,
|
||||
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
|
||||
subreddit: 'Rabbits',
|
||||
subreddit_subscribers: 298713,
|
||||
title: 'Someone told pickles it’s Monday… *internal fury*',
|
||||
ups: 1208,
|
||||
url: 'https://i.redd.it/gallery/cr8xudsnkgua1',
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
||||
ImageHelper.FetchImageFromRedditGallery = jest.fn().mockResolvedValue(undefined)
|
||||
|
||||
const result = await randomBunny('rabbits', 'new');
|
||||
|
||||
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(result.IsSuccess).toBe(false);
|
||||
expect(result.Error).toBeDefined();
|
||||
expect(result.Error?.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||
expect(result.Error?.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||
});
|
||||
});
|
51
yarn.lock
51
yarn.lock
|
@ -835,9 +835,9 @@
|
|||
"@babel/types" "^7.20.7"
|
||||
|
||||
"@types/eslint@^8.21.1":
|
||||
version "8.56.7"
|
||||
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.56.7.tgz#c33b5b5a9cfb66881beb7b5be6c34aa3e81d3366"
|
||||
integrity sha512-SjDvI/x3zsZnOkYZ3lCt9lOZWZLB2jIlNKz+LBgCtDurK0JZcwucxYHn1w2BJkD34dgX9Tjnak0txtq4WTggEA==
|
||||
version "8.56.9"
|
||||
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.56.9.tgz#403e9ced04a34e63f1c383c5b8ee1a94442c8cc4"
|
||||
integrity sha512-W4W3KcqzjJ0sHg2vAq9vfml6OhsJ53TcUjUqfzzZf/EChUtwspszj/S0pzMxnfRcO55/iGq47dscXw71Fxc4Zg==
|
||||
dependencies:
|
||||
"@types/estree" "*"
|
||||
"@types/json-schema" "*"
|
||||
|
@ -1883,6 +1883,36 @@ doctrine@^3.0.0:
|
|||
dependencies:
|
||||
esutils "^2.0.2"
|
||||
|
||||
dom-serializer@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-2.0.0.tgz#e41b802e1eedf9f6cae183ce5e622d789d7d8e53"
|
||||
integrity sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==
|
||||
dependencies:
|
||||
domelementtype "^2.3.0"
|
||||
domhandler "^5.0.2"
|
||||
entities "^4.2.0"
|
||||
|
||||
domelementtype@^2.3.0:
|
||||
version "2.3.0"
|
||||
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-2.3.0.tgz#5c45e8e869952626331d7aab326d01daf65d589d"
|
||||
integrity sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==
|
||||
|
||||
domhandler@^5.0.2, domhandler@^5.0.3:
|
||||
version "5.0.3"
|
||||
resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-5.0.3.tgz#cc385f7f751f1d1fc650c21374804254538c7d31"
|
||||
integrity sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==
|
||||
dependencies:
|
||||
domelementtype "^2.3.0"
|
||||
|
||||
domutils@^3.1.0:
|
||||
version "3.1.0"
|
||||
resolved "https://registry.yarnpkg.com/domutils/-/domutils-3.1.0.tgz#c47f551278d3dc4b0b1ab8cbb42d751a6f0d824e"
|
||||
integrity sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==
|
||||
dependencies:
|
||||
dom-serializer "^2.0.0"
|
||||
domelementtype "^2.3.0"
|
||||
domhandler "^5.0.3"
|
||||
|
||||
dot-prop@^6.0.1:
|
||||
version "6.0.1"
|
||||
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-6.0.1.tgz#fc26b3cf142b9e59b74dbd39ed66ce620c681083"
|
||||
|
@ -1934,6 +1964,11 @@ end-of-stream@^1.1.0, end-of-stream@^1.4.1:
|
|||
dependencies:
|
||||
once "^1.4.0"
|
||||
|
||||
entities@^4.2.0, entities@^4.5.0:
|
||||
version "4.5.0"
|
||||
resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48"
|
||||
integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==
|
||||
|
||||
error-ex@^1.3.1:
|
||||
version "1.3.2"
|
||||
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
|
||||
|
@ -2578,6 +2613,16 @@ html-escaper@^2.0.0:
|
|||
resolved "https://registry.yarnpkg.com/html-escaper/-/html-escaper-2.0.2.tgz#dfd60027da36a36dfcbe236262c00a5822681453"
|
||||
integrity sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==
|
||||
|
||||
htmlparser2@^9.1.0:
|
||||
version "9.1.0"
|
||||
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-9.1.0.tgz#cdb498d8a75a51f739b61d3f718136c369bc8c23"
|
||||
integrity sha512-5zfg6mHUoaer/97TxnGpxmbR7zJtPwIYFMZ/H5ucTlPZhKvtum05yiPK3Mgai3a0DyVxv7qYqoweaEd2nrYQzQ==
|
||||
dependencies:
|
||||
domelementtype "^2.3.0"
|
||||
domhandler "^5.0.3"
|
||||
domutils "^3.1.0"
|
||||
entities "^4.5.0"
|
||||
|
||||
http-cache-semantics@^4.0.0, http-cache-semantics@^4.1.1:
|
||||
version "4.1.1"
|
||||
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz#abe02fcb2985460bf0323be664436ec3476a6d5a"
|
||||
|
|
Loading…
Reference in a new issue