Last active
August 31, 2020 14:05
-
-
Save tchayen/dde13d3320cf557d2f7da0cba59c5541 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require("node-fetch"); | |
const SERVICE_ID = "e5e795f2-7f35-4b11-ba7d-d1c45cbec182"; | |
const YEAR = 2020; | |
const getWeekNumber = (date) => { | |
const firstDayOfYear = new Date(date.getFullYear(), 0, 1); | |
const pastDaysOfYear = (date - firstDayOfYear) / 86400000; | |
return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7); | |
}; | |
process.env.TZ = "Europe/Helsinki"; | |
const obtainSessionId = async () => { | |
const session = await fetch( | |
"https://migri.vihta.com/public/migri/api/sessions" | |
); | |
const response = await session.json(); | |
return response.id; | |
}; | |
const getLocalities = async (token) => { | |
const response = await fetch( | |
"https://migri.vihta.com/public/migri/api/services/localities", | |
{ | |
headers: { | |
accept: "application/json, text/plain, */*", | |
"accept-language": "en,pl-PL;q=0.9,pl;q=0.8,en-US;q=0.7", | |
"content-type": "application/json;charset=UTF-8", | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-origin", | |
"vihta-session": token, | |
}, | |
referrer: "https://migri.vihta.com/public/migri/", | |
referrerPolicy: "no-referrer-when-downgrade", | |
body: JSON.stringify({ | |
serviceSelections: [{ values: [SERVICE_ID] }], | |
extraServices: [], | |
}), | |
method: "POST", | |
mode: "cors", | |
} | |
); | |
return await response.json(); | |
}; | |
const checkSlots = async (token, week, office) => { | |
const response = await fetch( | |
`https://migri.vihta.com/public/migri/api/scheduling/offices/${office}/${YEAR}/w${week}?end_hours=24&start_hours=0`, | |
{ | |
headers: { | |
accept: "application/json, text/plain, */*", | |
"accept-language": "en,pl-PL;q=0.9,pl;q=0.8,en-US;q=0.7", | |
"content-type": "application/json;charset=UTF-8", | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-origin", | |
"vihta-session": token, | |
}, | |
referrer: "https://migri.vihta.com/public/migri/", | |
referrerPolicy: "no-referrer-when-downgrade", | |
body: JSON.stringify({ | |
serviceSelections: [{ values: [SERVICE_ID] }], | |
extraServices: [], | |
}), | |
method: "POST", | |
mode: "cors", | |
credentials: "include", | |
} | |
); | |
return await response.json(); | |
}; | |
const run = async () => { | |
const token = await obtainSessionId(); | |
const localities = await getLocalities(token); | |
const startingWeek = getWeekNumber(new Date()); | |
const endingWeek = getWeekNumber(new Date("2020-10-01")); | |
const offices = localities.localities | |
.filter((locality) => locality.name === "Helsinki") | |
.map((locality) => locality.offices.map((office) => office.id)) | |
.flat(); | |
const requests = []; | |
for (let week = startingWeek; week <= endingWeek; week++) { | |
for (const office of offices) { | |
requests.push(checkSlots(token, week, office)); | |
} | |
} | |
const responses = await Promise.all(requests); | |
const times = responses.map((result, index) => { | |
return { | |
week: startingWeek + index, | |
name: result.office.name, | |
dailyTimes: result.dailyTimesByOffice, | |
}; | |
}); | |
times.forEach((time) => { | |
if (time.dailyTimes.flat().length > 0) { | |
console.log(time); | |
} | |
}); | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment