Skip to content

Instantly share code, notes, and snippets.

@qsun
Last active December 3, 2021 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qsun/158d87d3bf010aabe4bd2490bf0d8fdd to your computer and use it in GitHub Desktop.
Save qsun/158d87d3bf010aabe4bd2490bf0d8fdd to your computer and use it in GitHub Desktop.
# Trying to make an appointment with someone as soon as possible.
import os
from datetime import datetime
from http.cookiejar import CookieJar
import requests
from bs4 import BeautifulSoup
notification_url = os.environ['IFTTT_WEBHOOK']
deadman_snitch_url = os.environ['DEADMAN_SNITCH_URL']
def notify_failure():
# requests.post(notification_url.format(info='failure'))
pass
def notify_success(first_slot):
print("Found available slot. Ping: ", end='')
print(notification_url.format(info='notification'))
requests.post(notification_url.format(info='notification'))
def ping():
"""ping deadman snitch"""
requests.get(deadman_snitch_url)
def is_available(calendar_url: str, cookiejar: CookieJar, csrf_token: str, identity: str, before: int):
response = requests.post(calendar_url, cookies=cookiejar, json={'_csrf': csrf_token, 'meetingTemplateId': identity})
# print(response)
# print(response.content)
print(response.json())
available_slots = response.json()['result']
if len(available_slots) > 0:
# print('check')
first_available_slot = available_slots[0]
# oopsie, the server is running on python 3.5
# first_availability = int(datetime.fromisoformat(first_available_slot[:-1]).strftime('%s'))
first_availability = int(datetime.strptime(first_available_slot, "%Y-%m-%dT%H:%M:%S.%fZ").strftime('%s'))
print("First available timestamp: " + str(first_availability))
if first_availability < before:
return True
return False
def check_availability(start_url: str, calendar_url: str, identity: str):
response = requests.get(start_url)
if response.status_code != 200:
raise Exception('Failed to retrieve start page: %s' % (start_url,))
body = response.content.decode('utf-8')
bs = BeautifulSoup(body)
body = bs.find('body')
csrf_token = body['data-csrf-token']
cookiejar = response.cookies
# print(response.cookies)
print('Token: %s - %s' % (csrf_token, str(cookiejar),))
if is_available(calendar_url, cookiejar, csrf_token, identity, 1639138158): # before next wednesday
notify_success()
else:
print("Not available")
check_availability(os.environ['START_URL'], os.environ['CALENDAR_URL'], os.environ['OWNER_IDENTITY'])
ping()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment