Created
April 12, 2023 18:01
-
-
Save fortran01/c0e4c1fda8c795462af3b7d1eefd7730 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
from time import sleep | |
import requests | |
import datetime | |
# Get the store open dates for the next n days | |
def get_open_dates(start_date, store_id, num_days): | |
url = f"https://www.kaltire.com/on/demandware.store/Sites-Kaltire-Site/default/Appointment-GetStoreOpenDatesByDate?startDate={start_date}&storeID={store_id}&numDays={num_days}&serviceId=2731" | |
headers = { | |
'authority': 'www.kaltire.com', | |
'accept': 'application/json, text/javascript, */*; q=0.01', | |
'accept-language': 'en-US,en;q=0.9', | |
'referer': 'https://www.kaltire.com/en/appointment-locations/?storedTiresNo=true', | |
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36', | |
'x-requested-with': 'XMLHttpRequest', | |
'cookie': '<car details are stored in a cookie>' | |
} | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
return response.json() | |
else: | |
return None | |
# Get the available appointment slots for a given date | |
def get_available_slots(date, store_id): | |
url = f"https://www.kaltire.com/on/demandware.store/Sites-Kaltire-Site/default/Appointment-GetOpenSlotByDate?storeID={store_id}&date={date}&serviceId=2731" | |
headers = { | |
'authority': 'www.kaltire.com', | |
'accept': 'application/json, text/javascript, */*; q=0.01', | |
'accept-language': 'en-US,en;q=0.9', | |
'referer': 'https://www.kaltire.com/en/appointment-locations/?storedTiresNo=true', | |
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36', | |
'x-requested-with': 'XMLHttpRequest' | |
} | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
return response.json() | |
else: | |
return None | |
# Check for available appointment slots starting from next Sunday | |
while True: | |
print(f"{datetime.datetime.now()} Checking for available appointment slots...") | |
count = 0 | |
next_sunday = datetime.datetime.today() + datetime.timedelta(days=(6-datetime.datetime.today().weekday())%7+1) | |
while count == 0: | |
# Determine how many days are remaining in the current week | |
days_remaining = 7 - next_sunday.weekday() | |
# Get the store open dates for the next n days | |
open_dates = get_open_dates(next_sunday.strftime("%Y%m%d"), 879, days_remaining) | |
if open_dates and int(open_dates['count']) > 0: | |
for date_obj in open_dates['data']: | |
date_str = date_obj['date'] | |
# Get the available appointment slots for the date | |
slots = get_available_slots(date_str, 879) | |
if slots and int(slots['count']) > 0: | |
count = int(slots['count']) | |
# Convert date_str (20230516) to an easy to read format (May 16, 2023) | |
date_str = datetime.datetime.strptime(date_str, "%Y%m%d").strftime("%B %d, %Y") | |
print(f"Found {count} available appointment slots on {date_str}") | |
break | |
# Increment the date by 1 week if we haven't found an available slot | |
if count == 0: | |
next_sunday += datetime.timedelta(days=7) | |
print(f"{datetime.datetime.now()} Sleeping for 30 seconds...") | |
sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment