From b802a4ff3dfe7a0a6a9d34faea0bd699c03f54d7 Mon Sep 17 00:00:00 2001 From: CirnoBakaBOT Date: Sat, 15 May 2021 11:38:52 +0800 Subject: [PATCH 1/4] fix aggregate --- source/@declares/global.d.ts | 4 +- source/corefunc/config.ts | 13 ++- source/modules/arcfetch/arcapi.aggregate.ts | 106 ++++++++++++++------ 3 files changed, 88 insertions(+), 35 deletions(-) diff --git a/source/@declares/global.d.ts b/source/@declares/global.d.ts index fd3351b..e0c56cf 100644 --- a/source/@declares/global.d.ts +++ b/source/@declares/global.d.ts @@ -10,12 +10,14 @@ declare var BOTARCAPI_FORWARD_TIMESEC_DEFAULT: number; declare var BOTARCAPI_FORWARD_FEED_MAX: number; declare var BOTARCAPI_USERBEST_HISTORY_MAX: number; declare var BOTARCAPI_USERBEST_HISTORY_DEFAULT: number; +declare var BOTARCAPI_AGGREGATE_LIMITATION: number; +declare var BOTARCAPI_AGGREGATE_ENABLED: boolean; +declare var BOTARCAPI_AGGREGATE_CONCURRENT: boolean; declare var ARCAPI_RETRY: number; declare var ARCAPI_VERSION: number; declare var ARCAPI_APPVERSION: string; declare var ARCAPI_USERAGENT: string; -declare var ARCAPI_AGGREGATE_LIMITATION: number; declare var ARCAPI_URL: string; declare var DATABASE_PATH: string; diff --git a/source/corefunc/config.ts b/source/corefunc/config.ts index aaab7ad..f718297 100644 --- a/source/corefunc/config.ts +++ b/source/corefunc/config.ts @@ -42,12 +42,23 @@ const _default_config: any = { 'BOTARCAPI_USERBEST_HISTORY_MAX': 20, 'BOTARCAPI_USERBEST_HISTORY_DEFAULT': 7, + // aggregate limit + 'BOTARCAPI_AGGREGATE_LIMITATION': 6, + + // sending only one 'compose/aggregate' request instead + // of send a huge of requests to arcapi. + // this feature is for 3.6.0 arcapi + 'BOTARCAPI_AGGREGATE_ENABLED': false, + + // send a huge of requests concurrently + // only valid on 'BOTARCAPI_AGGREGATE_ENABLED' set to false + 'BOTARCAPI_AGGREGATE_CONCURRENT': true, + // arcaea api config 'ARCAPI_RETRY': 3, 'ARCAPI_VERSION': 14, 'ARCAPI_APPVERSION': '3.6.0c', 'ARCAPI_USERAGENT': 'Grievous Lady (Linux; U; Android 2.3.3; BotArcAPI)', - 'ARCAPI_AGGREGATE_LIMITATION': 6, 'ARCAPI_URL': 'https://arcapi.lowiro.com/blockchain', // path to database folder diff --git a/source/modules/arcfetch/arcapi.aggregate.ts b/source/modules/arcfetch/arcapi.aggregate.ts index 567e874..1a12730 100644 --- a/source/modules/arcfetch/arcapi.aggregate.ts +++ b/source/modules/arcfetch/arcapi.aggregate.ts @@ -3,54 +3,94 @@ const TAG: string = 'arcapi.aggregate.ts'; import syslog from '../syslog/syslog'; import arcfetch, { ArcFetchRequest, ArcFetchMethod } from './arcfetch'; import IArcAccount from './interfaces/IArcAccount'; +import arcapi_any from './arcapi.any'; export default (account: IArcAccount, endpoints: Array) => { - - return new Promise((resolve, reject) => { + + return new Promise(async (resolve, reject) => { // account will be BANNED from server if exceed - if (endpoints.length > ARCAPI_AGGREGATE_LIMITATION) + if (endpoints.length > BOTARCAPI_AGGREGATE_LIMITATION) return reject(new Error('Endpoints limit exceeded')); - // construct endpoint object - const _endpoints: Array = []; - endpoints.forEach((element, index) => { - _endpoints.push({ endpoint: element, id: index }); - }); - - // construct remote request - const _remote_request = - new ArcFetchRequest(ArcFetchMethod.GET, 'compose/aggregate', { - deviceId: account.device, - userToken: account.token, - submitData: new URLSearchParams({ 'calls': JSON.stringify(_endpoints) }) + if (BOTARCAPI_AGGREGATE_ENABLED) { + + // construct endpoint object + const _endpoints: Array = []; + endpoints.forEach((element, index) => { + _endpoints.push({ endpoint: element, id: index }); }); - // send request - arcfetch(_remote_request) - .then((root: any) => { + // construct remote request + const _remote_request = + new ArcFetchRequest(ArcFetchMethod.GET, 'compose/aggregate', { + deviceId: account.device, + userToken: account.token, + submitData: new URLSearchParams({ 'calls': JSON.stringify(_endpoints) }) + }); + + // send request + arcfetch(_remote_request) + .then((root: any) => { + + // teardown the object and pack data into array + const _data: Array = []; + root.value.forEach((element: any) => { + _data[element.id] = element.value[0]; + }) - // teardown the object and pack data into array - const _data: Array = []; - root.value.forEach((element: any) => { - _data[element.id] = element.value[0]; + resolve(_data); }) + .catch((e: string) => { + + // if token is invalid + // just erase the token and wait for + // auto login in next time allocating + if (e == 'UnauthorizedError') { + account.token = ''; + syslog.w(TAG, `Invalid token => ${account.name} ${account.token}`); + } + + reject(e); + + }); - resolve(_data); - }) - .catch((e: string) => { + } else { + if (BOTARCAPI_AGGREGATE_CONCURRENT) { - // if token is invalid - // just erase the token and wait for - // auto login in next time allocating - if (e == 'UnauthorizedError') { - account.token = ''; - syslog.w(TAG, `Invalid token => ${account.name} ${account.token}`); + // construct tasks + const _tasks: Array> = []; + endpoints.forEach((element, index) => { + _tasks.push(new Promise(async (resolve, reject) => { + resolve(await arcapi_any(account, ArcFetchMethod.GET, element, "")); + })); + }); + + // wait for data coming in + Promise.all(_tasks) + .then(data => { + let _results: any[] = []; + + data.forEach((element, index) => + _results.push(element.value[0])); + + resolve(_results); + }); + + } else { + + let _results: any[] = []; + + // request one by one + for (let i = 0; i < endpoints.length; ++i) { + const _data: any = await arcapi_any(account, ArcFetchMethod.GET, endpoints[i], ""); + _results.push(_data.value[0]); } - reject(e); + resolve(_results); + } - }); + } }); From 89e24941dfea648fe80fb084cf37f140c6d26379 Mon Sep 17 00:00:00 2001 From: CirnoBakaBOT Date: Sat, 15 May 2021 11:39:13 +0800 Subject: [PATCH 2/4] fix user best30 --- source/publicapi/v4/user/best.ts | 2 +- source/publicapi/v4/user/best30.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/publicapi/v4/user/best.ts b/source/publicapi/v4/user/best.ts index 42cda26..5fea5b8 100644 --- a/source/publicapi/v4/user/best.ts +++ b/source/publicapi/v4/user/best.ts @@ -125,7 +125,7 @@ export default (argument: any): Promise => { // get rank result try { - _arc_ranklist = await arcapi_rank_friend(_arc_account, _arc_songid, _arc_difficulty, 0, 1); + _arc_ranklist = await arcapi_rank_friend(_arc_account, _arc_songid, _arc_difficulty); } catch (e) { syslog.e(TAG, e.stack); throw new APIError(-18, 'internal error occurred'); } if (!_arc_ranklist.length) diff --git a/source/publicapi/v4/user/best30.ts b/source/publicapi/v4/user/best30.ts index 6a6a392..08f54e3 100644 --- a/source/publicapi/v4/user/best30.ts +++ b/source/publicapi/v4/user/best30.ts @@ -205,7 +205,7 @@ const do_fetch_userbest30 = const _endpoints: Array = []; for (let j = 0; j < 6; ++j) { const v = _chartheap[i * 6 + j]; - _endpoints.push(`/score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=1`); + _endpoints.push(`score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=11`); } // send request @@ -247,7 +247,7 @@ const do_fetch_userbest30 = // fill the endpoints and chartheap const v: any = _arc_chartlist.shift(); - _endpoints.push(`/score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=1`); + _endpoints.push(`score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=11`); _chartheap.push(v); } else break; From 6a2be7b32ac0e7baeb2c75f571c3e2ebb570af08 Mon Sep 17 00:00:00 2001 From: CirnoBakaBOT Date: Sat, 15 May 2021 11:40:43 +0800 Subject: [PATCH 3/4] 0.3.2 --- docker-compose.yml | 2 +- package-lock.json | 2 +- package.json | 2 +- source/corefunc/config.ts | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 1344abf..ca35e42 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,7 @@ services: BOTARCAPI_CONFIG: | { "SERVER_PORT": 80 - "BOTARCAPI_VERSTR": "v0.3.1", + "BOTARCAPI_VERSTR": "v0.3.2", "ARCAPI_VERSION": 14, "ARCAPI_APPVERSION": "3.6.0c", "ARCAPI_USERAGENT": "Grievous Lady (Linux; U; Android 2.3.3; BotArcAPI)", diff --git a/package-lock.json b/package-lock.json index d0b2121..964eb9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "botarcapi", - "version": "0.3.1", + "version": "0.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7840644..dade80d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "botarcapi", - "version": "0.3.1", + "version": "0.3.2", "description": "A fast and convenient Arcaea API for your bot.", "dependencies": { "abab": "^2.0.3", diff --git a/source/corefunc/config.ts b/source/corefunc/config.ts index f718297..f9f4eec 100644 --- a/source/corefunc/config.ts +++ b/source/corefunc/config.ts @@ -6,8 +6,8 @@ const _default_config: any = { // botarcapi version 'BOTARCAPI_MAJOR': 0, 'BOTARCAPI_MINOR': 3, - 'BOTARCAPI_VERSION': 1, - 'BOTARCAPI_VERSTR': 'BotArcAPI v0.3.1', + 'BOTARCAPI_VERSION': 2, + 'BOTARCAPI_VERSTR': 'BotArcAPI v0.3.2', // useragent whitelist // if set '[]' will accept all requests From 2634e73401a94993c5c64f71131b3138c6f4debd Mon Sep 17 00:00:00 2001 From: CirnoBakaBOT Date: Sat, 15 May 2021 12:00:38 +0800 Subject: [PATCH 4/4] update config --- source/corefunc/config.ts | 2 +- source/modules/arcfetch/arcapi.aggregate.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/corefunc/config.ts b/source/corefunc/config.ts index f9f4eec..7792c1e 100644 --- a/source/corefunc/config.ts +++ b/source/corefunc/config.ts @@ -52,7 +52,7 @@ const _default_config: any = { // send a huge of requests concurrently // only valid on 'BOTARCAPI_AGGREGATE_ENABLED' set to false - 'BOTARCAPI_AGGREGATE_CONCURRENT': true, + 'BOTARCAPI_AGGREGATE_CONCURRENT': false, // arcaea api config 'ARCAPI_RETRY': 3, diff --git a/source/modules/arcfetch/arcapi.aggregate.ts b/source/modules/arcfetch/arcapi.aggregate.ts index 1a12730..acbca0d 100644 --- a/source/modules/arcfetch/arcapi.aggregate.ts +++ b/source/modules/arcfetch/arcapi.aggregate.ts @@ -75,7 +75,8 @@ export default (account: IArcAccount, endpoints: Array) => { _results.push(element.value[0])); resolve(_results); - }); + + }).catch((e: string) => reject(e)); } else {