Skip to content

Commit

Permalink
Add org
Browse files Browse the repository at this point in the history
  • Loading branch information
TaYaKi71751 committed Jan 5, 2022
1 parent fa50004 commit c529f7e
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 72 deletions.
116 changes: 44 additions & 72 deletions src/Example.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,55 @@
import {
authRequest,
standbyRequest,
infoRequest
} from '@corcc/nvr';
import { LightResponse } from '@corcc/nvr/lib/util/type';

import { confirmRequest, errorRequest, failureRequest, progressRequest, successRequest } from '@corcc/nvr';
import { config } from 'dotenv';
import { randomNumber } from './util/Random';
import { getVaccinesFromResponseBody } from './util/Response';
import { vaccineQuantity } from './info/Request';
config();
async function followRedirect ({
res

export async function reservationSubmit ({
org,
key,
vaccines
}: {
res: any
}): Promise<LightResponse | any> {
if (res.responseCode == 200) {
const { location } = res;
if (!location) {
let result = await res;
result = getVaccinesFromResponseBody(res);
return result;
}
}
if (res.responseCode == 302) {
const u: any = new URL('https://example.com');
u.params = { '': '' };
let _res: LightResponse = {
responseCode: 0,
headers: {},
body: '',
location: u
};
const { key } = res.location.params;
if (res.location.pathname.indexOf('auth') > -1) {
_res = await authRequest({
key
});
}
if (res.location.pathname.indexOf('info') > -1) {
_res = await infoRequest({
key
});
}
if (_res.responseCode == 0) {
throw new Error();
}
return await followRedirect({
res: _res
org: string,
key: string,
vaccines: any
}): Promise<any> {
config();
const name = process.env.VACCINE_NAME;
let { cd }: any = vaccines.filter((vaccine: any) => (vaccine.name.indexOf(name) == 0));
cd = cd ? cd[0] : cd;
const progressResult = await progressRequest({
key,
cd
});
const progressResponseCode = progressResult.responseCode;
const confirmResult = await (async function (r: number) {
switch ((r / 100)) {
case 2: return await confirmRequest({
key,
cd
});
}
}

async function vaccineQuantity () {
const standByResponse: LightResponse = await standbyRequest({
orgCd: process.env.orgCd,
sid: process.env.sid
default: throw new Error();
}
})(Number(progressResponseCode));
const { code } = JSON.parse(confirmResult.body);
switch (code) {
case 'success': return successRequest({
key,
cd
});
let result = await followRedirect({
res: standByResponse
case 'failure': return failureRequest({
key,
cd,
code
});
case 'undefined': return errorRequest({
key,
cd
});
const filterAvailable:Function = function (r:any):any {
const _r = r;
_r.vaccines = _r.vaccines.filter((_: any) => (!_.disabled));
return _r;
};
result = filterAvailable(result);
let infoResponse: any;
while (!result.vaccines.length) {
const twoSec: number = 2000;
const randomTwoSec:number = twoSec + randomNumber(randomNumber(0x7FF));
await new Promise(resolve => setTimeout(resolve, randomTwoSec));
infoResponse = await infoRequest({
key: result.key
});
result = getVaccinesFromResponseBody(infoResponse);
result = filterAvailable(result);
}
return result;
}

(async function () {
await vaccineQuantity();
const vaccinesQuantities = await vaccineQuantity();
const reservationResult = await reservationSubmit(vaccinesQuantities);
})();
79 changes: 79 additions & 0 deletions src/info/Request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
authRequest,
standbyRequest,
infoRequest
} from '@corcc/nvr';
import { LightResponse } from '@corcc/nvr/lib/util/type';
import { config } from 'dotenv';
import { randomNumber } from '../util/Random';
import { getVaccinesFromResponseBody } from '../util/Response';

async function followRedirect ({
res
}: {
res: any
}): Promise<LightResponse | any> {
if (res.responseCode == 200) {
const { location } = res;
if (!location) {
let result = await res;
result = getVaccinesFromResponseBody(res);
return result;
}
}
if (res.responseCode == 302) {
const u: any = new URL('https://example.com');
u.params = { '': '' };
let _res: LightResponse = {
responseCode: 0,
headers: {},
body: '',
location: u
};
const { key } = res.location.params;
if (res.location.pathname.indexOf('auth') > -1) {
_res = await authRequest({
key
});
}
if (res.location.pathname.indexOf('info') > -1) {
_res = await infoRequest({
key
});
}
if (_res.responseCode == 0) {
throw new Error();
}
return await followRedirect({
res: _res
});
}
}

export async function vaccineQuantity () {
const standByResponse: LightResponse = await standbyRequest({
orgCd: process.env.orgCd,
sid: process.env.sid
});
let result = await followRedirect({
res: standByResponse
});
const filterAvailable:Function = function (r:any):any {
const _r = r;
_r.vaccines = _r.vaccines.filter((_: any) => (!_.disabled));
return _r;
};
result = filterAvailable(result);
let infoResponse: any;
while (!result.vaccines.length) {
const twoSec: number = 2000;
const randomTwoSec:number = twoSec + randomNumber(randomNumber(0x7FF));
await new Promise(resolve => setTimeout(resolve, randomTwoSec));
infoResponse = await infoRequest({
key: result.key
});
result = getVaccinesFromResponseBody(infoResponse);
result = filterAvailable(result);
}
return result;
}
Empty file added src/submit/Request.ts
Empty file.
9 changes: 9 additions & 0 deletions src/util/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export function getDataKeyFromResponseBody ({
body,
headers
}: LightResponse): string {
if (responseCode != 200) {
throw new Error();
}
const infoCheerio: CheerioAPI = loadCheerio(body);
return infoCheerio('[data-key]').attr()['data-key'];
}
Expand All @@ -18,11 +21,16 @@ export function getVaccinesFromResponseBody ({
body,
headers
}: LightResponse): {
org: string,
key: string,
vaccines: any
} {
if (responseCode != 200) {
throw new Error();
}
const infoCheerio: CheerioAPI = loadCheerio(body);
const vaccineRadioItems = infoCheerio('ul > li.radio_item');
const org = infoCheerio('.h_title > span').text().trim();
const vaccines: any = ov(vaccineRadioItems).filter((_) =>
(typeof _.attribs != 'undefined')).map((vaccineRadioItem): {
[x: string]: string | {
Expand Down Expand Up @@ -51,6 +59,7 @@ export function getVaccinesFromResponseBody ({
return vaccine;
});
return {
org,
key: infoCheerio('[data-key]').attr()['data-key'],
vaccines
};
Expand Down

0 comments on commit c529f7e

Please sign in to comment.