Compare commits
13 commits
647b823d2e
...
c4ba04677d
Author | SHA1 | Date | |
---|---|---|---|
c4ba04677d | |||
6ffcb7a6ba | |||
263a84dd4c | |||
3a9d5b3063 | |||
99e6f0e586 | |||
d02c6e7731 | |||
24f9444af6 | |||
948f513b53 | |||
089f693413 | |||
bb9f092731 | |||
1b859351df | |||
3c0a82b5cc | |||
fdd49a94b9 |
7 changed files with 101 additions and 49 deletions
18
.gitea/ISSUE_TEMPLATE.md
Normal file
18
.gitea/ISSUE_TEMPLATE.md
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
Epic: \
|
||||||
|
Story Points:
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*No description*
|
||||||
|
|
||||||
|
## Acceptance Criteria
|
||||||
|
|
||||||
|
*No acceptance criteria*
|
||||||
|
|
||||||
|
## Subtasks
|
||||||
|
|
||||||
|
*No subtasks*
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
*No notes*
|
29
.gitea/PULL_REQUEST_TEMPLATE.md
Normal file
29
.gitea/PULL_REQUEST_TEMPLATE.md
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# 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 "./src";
|
import randomBunny from "./dist";
|
||||||
|
|
||||||
async function app() {
|
async function app() {
|
||||||
const result = await randomBunny('rabbits', 'hot', 100);
|
const result = await randomBunny('rabbits', 'hot');
|
||||||
|
|
||||||
console.log(result);
|
console.log(result);
|
||||||
}
|
}
|
||||||
|
|
13
package.json
13
package.json
|
@ -1,11 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "random-bunny",
|
"name": "random-bunny",
|
||||||
"version": "2.0.3",
|
"version": "2.0.5",
|
||||||
"description": "Get a random subreddit image url",
|
"description": "Get a random subreddit image url",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"author": "Vylpes",
|
"author": "Vylpes",
|
||||||
"main": "./dist/index",
|
"main": "./dist/index.js",
|
||||||
"typings": "./dist",
|
"typings": "./dist/index.d.ts",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"rabbit",
|
"rabbit",
|
||||||
"bunny",
|
"bunny",
|
||||||
|
@ -33,9 +33,12 @@
|
||||||
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
|
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
|
||||||
"funding": "https://ko-fi.com/vylpes",
|
"funding": "https://ko-fi.com/vylpes",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^16.11.11",
|
"@types/node": "^18.0.0",
|
||||||
"eslint": "^7.17.0",
|
"eslint": "^7.17.0",
|
||||||
"ts-node": "^10.4.0",
|
"ts-node": "^10.4.0",
|
||||||
"typescript": "^4.5.2"
|
"typescript": "^4.5.2"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ yarn add random-bunny
|
||||||
import randomBunny from "random-bunny";
|
import randomBunny from "random-bunny";
|
||||||
|
|
||||||
// ... In an async function
|
// ... In an async function
|
||||||
const result = await randomBunny('rabbits', 'hot', 100);
|
const result = await randomBunny('rabbits', 'hot');
|
||||||
console.log(result);
|
console.log(result);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ console.log(result);
|
||||||
|
|
||||||
### `randomBunny()`
|
### `randomBunny()`
|
||||||
|
|
||||||
Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `maxTries?` (default 100)
|
Returns a `json string` for a random post. Accepts 2 arguments: `subreddit`, and `sortby` ('new', 'hot', 'top')
|
||||||
|
|
||||||
The json string which gets returned consists of:
|
The json string which gets returned consists of:
|
||||||
- archived
|
- archived
|
||||||
|
@ -44,8 +44,6 @@ The json string which gets returned consists of:
|
||||||
|
|
||||||
`sortBy` will default to 'hot' if not given or invalid
|
`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
|
## Notes
|
||||||
|
|
||||||
* Node 4 or newer.
|
* Node 4 or newer.
|
||||||
|
|
50
src/index.ts
50
src/index.ts
|
@ -10,7 +10,7 @@ const sortable = [
|
||||||
'top'
|
'top'
|
||||||
];
|
];
|
||||||
|
|
||||||
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IReturnResult> {
|
export default async function randomBunny(subreddit: string, sortBy?: string): Promise<IReturnResult> {
|
||||||
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
||||||
|
|
||||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
||||||
|
@ -30,39 +30,37 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma
|
||||||
}
|
}
|
||||||
|
|
||||||
const data: IFetchResult[] = json.data.children;
|
const data: IFetchResult[] = json.data.children;
|
||||||
|
|
||||||
const dataWithImages = new List<IFetchResult>(data)
|
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'))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
for (let i = 0; i < maxTries; i++) {
|
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
|
||||||
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) 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']
|
|
||||||
};
|
|
||||||
|
|
||||||
|
if (!randomSelect) {
|
||||||
return {
|
return {
|
||||||
IsSuccess: true,
|
IsSuccess: false,
|
||||||
Result: redditResult
|
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
|
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 {
|
return {
|
||||||
IsSuccess: false
|
IsSuccess: true,
|
||||||
}
|
Result: redditResult
|
||||||
|
};
|
||||||
}
|
}
|
30
yarn.lock
30
yarn.lock
|
@ -82,10 +82,15 @@
|
||||||
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
|
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
|
||||||
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==
|
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==
|
||||||
|
|
||||||
"@types/node@^16.11.11":
|
"@types/http-cache-semantics@^4.0.1":
|
||||||
version "16.11.11"
|
version "4.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.11.tgz#6ea7342dfb379ea1210835bada87b3c512120234"
|
resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz#0ea7b61496902b95890dc4c3a116b60cb8dae812"
|
||||||
integrity sha512-KB0sixD67CeecHC33MYn+eYARkqTheIRNuu97y2XMjR7Wu3XibO1vaY6VBV6O/a89SPI81cEUIYT87UqUWlZNw==
|
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==
|
||||||
|
|
||||||
acorn-jsx@^5.3.1:
|
acorn-jsx@^5.3.1:
|
||||||
version "5.3.1"
|
version "5.3.1"
|
||||||
|
@ -187,12 +192,13 @@ cacheable-lookup@^7.0.0:
|
||||||
integrity sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w==
|
integrity sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w==
|
||||||
|
|
||||||
cacheable-request@^10.2.1:
|
cacheable-request@^10.2.1:
|
||||||
version "10.2.4"
|
version "10.2.8"
|
||||||
resolved "https://registry.yarnpkg.com/cacheable-request/-/cacheable-request-10.2.4.tgz#9b9700f9f71b281d5f0e99b514fc9a94e3fbb589"
|
resolved "https://registry.yarnpkg.com/cacheable-request/-/cacheable-request-10.2.8.tgz#899ae6c0c8c7127f263b2005ecaac07c95124079"
|
||||||
integrity sha512-IWIea8ei1Ht4dBqvlvh7Gs7EYlMyBhlJybLDUB9sadEqHqftmdNieMLIR5ia3vs8gbjj9t8hXLBpUVg3vcQNbg==
|
integrity sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==
|
||||||
dependencies:
|
dependencies:
|
||||||
|
"@types/http-cache-semantics" "^4.0.1"
|
||||||
get-stream "^6.0.1"
|
get-stream "^6.0.1"
|
||||||
http-cache-semantics "^4.1.0"
|
http-cache-semantics "^4.1.1"
|
||||||
keyv "^4.5.2"
|
keyv "^4.5.2"
|
||||||
mimic-response "^4.0.0"
|
mimic-response "^4.0.0"
|
||||||
normalize-url "^8.0.0"
|
normalize-url "^8.0.0"
|
||||||
|
@ -549,10 +555,10 @@ has-flag@^4.0.0:
|
||||||
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
|
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
|
||||||
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==
|
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==
|
||||||
|
|
||||||
http-cache-semantics@^4.1.0:
|
http-cache-semantics@^4.1.1:
|
||||||
version "4.1.0"
|
version "4.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
|
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz#abe02fcb2985460bf0323be664436ec3476a6d5a"
|
||||||
integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==
|
integrity sha512-er295DKPVsV82j5kw1Gjt+ADA/XYHsajl82cGNQG2eyoPkvgUhX+nDIyelzhIWbbsXP39EHcI6l5tYs2FYqYXQ==
|
||||||
|
|
||||||
http2-wrapper@^2.1.10:
|
http2-wrapper@^2.1.10:
|
||||||
version "2.2.0"
|
version "2.2.0"
|
||||||
|
|
Loading…
Add table
Reference in a new issue