def request_slots(self, facility): logger.debug('requesting slots for ' + facility) sd = time.strftime('%d/%m/%Y') sd_str = urllib.parse.quote_plus(sd) params = {} params['tokenIdQuery'] = self.session.token params['showFirstAvailable'] = 'true' params['startDate'] = sd_str params['bookingGuideline'] = 'COVIDVACCINE' uri = f"{self.SLOT_REQUEST}/{facility}/slot-locks" headers = { 'Cookie': self.session.cookies, 'Content-Type': 'application/json' } try: response = requests.post(uri, headers=headers, params=params) response.raise_for_status() except requests.exceptions.HTTPError as errh: logger.debug("Http Error:", errh) # most likely login is out of date raise TokenExpiredException(errh) except requests.exceptions.ConnectionError as errc: logger.error("Error Connecting:", errc) except requests.exceptions.Timeout as errt: logger.error("Timeout Error:", errt) except requests.exceptions.RequestException as err: logger.error("Oops: Something Else", err) else: if (response): t = response.json() if not t['slots']: logger.info(f"No slots available at: {facility}") return 0 else: logger.info(t) appointmentList = [] for sl in t['slots']: appt = f"* {sl['facilityName']} at {sl['appointmentDate']} {sl['appointmentTime']}" appointmentList.append(appt) logger.info(f"SLOTS AVAILABLE: {facility}") TwilioNotifier.send_notification( f"Vaccine slot(s) at {facility} - {'|'.join(appointmentList)}" ) return len(appointmentList) logger.warning(f"Response for slots at {facility} was empty") return -1
def check_slots_by_facility(self, facility_code): logger.debug('checking for slots in facility ' + facility_code) #hit facility status = self.request_slots(facility_code) #if there is a slot - open browser, force cookies if status == 1: self.get_appointment() else: #delete the appointment self.delete_appointment() return status
def delete_appointment(self): logger.debug('cleaning up appointments related to token...') headers = {'Cookie': self.session.cookies} params = {} params['tokenIdQuery'] = self.session.token try: response = requests.delete(self.SLOT_DELETE, headers=headers, params=params) except requests.exceptions.HTTPError as errh: logger.error("Http Error:", errh) except requests.exceptions.ConnectionError as errc: logger.error("Error Connecting:", errc) except requests.exceptions.Timeout as errt: logger.error("Timeout Error:", errt) except requests.exceptions.RequestException as err: logger.error("Oops: Something Else", err)
def do_login(self, is_headless): login_u = os.getenv('KAISER_LOGIN') login_p = os.getenv('KAISER_PASSWORD') browser_path = os.getenv('PATH_TO_FIREFOX_DRIVER') #set the time when i rebuild this self.created_at = time.time() self.session.set_login(login_u) opts = Options() opts.headless = is_headless browser = Firefox(options=opts, executable_path=browser_path) browser.get(self.LOGIN_REQUEST) login_username_field = browser.find_element_by_id('username') login_username_field.send_keys(login_u) login_password_field = browser.find_element_by_id('password') login_password_field.send_keys(login_p) login_submit = browser.find_element_by_id('sign-on') login_submit.submit() # need the browser to load the my doctor online page delay = 5 try: WebDriverWait(browser, delay).until( EC.presence_of_element_located((By.ID, 'member-select-id'))) logger.debug('Page loaded.') except TimeoutException: logger.warning('Loading took too long...') return False self.session.set_cookies_raw(browser.get_cookies()) logger.debug(f"COOKIE: {self.session.cookies}") if is_headless: browser.close() #TODO: need to create a condition where the login doesnt work return True
def get_facilities(self): logger.debug('getting facility list and token...') headers = {'Cookie': self.session.cookies} try: response = requests.get(self.FACILITIES_REQUEST, headers=headers) response.raise_for_status() except requests.exceptions.HTTPError as errh: logger.debug("Http Error:", errh) #most likely login is out of date raise LoginExpiredException(errh) except requests.exceptions.ConnectionError as errc: logger.error("Error Connecting:", errc) except requests.exceptions.Timeout as errt: logger.error("Timeout Error:", errt) except requests.exceptions.RequestException as err: logger.error("Oops: Something Else", err) else: if (response): t = response.json() if 'token' in t: token = t['token'] logger.debug(f"TOKEN: {token}") self.session.set_token(token) return t else: logger.warning('Response for facilities was empty') return None