Last active
April 7, 2020 19:22
-
-
Save glisha/b240daee88ac43c0f43c31b951b2b323 to your computer and use it in GitHub Desktop.
Get availalbe free slots for online order by the AH
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
#!/usr/bin/python3 | |
""" | |
This script notifies you by mail when there are available time slots for online shopping at the AH. | |
./get.py --postcode 1111aa --to-mail <WHERE_TO_SEND_THE_ALLERT> --from-mail <GMAIL_SENDER_ADDRESS> --password <THEPASS> | |
""" | |
import json | |
import urllib.request | |
from email.mime.text import MIMEText | |
import smtplib | |
import logging | |
logging.basicConfig(level=logging.INFO,format="%(asctime)s %(filename)s: %(message)s") | |
import argparse | |
def sendgmail(from_mail,to_mail,username,password,subject,text): | |
msg = MIMEText(text) | |
msg['Subject'] = subject | |
msg['From'] = from_mail | |
msg['To'] = to_mail | |
server = smtplib.SMTP('smtp.gmail.com:587') | |
server.starttls() | |
server.login(username,password) | |
server.sendmail(from_mail, to_mail,msg.as_string()) | |
server.quit() | |
def fetch_dates(postcode,local_file=False): | |
if local_file: | |
resp = json.load(open(local_file)) | |
else: | |
url=f"https://www.ah.nl/service/rest/delegate?url=%2Fkies-moment%2Fbezorgen%2F{postcode}" | |
with urllib.request.urlopen(url) as r: | |
json_resp = r.read() | |
resp = json.loads(json_resp) | |
return resp["_embedded"]["lanes"][3]["_embedded"]["items"][0]["_embedded"]["deliveryDates"] | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Fetch and alert for free slots in the AH') | |
parser.add_argument("--postcode", help="The postcode to fetch the free slots for", required=True) | |
parser.add_argument("--from-mail", help="The from mail", required=True) | |
parser.add_argument("--to-mail", help="The mail to send the alert to", required=True) | |
parser.add_argument("--password", help="The password to authenticate to Gmail", required=True) | |
args = parser.parse_args() | |
delivery_dates = fetch_dates(args.postcode) | |
free_slots = [] | |
for day in delivery_dates: | |
date = day["date"] | |
for slot in day["deliveryTimeSlots"]: | |
if slot["state"] != "full": | |
full = False | |
text = f"{date} from {slot['from']}" | |
free_slots.append(text) | |
logging.info(text) | |
if free_slots: | |
mailtext = "Free slots available on\n {}".format("\n".join(free_slots)) | |
sendgmail(args.from_mail,args.to_mail,args.from_mail,args.password,"Free slots available!",mailtext) | |
else: | |
logging.info("No slots available in the next 2 weeks.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment