Skip to content

Commit

Permalink
Merge pull request #5 from hendraaagil/hospital-type-2
Browse files Browse the repository at this point in the history
chore: Update hospitals data API
  • Loading branch information
satyawikananda authored Jul 11, 2021
2 parents 33ff4ba + 860200f commit 33cd252
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
1 change: 0 additions & 1 deletion handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
71 changes: 51 additions & 20 deletions src/scrape/hospitals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,26 +20,32 @@ export const getHospitalList = async ({
const hospitals: Array<HospitalsList> = []

$(".row > .cardRS").each((_, el) => {
const beds: Array<BedsList> = []

const name: string = $(el).data("string") as string
const getPhone: string =
$(el).find(".card-footer > div > span").text().trim().replace(" ", "") ??
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 = +$(el).find(".card-body .col-md-5 > p > b").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)")
Expand All @@ -54,15 +61,39 @@ export const getHospitalList = async ({
? +getQueue.replace(/[^\d]/gi, " ")
: 0

hospitals.push({
id,
name,
address,
phone,
queue,
bed_availability,
info
})
if (type == 2) {
$(el).find(".col-md-4").each((_, el) => {
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: totalBeds,
bed_class: bedClass,
room_name: roomName,
info,
})
})
hospitals.push({
id,
name,
address,
phone,
available_beds: beds,
info
})
} else {
hospitals.push({
id,
name,
address,
phone,
queue,
bed_availability,
info
})
}

})
return {
status,
Expand Down
14 changes: 11 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ export interface ParamHospital {
}

export interface HospitalsList {
id: string
name: string
address: string | null
phone: string | null
bed_availability: number
bed_availability?: number
available_beds?: BedsList[] | null
info: string | null
id: string
queue: number
queue?: number
}

export interface BedsList {
available: number
bed_class: string
room_name: string
info: string
}

export interface ResponseHopitalsList {
Expand Down

1 comment on commit 33cd252

@vercel
Copy link

@vercel vercel bot commented on 33cd252 Jul 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.