Compare commits

..

No commits in common. "develop" and "master" have entirely different histories.

11 changed files with 358 additions and 410 deletions

View file

@ -9,12 +9,13 @@ The project can be downloaded as a binary for your system via the [GitHub Releas
We currently support:
- Linux (x64)
- Windows (x64)
- macOS (x64, Arm64\*)
The git repository can also be cloned and ran via `yarn build` and `yarn start`.
You can produce the binary using the `yarn package` command. This creates the binaries in the `./bin` folder.
> **NOTE:** As of version 2.4 I will no longer be supporting and building macOS builds as I have no capacity to test and build for it.
> **NOTE:** We are aware of a bug in the macOS Arm64 builds failing to execute. For now you're still able to use the x64 builds under Rosetta fine. This will hopefully be fixed in a future release.
## Default Output
@ -26,7 +27,6 @@ $ 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
@ -36,8 +36,6 @@ 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.
@ -75,11 +73,9 @@ $ 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","Gallery":["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"}
```
- 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.

View file

@ -43,20 +43,24 @@
"@eslint/js": "^9.8.0",
"@types/eslint": "^9.6.0",
"@types/jest": "^29.5.8",
"@types/node": "^22.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"@yao-pkg/pkg": "^5.12.0",
"eslint": "^9.8.0",
"jest": "^29.7.0",
"jest-mock-extended": "^3.0.7",
"jest-mock-extended": "^3.0.3",
"np": "^10.0.0",
"ts-jest": "^29.1.1",
"ts-mockito": "^2.6.1",
"typescript": "^5.0.0",
"typescript-eslint": "^7.18.0"
},
"resolutions": {},
"resolutions": {
"np/**/got": "^14.0.0",
"**/semver": "^7.5.2",
"@babel/traverse": "^7.23.2"
},
"files": [
"dist"
],
@ -64,6 +68,8 @@
"scripts": "dist/**/*.js",
"targets": [
"latest-linux-x64",
"latest-macos-x64",
"latest-macos-arm64",
"latest-win-x64"
],
"outputPath": "bin"

View file

@ -39,7 +39,6 @@ The json string which gets returned consists of:
- archived
- author
- downs
- gallery
- hidden
- permalink
- subreddit

View file

@ -2,12 +2,11 @@ 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
}

View file

@ -2,19 +2,17 @@ import fetch from "got-cjs";
import * as htmlparser from "htmlparser2";
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);
if (!fetched || fetched.errored || fetched.statusCode != 200) {
return [];
return undefined;
}
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);
const imgSrc = img
.flatMap(x => x.attributes.find(x => x.name == "src")?.value)
.filter(x => x != undefined);
const imgSrc = img?.attributes.find(x => x.name == "src")?.value;
return imgSrc;
}

View file

@ -14,13 +14,7 @@ export default class OutputHelper {
outputLines.push(`Archived = ${result.Archived}`);
outputLines.push(`Author = ${result.Author}`);
outputLines.push(`Downvotes = ${result.Downs}`);
if (result.Gallery.length > 1) {
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
}
outputLines.push(`Hidden = ${result.Hidden}`);
outputLines.push(`Permalink = ${result.Permalink}`);
outputLines.push(`Subreddit = ${result.Subreddit}`);
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
@ -28,7 +22,6 @@ export default class OutputHelper {
outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`);
if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
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;
let url: string;
let gallery: string[];
if (randomData.url.includes("/gallery")) {
const galleryImage = await ImageHelper.FetchImageFromRedditGallery(randomData.url);
@ -113,11 +112,9 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
}
}
url = galleryImage[0];
gallery = galleryImage;
url = galleryImage;
} else {
url = randomData.url;
gallery = [randomData.url];
}
const redditResult: IRedditResult = {
@ -131,7 +128,6 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
Title: randomData['title'],
Ups: randomData['ups'],
Url: url,
Gallery: gallery,
};
return {

View file

@ -13,7 +13,7 @@ Upvotes = 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.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`] = `
"Archived = false
@ -30,17 +30,3 @@ Query.Subreddit = rabbits
Query.Sort By = hot
Query.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"
`;

View file

@ -17,19 +17,18 @@ describe("FetchImageFromRedditGallery", () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith("https://redd.it/gallery/image");
expect(result.length).toBe(1);
expect(result[0]).toBe("https://preview.redd.it/image.png");
expect(result).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);
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({
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
errored: "Error",
@ -38,10 +37,10 @@ describe("FetchImageFromRedditGallery", () => {
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({
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
errored: undefined,
@ -50,10 +49,10 @@ describe("FetchImageFromRedditGallery", () => {
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({
body: "<html><body></body></html>",
errored: undefined,
@ -62,10 +61,10 @@ describe("FetchImageFromRedditGallery", () => {
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({
body: "<html><body><img /></body></html>",
errored: undefined,
@ -74,10 +73,10 @@ describe("FetchImageFromRedditGallery", () => {
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({
body: "<html><body><img src='main.png' /></body></html>",
errored: undefined,
@ -86,6 +85,6 @@ describe("FetchImageFromRedditGallery", () => {
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!",
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;
@ -56,7 +55,6 @@ 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;
@ -91,7 +89,6 @@ 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;
@ -105,40 +102,4 @@ describe("GenerateOutput", () => {
// Assert
expect(result).toMatchSnapshot();
});
test("GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added", () => {
// Arrange
const response = {
IsSuccess: true,
Query: {
subreddit: "rabbits",
sortBy: "hot",
limit: 100,
},
Result: {
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",
"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
],
},
} as IReturnResult;
const options = {} as ICliOptions;
// Act
const result = OutputHelper.GenerateOutput(response, options);
// Assert
expect(result).toMatchSnapshot();
});
});

633
yarn.lock

File diff suppressed because it is too large Load diff