Allow to make multiple bookings in ine wun

This commit is contained in:
Jan Losinski 2019-01-06 03:35:36 +01:00
parent 26037bda55
commit 771102bcc2
2 changed files with 23 additions and 9 deletions

View File

@ -3,7 +3,7 @@ import logging
import datetime import datetime
import time import time
from punkow.scraper import BookingService, BASE_URL from punkow.scraper import BookingData, BookingService, BASE_URL
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -33,8 +33,10 @@ if __name__ == "__main__":
while True: while True:
try: try:
logger.info("Try to get an appointment at %s", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) logger.info("Try to get an appointment at %s", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
data = BookingData(name=args.name, email=args.email)
svc = BookingService(url, debug=False) svc = BookingService(url, debug=False)
if svc.book(name=args.name, email=args.email): for booked in svc.book([data]):
if data == booked:
break break
time.sleep(max(30, args.interval)) time.sleep(max(30, args.interval))
except KeyboardInterrupt: except KeyboardInterrupt:

View File

@ -1,5 +1,6 @@
import contextlib import contextlib
import logging import logging
import typing
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -20,6 +21,11 @@ def print_url(r, *args, **kwargs):
logger.debug("response headers: %s", r.headers) logger.debug("response headers: %s", r.headers)
class BookingData(typing.NamedTuple):
name: str
email: str
class BookingService(object): class BookingService(object):
def __init__(self, start_url, debug=True): def __init__(self, start_url, debug=True):
self.session = None self.session = None
@ -172,13 +178,19 @@ class BookingService(object):
return True return True
def book(self, name, email): def book(self, data: typing.List[BookingData]):
logging.info("Look for appontments at %s", BASE_URL + self.start_url) logging.info("Look for appontments at %s", BASE_URL + self.start_url)
data_iter = iter(data)
cur_data = next(data_iter, None)
if cur_data is None:
return
for day_url in self._iter_bookable_day_urls(self.start_url): for day_url in self._iter_bookable_day_urls(self.start_url):
with self._local_referrer(): with self._local_referrer():
for slot_url in self._iter_bookable_times(day_url): for slot_url in self._iter_bookable_times(day_url):
if self._book_appointment(slot_url, name, email): if self._book_appointment(slot_url, cur_data.name, cur_data.email):
return True cur_data = next(data_iter)
return False if cur_data is None:
return
return