Last active
July 18, 2024 06:20
-
-
Save nikdoof/b712412cd0f70c86e860712aa528b0e5 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
""" | |
Scrapes for phlebotomy appointments at St Helens & Knowsley Teaching Hospitals locations. | |
The local NHS trust uses Simplybook to manage their bloods appointment, and | |
unfortunatly the Web UI for doing this is slow and unreliable. It can take | |
upwards of 5-10 minutes to check for available appointments at each location. | |
This script scrapes the API to provide a list of the next available appointment | |
at each location, saving time when hunting for one. | |
""" | |
import requests | |
import uuid | |
from random_user_agent.user_agent import UserAgent | |
from random_user_agent.params import SoftwareName, OperatingSystem | |
# Generate a random user agent | |
user_agent = UserAgent( | |
software_names=[SoftwareName.EDGE.value, SoftwareName.CHROME.value, SoftwareName.SAFARI.value], | |
operating_systems=[OperatingSystem.MACOS.value], | |
limit=100, | |
).get_random_user_agent() | |
print(user_agent) | |
# Setup the session | |
session = requests.session() | |
session.headers.update( | |
{ | |
"Cache-Control": "no-cache", | |
"Pragma": "no-cache", | |
"User-Agent": user_agent, | |
"X-Debug": str(uuid.uuid4()), | |
"X-Requested-With": "XMLHttpRequest", | |
"Referer": "https://sthk.simplybook.cc/v2/", | |
} | |
) | |
# Grab all locations | |
locations = session.get("https://sthk.simplybook.cc/v2/ext/location") | |
for location in locations.json(): | |
# ID of the bloods service | |
service_id = location["events"][0] | |
# Grab the first day for the service | |
resp = session.get( | |
"https://sthk.simplybook.cc/v2/booking/first-working-day", | |
params={ | |
"location": location["id"], | |
"service": service_id, | |
"provider": "any", | |
"timeline": "modern", | |
}, | |
) | |
# Get the start date from the 'id' field | |
startdate = resp.json()["id"] | |
# Hit the timeslots API | |
resp = session.get( | |
"https://sthk.simplybook.cc/v2/booking/time-slots", | |
params={ | |
"from": startdate, | |
"to": startdate, | |
"location": location["id"], | |
"service": service_id, | |
"count": 1, | |
"category": "", | |
"provider": "any", | |
"booking_id": "", | |
}, | |
) | |
# print(resp.json()) | |
for appt in resp.json(): | |
# Skip any appointments marked as busy | |
if appt["type"] == "busy": | |
print("Busy...") | |
continue | |
print( | |
"{0} - {1}, {2} slots available".format( | |
location["title"], appt["id"], appt["available_slots"] | |
) | |
) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment