From adf3fe0d731bf58cb02f10a6661bffd892ab358c Mon Sep 17 00:00:00 2001 From: maple3142 Date: Tue, 24 Sep 2019 20:40:28 +0800 Subject: [PATCH 1/4] add a utility function makeIterable --- readme.md | 15 +++++++++++++++ src/index.test.ts | 7 +++++++ src/index.ts | 17 +++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/readme.md b/readme.md index 85222dd..8f03acd 100644 --- a/readme.md +++ b/readme.md @@ -80,6 +80,21 @@ See examples. #### nextQuery(): string; +#### makeIterable(resp?: Object): AsyncIterable + +
+ +```js +const json = await pixiv.searchIllust('艦これ10000users入り') +let ar = [] +for await (const r of pixiv.makeIterable(json)){ + ar = ar.concat(r.illusts) +} +console.log(ar.length) +``` + +
+ #### userDetail(id: ID, params?: Object): Promise; #### userIllusts(id: ID, params?: Object): Promise; diff --git a/src/index.test.ts b/src/index.test.ts index 6fa99fb..6fcc46d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -133,3 +133,10 @@ test('not camelcaseKeys', async () => { const json = await pixiv.userIllusts(userId, { userId: 2957827 }) expect({}.hasOwnProperty.call(json, 'next_url')).toBe(false) }) + +test('makeIterable', async () => { + expect.assertions(1) + const json = await pixiv.userIllusts(userId, { userId: 2957827 }) + const iterable = pixiv.makeIterable(json) + expect({}.hasOwnProperty.call(iterable, Symbol.asyncIterator)).toBe(true) +}) diff --git a/src/index.ts b/src/index.ts index 73f3a74..38f6ebe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,6 +114,23 @@ class PixivApp { return url.parse(this.nextUrl, true).params } + makeIterable(resp: object): AsyncIterable { + // eslint-disable-next-line @typescript-eslint/no-this-alias + const self = this + const nextUrl = this.camelcaseKeys ? 'nextUrl' : 'next_url' + return { + async *[Symbol.asyncIterator]() { + yield resp + // eslint-disable-next-line require-atomic-updates + while (resp[nextUrl]) { + // eslint-disable-next-line require-atomic-updates, no-await-in-loop + resp = await self.fetch(resp[nextUrl]) + yield resp + } + } + } + } + userDetail(id: number, params = {}) { params = { user_id: id, From c49da3b8d902eb43be28fba15ec63fe368b1a74f Mon Sep 17 00:00:00 2001 From: maple Date: Thu, 26 Sep 2019 17:30:11 +0800 Subject: [PATCH 2/4] update readme message to warn users --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 8f03acd..a768bad 100644 --- a/readme.md +++ b/readme.md @@ -89,6 +89,7 @@ const json = await pixiv.searchIllust('艦これ10000users入り') let ar = [] for await (const r of pixiv.makeIterable(json)){ ar = ar.concat(r.illusts) + await sleep(1000) // if the request rate is too high, Pixiv might ban you } console.log(ar.length) ``` From 948444c8393aaf8295ff484703785bb0e6d720b0 Mon Sep 17 00:00:00 2001 From: akameco Date: Fri, 27 Sep 2019 11:32:08 +0900 Subject: [PATCH 3/4] Add @maple3142 as contributor --- .all-contributorsrc | 9 +++++++++ readme.md | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 98bd9b6..0fb0186 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -55,6 +55,15 @@ "contributions": [ "code" ] + }, + { + "login": "maple3142", + "name": "maple", + "avatar_url": "https://avatars1.githubusercontent.com/u/9370547?v=4", + "profile": "https://blog.maple3142.net/", + "contributions": [ + "code" + ] } ], "repoType": "github", diff --git a/readme.md b/readme.md index a768bad..50ff4a9 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ # pixiv-app-api [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) -[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-) +[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) > Promise base pixiv API client @@ -544,6 +544,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds Cake
Cake

💻 ⚠️ Ade Firman Fauzi
Ade Firman Fauzi

💻 yeti2018
yeti2018

💻 + maple
maple

💻 From c0a9cc1c2da3c85ec2934476e2e6c885b2e42768 Mon Sep 17 00:00:00 2001 From: akameco Date: Fri, 27 Sep 2019 11:32:46 +0900 Subject: [PATCH 4/4] =?UTF-8?q?docs(readme):=20Pixiv=20=E2=86=92=20pixiv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 50ff4a9..fd60422 100644 --- a/readme.md +++ b/readme.md @@ -89,7 +89,7 @@ const json = await pixiv.searchIllust('艦これ10000users入り') let ar = [] for await (const r of pixiv.makeIterable(json)){ ar = ar.concat(r.illusts) - await sleep(1000) // if the request rate is too high, Pixiv might ban you + await sleep(1000) // if the request rate is too high, pixiv might ban you } console.log(ar.length) ```