Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Southcoast Health scraper #274

Merged
merged 7 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions site-scrapers/SouthcoastHealth/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const entityName = "Southcoast Health";

const sites = [
{
name: "Southcoast Health (Fall River)",
street: "20 Star Street",
city: "Fall River",
zip: "02724",
signUpLink:
"https://www.southcoast.org/covid-19-vaccine-scheduling/#/slot-search/covid/resource-type/98C5A8BE-25D1-4125-9AD5-1EE64AD164D2",
},
{
name: "Southcoast Health (North Dartmouth)",
street: "375 Faunce Corner Road",
city: "North Dartmouth",
zip: "02747",
signUpLink:
"https://www.southcoast.org/covid-19-vaccine-scheduling/#/slot-search/covid/resource-type/98C5A8BE-25D1-4125-9AD5-1EE64AD164D2",
},
{
name: "Southcoast Health (Wareham)",
street: "48 Marion Road",
city: "Wareham",
zip: "02571",
signUpLink:
"https://www.southcoast.org/covid-19-vaccine-scheduling/#/slot-search/covid/resource-type/98C5A8BE-25D1-4125-9AD5-1EE64AD164D2",
},
];

const siteUrl =
"https://www.southcoast.org/covid-19-vaccine-scheduling/#/slot-search/covid/resource-type/98C5A8BE-25D1-4125-9AD5-1EE64AD164D2";
module.exports = {
entityName,
sites,
siteUrl,
};
111 changes: 111 additions & 0 deletions site-scrapers/SouthcoastHealth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const { siteUrl, sites, entityName } = require("./config");
const { parseJson } = require("./responseParser.js");
const moment = require("moment");

module.exports = async function GetAvailableAppointments(browser) {
console.log(`${entityName} starting.`);
const siteData = await ScrapeWebsiteData(browser, sites);
console.log(`${entityName} done.`);
return {
parentLocationName: entityName,
timestamp: moment().format(),
individualLocationData: siteData,
};
};

/**
*
* @param {*} browser
* @param {*} pageService
* @param {*} sites
* @returns array of site details and availability
*/
async function ScrapeWebsiteData(browser, sites) {
const page = await browser.newPage();

const submitButtonSelector = "button.btn.btn-default.btn-lg.center-block";
await Promise.all([
page.goto(siteUrl),
await page.waitForSelector(submitButtonSelector, {
visible: true,
timeout: 15000,
}),
]);

await page.evaluate((submitButtonSelector) => {
document.querySelector(submitButtonSelector).click();
}, submitButtonSelector);

await page.waitForSelector("#oas-scheduler");

const siteData = [];

for (const site of sites) {
const availabilityContainer = await getAvailabilityForSite(page, site);

siteData.push({
...site,
availability: availabilityContainer,
hasAvailability: Object.keys(availabilityContainer).length > 0,
});
}

page.close();
return siteData;
}

async function getAvailabilityForSite(page, site) {
const responseJson = await fetchDataForSite(page, site);
const availabilityContainer = parseJson(responseJson);
return availabilityContainer;
}

async function fetchDataForSite(page, site) {
const location = site.city;

// 2021-05-08T02:34:12.058Z
const startDateStr = moment().toISOString();
// 2021-05-21T02:34:12.058Z 13 days
const endDateStr = moment().add(13, "days").toISOString();

const response = await page.evaluate(
async (location, startDateStr, endDateStr) => {
const json = await fetch(
"https://southcoastapps.southcoast.org/OnlineAppointmentSchedulingApi/api/resourceTypes/slots/search",
{
headers: {
accept: "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/json",
"sec-ch-ua":
'" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
sessiontoken: "5d886340-71e9-4bd2-9449-80d5eb3263e9",
},
referrer: "https://www.southcoast.org/",
referrerPolicy: "strict-origin-when-cross-origin",
body: `{"ProviderCriteria":{"SpecialtyID":null,"ConcentrationID":null},"ResourceTypeId":"98C5A8BE-25D1-4125-9AD5-1EE64AD164D2","StartDate":"${startDateStr}","EndDate":"${endDateStr}","Location":"${location}"}`,
method: "POST",
mode: "cors",
credentials: "omit",
}
)
.then((res) => res.json())
.then((json) => {
return json;
})
.catch((error) =>
console.log(`error fetching site data: ${error}`)
);
return json;
},
location,
startDateStr,
endDateStr
);

return response;
}
43 changes: 43 additions & 0 deletions site-scrapers/SouthcoastHealth/responseParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Functionality here is unit testable.
*/
/**
* Sample data format:
{...
"dateToSlots": {
"2021-05-08": {},
"2021-05-09": {},
"2021-05-10": {
"e01e1f56-0029-4256-be54-a8f2a33d6011": {
"slots": [...]
}
}, ...
*
* @param {JSON} json
* @returns if no slots -> {}; otherwise { MM-DD-YYYY: { numberAvailableAppointments: x, hasAvailability: y}, ... }
*/
function parseJson(json) {
const dateToSlots = json.dateToSlots;

if (!dateToSlots) {
return {};
}
const availabilityContainer = {};
// Otherwise, get dates with slots: keys are dates
for (const [date, slotsObj] of Object.entries(dateToSlots)) {
const slots = Object.values(slotsObj);

if (slots.length > 0) {
availabilityContainer[date] = {
numberAvailableAppointments: slots[0].slots.length,
hasAvailability: true,
};
}
}

return availabilityContainer;
}

module.exports = {
parseJson,
};
Loading