def run_tasks(self): #random interval between 5 min and 7 minutes interval = random.randint(300, 420) # seconds delay 300 # schedule the next one? e1 = self.scheduler.enter(interval, 1, self.run_tasks) logger.info(f"{time.strftime('%Y-%m-%d %H:%M:%S')} running...") for strategy in self.strategies: report = strategy.execute() self.reports.append(report)
def execute(self): start = time.perf_counter() report = ReportObject() report.strategy = self.name #build the session here if not self.session.is_active(): #if login is expired, we should probbaly build both if not self.session.is_login_active(): logger.info('building full session...') if self.do_login(True): self.get_facilities() else: logger.warning('unable to build login') if not self.session.is_token_active(): logger.info('refreshing token...') self.get_facilities() #start processing - at this point we hopefully have a valid session if self.session.is_active(): logger.info('making requests...') for facility in self.facilities: status = self.check_slots_by_facility(facility) report.add_facility(facility, status) else: logger.warning('session was not active, something happened') end = time.perf_counter() report.duration = end - start logger.info(report.to_string()) return report
def send_notification(msg): logger.info('sending notification(s)') account_sid = os.getenv('TWILIO_ACCOUNT_SID') auth_token = os.getenv('TWILIO_AUTH_TOKEN') sender = os.getenv('TWILIO_FROM') recip = os.getenv('TWILIO_TO') recipients = [] if ',' in recip: recipients = recip.split(',') else: recipients.append(recip) twilio_client = Client(account_sid, auth_token) for rec in recipients: msg = twilio_client.messages.create(to=rec, from_=sender, body=msg)
def send_report(self): #every 1 hr, send a report e2 = self.scheduler.enter(3600, 1, self.send_report) logger.info(f"{time.strftime('%Y-%m-%d %H:%M:%S')} sending report...") msg = '' is_first = True for rep in self.reports: if is_first: is_first = False else: msg += ' /r/n ' msg += rep.to_string() MailgunNotifier.send_email('Vaccine Slot Check Results', msg) #clear the reports self.reports = []
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