From 06925012d02cdd35205c0b6e3207e9d18385d65a Mon Sep 17 00:00:00 2001 From: hendraaagil Date: Sat, 10 Jul 2021 17:48:20 +0700 Subject: [PATCH 1/6] feat: update hospitals data for type 2 --- handler.ts | 1 - src/scrape/hospitals.ts | 50 ++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/handler.ts b/handler.ts index 60d95e9..bd1c95c 100644 --- a/handler.ts +++ b/handler.ts @@ -24,7 +24,6 @@ export const handler: Handler = async (req, res, next) => { provinceid: provinceid as string, cityid: cityid as string, }); - if(type == "2") return res.end("API sedang dalam tahap perbaikan"); return res.end(JSON.stringify(data)); } diff --git a/src/scrape/hospitals.ts b/src/scrape/hospitals.ts index 515de00..80bc627 100644 --- a/src/scrape/hospitals.ts +++ b/src/scrape/hospitals.ts @@ -25,20 +25,48 @@ export const getHospitalList = async ({ null const getAddress: string = $(el) .find( - `${ - type == 1 ? ".card-body .col-md-7 > p" : ".card-body .col-md-5 > p" - }`, + `${type == 1 ? ".card-body .col-md-7 > p" : ".card-body .col-md-5 > p"}` ) .text() .trim() - const bed_availability: number = +$(el) - .find(".card-body .col-md-5 > p > b") - .text() - .trim() - const getInfo: string = $(el) - .find(".card-body .col-md-5 > p:nth-child(4)") - .text() - .trim() + const bed_availability: number = + type == 1 + ? ( + +$(el) + .find(".card-body .col-md-5 > p > b") + .text() + .trim() + ) + : ( + (+$(el) + .find('.card-body .col-md-7 .col-md-4:nth-child(1) .card-body > .text-center:nth-child(1)') + .text() + .trim() + ) + + (+$(el) + .find('.card-body .col-md-7 .col-md-4:nth-child(2) .card-body > .text-center:nth-child(1)') + .text() + .trim() + ) + + (+$(el) + .find('.card-body .col-md-7 .col-md-4:nth-child(3) .card-body > .text-center:nth-child(1)') + .text() + .trim() + ) + ) + const getInfo: string = + type == 1 + ? ( + $(el) + .find(".card-body .col-md-5 > p:nth-child(4)") + .text() + .trim() + ) : ( + $(el) + .find(".card-body .col-md-7 .col-md-4:nth-child(1) .card-footer > .text-center") + .text() + .trim() + ) const getId: string = ($(el).find(".card-footer > div > a").attr("href") as string) const getQueue: string = $(el) .find(".card-body .col-md-5 > p:nth-child(3)") From 742df3ccbd92f183911cbe74d7da92d21b29f636 Mon Sep 17 00:00:00 2001 From: hendraaagil Date: Sun, 11 Jul 2021 12:30:54 +0700 Subject: [PATCH 2/6] feat: try to get bed data on each class card --- src/scrape/hospitals.ts | 48 +++++++++++++++++++---------------------- src/types/index.ts | 8 +++++++ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/scrape/hospitals.ts b/src/scrape/hospitals.ts index 80bc627..18f24ef 100644 --- a/src/scrape/hospitals.ts +++ b/src/scrape/hospitals.ts @@ -2,6 +2,7 @@ import qs from "query-string" import { scrapeSite } from "~/utils/scrape-site" import { capitalizeStr } from "~/utils/capitalize-str" import type { + BedsList, HospitalsList, ParamHospital, ResponseHopitalsList, @@ -18,7 +19,9 @@ export const getHospitalList = async ({ const RE_NUMBER = /\d/ const hospitals: Array = [] - $(".row > .cardRS").each((_, el) => { + $(".row > .cardRS").each((index, el) => { + const beds: Array = [] + const name: string = $(el).data("string") as string const getPhone: string = $(el).find(".card-footer > div > span").text().trim().replace(" ", "") ?? @@ -29,31 +32,7 @@ export const getHospitalList = async ({ ) .text() .trim() - const bed_availability: number = - type == 1 - ? ( - +$(el) - .find(".card-body .col-md-5 > p > b") - .text() - .trim() - ) - : ( - (+$(el) - .find('.card-body .col-md-7 .col-md-4:nth-child(1) .card-body > .text-center:nth-child(1)') - .text() - .trim() - ) + - (+$(el) - .find('.card-body .col-md-7 .col-md-4:nth-child(2) .card-body > .text-center:nth-child(1)') - .text() - .trim() - ) + - (+$(el) - .find('.card-body .col-md-7 .col-md-4:nth-child(3) .card-body > .text-center:nth-child(1)') - .text() - .trim() - ) - ) + const bed_availability: number = +$(el).find(".card-body .col-md-5 > p > b").text().trim() const getInfo: string = type == 1 ? ( @@ -82,6 +61,22 @@ export const getHospitalList = async ({ ? +getQueue.replace(/[^\d]/gi, " ") : 0 + if (type == 2) { + $(`.col-md-4.text-center.mb-2:nth-child(${index + 1})`).each((_, el) => { + const available: number = +$(el).find('.card-body > .text-center:nth-child(1)').text().trim() + const bedClass: string = $(el).find('.card-body > .text-center:nth-child(2)').text().trim() + const getInfo: string = $(el).find('.card-footer > .text-center:nth-child(1)').text().trim() + const info: string = capitalizeStr(getInfo) + + beds.push({ + hospital_id: id, + available, + bed_class: bedClass, + info + }) + }) + } + hospitals.push({ id, name, @@ -89,6 +84,7 @@ export const getHospitalList = async ({ phone, queue, bed_availability, + beds, info }) }) diff --git a/src/types/index.ts b/src/types/index.ts index 2486e44..d87595e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -23,11 +23,19 @@ export interface HospitalsList { address: string | null phone: string | null bed_availability: number + beds: BedsList[] | null info: string | null id: string queue: number } +export interface BedsList { + hospital_id: string + available: number + bed_class: string + info: string +} + export interface ResponseHopitalsList { status: number hospitals: HospitalsList[] From ee4d7c37bd087c87c01850fd2a42f23ef6316213 Mon Sep 17 00:00:00 2001 From: hendraaagil Date: Sun, 11 Jul 2021 17:43:52 +0700 Subject: [PATCH 3/6] feat: update hospitals data for type 2 --- src/scrape/hospitals.ts | 34 ++++++++++++++++++++++------------ src/types/index.ts | 1 - 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/scrape/hospitals.ts b/src/scrape/hospitals.ts index 18f24ef..a408c74 100644 --- a/src/scrape/hospitals.ts +++ b/src/scrape/hospitals.ts @@ -19,7 +19,7 @@ export const getHospitalList = async ({ const RE_NUMBER = /\d/ const hospitals: Array = [] - $(".row > .cardRS").each((index, el) => { + $(".row > .cardRS").each((_, el) => { const beds: Array = [] const name: string = $(el).data("string") as string @@ -62,18 +62,28 @@ export const getHospitalList = async ({ : 0 if (type == 2) { - $(`.col-md-4.text-center.mb-2:nth-child(${index + 1})`).each((_, el) => { - const available: number = +$(el).find('.card-body > .text-center:nth-child(1)').text().trim() - const bedClass: string = $(el).find('.card-body > .text-center:nth-child(2)').text().trim() - const getInfo: string = $(el).find('.card-footer > .text-center:nth-child(1)').text().trim() - const info: string = capitalizeStr(getInfo) + const available1: number = +$(el).find('.col-md-4:nth-child(1) .card-body > .text-center:nth-child(1)').text().trim() + const bedClass1: string = $(el).find('.col-md-4:nth-child(1) .card-body > .text-center:nth-child(2)').text().trim() + const getInfo1: string = $(el).find('.col-md-4:nth-child(1) .card-footer > .text-center:nth-child(1)').text().trim() + const available2: number = +$(el).find('.col-md-4:nth-child(2) .card-body > .text-center:nth-child(1)').text().trim() + const bedClass2: string = $(el).find('.col-md-4:nth-child(2) .card-body > .text-center:nth-child(2)').text().trim() + const getInfo2: string = $(el).find('.col-md-4:nth-child(2) .card-footer > .text-center:nth-child(1)').text().trim() + const available3: number = +$(el).find('.col-md-4:nth-child(3) .card-body > .text-center:nth-child(1)').text().trim() + const bedClass3: string = $(el).find('.col-md-4:nth-child(3) .card-body > .text-center:nth-child(2)').text().trim() + const getInfo3: string = $(el).find('.col-md-4:nth-child(3) .card-footer > .text-center:nth-child(1)').text().trim() - beds.push({ - hospital_id: id, - available, - bed_class: bedClass, - info - }) + beds.push({ + available: available1, + bed_class: bedClass1, + info: getInfo1 + }, { + available: available2, + bed_class: bedClass2, + info: getInfo2 + }, { + available: available3, + bed_class: bedClass3, + info: getInfo3 }) } diff --git a/src/types/index.ts b/src/types/index.ts index d87595e..ab8dc32 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -30,7 +30,6 @@ export interface HospitalsList { } export interface BedsList { - hospital_id: string available: number bed_class: string info: string From 563108dd67064b4f5dac0cd78f1e02337906f6d0 Mon Sep 17 00:00:00 2001 From: hendraaagil Date: Sun, 11 Jul 2021 18:20:25 +0700 Subject: [PATCH 4/6] fix: capitalize info value --- src/scrape/hospitals.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scrape/hospitals.ts b/src/scrape/hospitals.ts index a408c74..b05e945 100644 --- a/src/scrape/hospitals.ts +++ b/src/scrape/hospitals.ts @@ -75,15 +75,15 @@ export const getHospitalList = async ({ beds.push({ available: available1, bed_class: bedClass1, - info: getInfo1 + info: capitalizeStr(getInfo1) }, { available: available2, bed_class: bedClass2, - info: getInfo2 + info: capitalizeStr(getInfo2) }, { available: available3, bed_class: bedClass3, - info: getInfo3 + info: capitalizeStr(getInfo3) }) } From c1c76bfa567d5722cf5cf03e7f906a21f627a3bd Mon Sep 17 00:00:00 2001 From: satyawikananda Date: Sun, 11 Jul 2021 21:12:37 +0800 Subject: [PATCH 5/6] chore: update structure data of type non covid --- src/scrape/hospitals.ts | 59 +++++++++++++++++++---------------------- src/types/index.ts | 8 +++--- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/scrape/hospitals.ts b/src/scrape/hospitals.ts index b05e945..f5651c6 100644 --- a/src/scrape/hospitals.ts +++ b/src/scrape/hospitals.ts @@ -62,41 +62,36 @@ export const getHospitalList = async ({ : 0 if (type == 2) { - const available1: number = +$(el).find('.col-md-4:nth-child(1) .card-body > .text-center:nth-child(1)').text().trim() - const bedClass1: string = $(el).find('.col-md-4:nth-child(1) .card-body > .text-center:nth-child(2)').text().trim() - const getInfo1: string = $(el).find('.col-md-4:nth-child(1) .card-footer > .text-center:nth-child(1)').text().trim() - const available2: number = +$(el).find('.col-md-4:nth-child(2) .card-body > .text-center:nth-child(1)').text().trim() - const bedClass2: string = $(el).find('.col-md-4:nth-child(2) .card-body > .text-center:nth-child(2)').text().trim() - const getInfo2: string = $(el).find('.col-md-4:nth-child(2) .card-footer > .text-center:nth-child(1)').text().trim() - const available3: number = +$(el).find('.col-md-4:nth-child(3) .card-body > .text-center:nth-child(1)').text().trim() - const bedClass3: string = $(el).find('.col-md-4:nth-child(3) .card-body > .text-center:nth-child(2)').text().trim() - const getInfo3: string = $(el).find('.col-md-4:nth-child(3) .card-footer > .text-center:nth-child(1)').text().trim() - - beds.push({ - available: available1, - bed_class: bedClass1, - info: capitalizeStr(getInfo1) - }, { - available: available2, - bed_class: bedClass2, - info: capitalizeStr(getInfo2) - }, { - available: available3, - bed_class: bedClass3, - info: capitalizeStr(getInfo3) + $(el).find(".col-md-4").each((_, el) => { + const room: number = +$(el).find(".card-body > div:nth-child(1)").text().trim() + const bedClass: string = $(el).find(".card-body > div:nth-child(2)").text().trim() + const info: string = capitalizeStr($(el).find(".card-footer > div:nth-child(1)").text().trim()) + beds.push({ + available: room, + bed_class: bedClass, + info, + }) + }) + hospitals.push({ + id, + name, + address, + phone, + beds, + info + }) + } else { + hospitals.push({ + id, + name, + address, + phone, + queue, + bed_availability, + info }) } - hospitals.push({ - id, - name, - address, - phone, - queue, - bed_availability, - beds, - info - }) }) return { status, diff --git a/src/types/index.ts b/src/types/index.ts index ab8dc32..cd52989 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -19,14 +19,14 @@ export interface ParamHospital { } export interface HospitalsList { + id: string name: string address: string | null phone: string | null - bed_availability: number - beds: BedsList[] | null + bed_availability?: number + beds?: BedsList[] | null info: string | null - id: string - queue: number + queue?: number } export interface BedsList { From 860200feca96ecea3acf538690664f0ff06ad3e0 Mon Sep 17 00:00:00 2001 From: satyawikananda Date: Sun, 11 Jul 2021 22:20:20 +0800 Subject: [PATCH 6/6] chore: add room name data in type non covid --- src/scrape/hospitals.ts | 8 +++++--- src/types/index.ts | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/scrape/hospitals.ts b/src/scrape/hospitals.ts index f5651c6..5d9e477 100644 --- a/src/scrape/hospitals.ts +++ b/src/scrape/hospitals.ts @@ -63,12 +63,14 @@ export const getHospitalList = async ({ if (type == 2) { $(el).find(".col-md-4").each((_, el) => { - const room: number = +$(el).find(".card-body > div:nth-child(1)").text().trim() + const totalBeds: number = +$(el).find(".card-body > div:nth-child(1)").text().trim() const bedClass: string = $(el).find(".card-body > div:nth-child(2)").text().trim() + const roomName: string = $(el).find(".card-body > div:nth-child(3)").text().trim() const info: string = capitalizeStr($(el).find(".card-footer > div:nth-child(1)").text().trim()) beds.push({ - available: room, + available: totalBeds, bed_class: bedClass, + room_name: roomName, info, }) }) @@ -77,7 +79,7 @@ export const getHospitalList = async ({ name, address, phone, - beds, + available_beds: beds, info }) } else { diff --git a/src/types/index.ts b/src/types/index.ts index cd52989..80da799 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -24,7 +24,7 @@ export interface HospitalsList { address: string | null phone: string | null bed_availability?: number - beds?: BedsList[] | null + available_beds?: BedsList[] | null info: string | null queue?: number } @@ -32,6 +32,7 @@ export interface HospitalsList { export interface BedsList { available: number bed_class: string + room_name: string info: string }