Compare commits
1 commit
c4ba04677d
...
647b823d2e
Author | SHA1 | Date | |
---|---|---|---|
647b823d2e |
7 changed files with 49 additions and 101 deletions
|
@ -1,18 +0,0 @@
|
|||
Epic: \
|
||||
Story Points:
|
||||
|
||||
---
|
||||
|
||||
*No description*
|
||||
|
||||
## Acceptance Criteria
|
||||
|
||||
*No acceptance criteria*
|
||||
|
||||
## Subtasks
|
||||
|
||||
*No subtasks*
|
||||
|
||||
## Notes
|
||||
|
||||
*No notes*
|
|
@ -1,29 +0,0 @@
|
|||
# Description
|
||||
|
||||
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
|
||||
|
||||
Fixes # (issue)
|
||||
|
||||
## Type of change
|
||||
|
||||
Please delete options that are not relevant.
|
||||
|
||||
- [ ] Bug fix (non-breaking change which fixes an issue)
|
||||
- [ ] New feature (non-breaking change which adds functionality)
|
||||
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
||||
- [ ] This change requires a documentation update
|
||||
|
||||
# How Has This Been Tested?
|
||||
|
||||
Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration.
|
||||
|
||||
# Checklist
|
||||
|
||||
- [ ] My code follows the style guidelines of this project
|
||||
- [ ] I have performed a self-review of my own code
|
||||
- [ ] I have commented my code, particularly in hard-to-understand areas
|
||||
- [ ] I have made corresponding changes to the documentation
|
||||
- [ ] My changes generate no new warnings
|
||||
- [ ] I have added tests that provde my fix is effective or that my feature works
|
||||
- [ ] New and existing unit tests pass locally with my changes
|
||||
- [ ] Any dependent changes have been merged and published in downstream modules
|
4
app.ts
4
app.ts
|
@ -1,7 +1,7 @@
|
|||
import randomBunny from "./dist";
|
||||
import randomBunny from "./src";
|
||||
|
||||
async function app() {
|
||||
const result = await randomBunny('rabbits', 'hot');
|
||||
const result = await randomBunny('rabbits', 'hot', 100);
|
||||
|
||||
console.log(result);
|
||||
}
|
||||
|
|
13
package.json
13
package.json
|
@ -1,11 +1,11 @@
|
|||
{
|
||||
"name": "random-bunny",
|
||||
"version": "2.0.5",
|
||||
"version": "2.0.3",
|
||||
"description": "Get a random subreddit image url",
|
||||
"license": "MIT",
|
||||
"author": "Vylpes",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"main": "./dist/index",
|
||||
"typings": "./dist",
|
||||
"keywords": [
|
||||
"rabbit",
|
||||
"bunny",
|
||||
|
@ -33,12 +33,9 @@
|
|||
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
|
||||
"funding": "https://ko-fi.com/vylpes",
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.0.0",
|
||||
"@types/node": "^16.11.11",
|
||||
"eslint": "^7.17.0",
|
||||
"ts-node": "^10.4.0",
|
||||
"typescript": "^4.5.2"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ yarn add random-bunny
|
|||
import randomBunny from "random-bunny";
|
||||
|
||||
// ... In an async function
|
||||
const result = await randomBunny('rabbits', 'hot');
|
||||
const result = await randomBunny('rabbits', 'hot', 100);
|
||||
console.log(result);
|
||||
```
|
||||
|
||||
|
@ -29,7 +29,7 @@ console.log(result);
|
|||
|
||||
### `randomBunny()`
|
||||
|
||||
Returns a `json string` for a random post. Accepts 2 arguments: `subreddit`, and `sortby` ('new', 'hot', 'top')
|
||||
Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `maxTries?` (default 100)
|
||||
|
||||
The json string which gets returned consists of:
|
||||
- archived
|
||||
|
@ -44,6 +44,8 @@ The json string which gets returned consists of:
|
|||
|
||||
`sortBy` will default to 'hot' if not given or invalid
|
||||
|
||||
`maxTries` prevents the script from rerolling too many times. The script rerolls the randomiser if the post its given doesn't contain an image. Default 100.
|
||||
|
||||
## Notes
|
||||
|
||||
* Node 4 or newer.
|
||||
|
|
50
src/index.ts
50
src/index.ts
|
@ -10,7 +10,7 @@ const sortable = [
|
|||
'top'
|
||||
];
|
||||
|
||||
export default async function randomBunny(subreddit: string, sortBy?: string): Promise<IReturnResult> {
|
||||
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IReturnResult> {
|
||||
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
||||
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
||||
|
@ -30,37 +30,39 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P
|
|||
}
|
||||
|
||||
const data: IFetchResult[] = json.data.children;
|
||||
|
||||
|
||||
const dataWithImages = new List<IFetchResult>(data)
|
||||
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
|
||||
.ToArray();
|
||||
|
||||
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
|
||||
for (let i = 0; i < maxTries; i++) {
|
||||
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
|
||||
|
||||
const randomSelect = dataWithImages[random];
|
||||
const randomSelect = dataWithImages[random];
|
||||
|
||||
if (!randomSelect) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
if (!randomSelect) continue;
|
||||
|
||||
const randomData = randomSelect.data;
|
||||
|
||||
const redditResult: IRedditResult = {
|
||||
Archived: randomData['archived'],
|
||||
Downs: randomData['downs'],
|
||||
Hidden: randomData['hidden'],
|
||||
Permalink: randomData['permalink'],
|
||||
Subreddit: randomData['subreddit'],
|
||||
SubredditSubscribers: randomData['subreddit_subscribers'],
|
||||
Title: randomData['title'],
|
||||
Ups: randomData['ups'],
|
||||
Url: randomData['url']
|
||||
};
|
||||
};
|
||||
|
||||
const randomData = randomSelect.data;
|
||||
|
||||
const redditResult: IRedditResult = {
|
||||
Archived: randomData['archived'],
|
||||
Downs: randomData['downs'],
|
||||
Hidden: randomData['hidden'],
|
||||
Permalink: randomData['permalink'],
|
||||
Subreddit: randomData['subreddit'],
|
||||
SubredditSubscribers: randomData['subreddit_subscribers'],
|
||||
Title: randomData['title'],
|
||||
Ups: randomData['ups'],
|
||||
Url: randomData['url']
|
||||
};
|
||||
return {
|
||||
IsSuccess: true,
|
||||
Result: redditResult
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
IsSuccess: true,
|
||||
Result: redditResult
|
||||
};
|
||||
IsSuccess: false
|
||||
}
|
||||
}
|
30
yarn.lock
30
yarn.lock
|
@ -82,15 +82,10 @@
|
|||
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
|
||||
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==
|
||||
|
||||
"@types/http-cache-semantics@^4.0.1":
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz#0ea7b61496902b95890dc4c3a116b60cb8dae812"
|
||||
integrity sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ==
|
||||
|
||||
"@types/node@^18.0.0":
|
||||
version "18.11.18"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.18.tgz#8dfb97f0da23c2293e554c5a50d61ef134d7697f"
|
||||
integrity sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==
|
||||
"@types/node@^16.11.11":
|
||||
version "16.11.11"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.11.tgz#6ea7342dfb379ea1210835bada87b3c512120234"
|
||||
integrity sha512-KB0sixD67CeecHC33MYn+eYARkqTheIRNuu97y2XMjR7Wu3XibO1vaY6VBV6O/a89SPI81cEUIYT87UqUWlZNw==
|
||||
|
||||
acorn-jsx@^5.3.1:
|
||||
version "5.3.1"
|
||||
|
@ -192,13 +187,12 @@ cacheable-lookup@^7.0.0:
|
|||
integrity sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w==
|
||||
|
||||
cacheable-request@^10.2.1:
|
||||
version "10.2.8"
|
||||
resolved "https://registry.yarnpkg.com/cacheable-request/-/cacheable-request-10.2.8.tgz#899ae6c0c8c7127f263b2005ecaac07c95124079"
|
||||
integrity sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==
|
||||
version "10.2.4"
|
||||
resolved "https://registry.yarnpkg.com/cacheable-request/-/cacheable-request-10.2.4.tgz#9b9700f9f71b281d5f0e99b514fc9a94e3fbb589"
|
||||
integrity sha512-IWIea8ei1Ht4dBqvlvh7Gs7EYlMyBhlJybLDUB9sadEqHqftmdNieMLIR5ia3vs8gbjj9t8hXLBpUVg3vcQNbg==
|
||||
dependencies:
|
||||
"@types/http-cache-semantics" "^4.0.1"
|
||||
get-stream "^6.0.1"
|
||||
http-cache-semantics "^4.1.1"
|
||||
http-cache-semantics "^4.1.0"
|
||||
keyv "^4.5.2"
|
||||
mimic-response "^4.0.0"
|
||||
normalize-url "^8.0.0"
|
||||
|
@ -555,10 +549,10 @@ has-flag@^4.0.0:
|
|||
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
|
||||
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==
|
||||
|
||||
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"
|
||||
integrity sha512-er295DKPVsV82j5kw1Gjt+ADA/XYHsajl82cGNQG2eyoPkvgUhX+nDIyelzhIWbbsXP39EHcI6l5tYs2FYqYXQ==
|
||||
http-cache-semantics@^4.1.0:
|
||||
version "4.1.0"
|
||||
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
|
||||
integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==
|
||||
|
||||
http2-wrapper@^2.1.10:
|
||||
version "2.2.0"
|
||||
|
|
Loading…
Reference in a new issue