mirror of https://github.com/janLo/punkow
Change scraper to return the booking result
This commit is contained in:
parent
9d91371b55
commit
40a0c09384
|
|
@ -24,7 +24,6 @@ def print_url(r, *args, **kwargs):
|
||||||
class BookingData(typing.NamedTuple):
|
class BookingData(typing.NamedTuple):
|
||||||
name: str
|
name: str
|
||||||
email: str
|
email: str
|
||||||
id: typing.Optional[int]
|
|
||||||
|
|
||||||
|
|
||||||
class BookingResult(typing.NamedTuple):
|
class BookingResult(typing.NamedTuple):
|
||||||
|
|
@ -174,7 +173,7 @@ class BookingService(object):
|
||||||
if process is None or process == -1:
|
if process is None or process == -1:
|
||||||
logger.error("No process id found")
|
logger.error("No process id found")
|
||||||
self.session.get(BASE_URL + ABORT_URL)
|
self.session.get(BASE_URL + ABORT_URL)
|
||||||
return False
|
return None
|
||||||
|
|
||||||
formdata["process"] = process.attrs["value"]
|
formdata["process"] = process.attrs["value"]
|
||||||
|
|
||||||
|
|
@ -185,19 +184,19 @@ class BookingService(object):
|
||||||
logger.warning("Not really booked as we're in debug mode!")
|
logger.warning("Not really booked as we're in debug mode!")
|
||||||
self.session.get(BASE_URL + ABORT_URL)
|
self.session.get(BASE_URL + ABORT_URL)
|
||||||
logger.warning("Aborted!")
|
logger.warning("Aborted!")
|
||||||
return True
|
return None
|
||||||
|
|
||||||
response = self.session.post(BASE_URL + REGISTER_URL, data=formdata)
|
response = self.session.post(BASE_URL + REGISTER_URL, data=formdata)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
logger.error("Could not book appointment. Status: %d", response.status_code)
|
logger.error("Could not book appointment. Status: %d", response.status_code)
|
||||||
return False
|
return None
|
||||||
|
|
||||||
register_html = BeautifulSoup(response.content, 'html.parser')
|
register_html = BeautifulSoup(response.content, 'html.parser')
|
||||||
success = register_html.find("div", {"class": "submit-success-message"})
|
success = register_html.find("div", {"class": "submit-success-message"})
|
||||||
|
|
||||||
if success is None or success == -1:
|
if success is None or success == -1:
|
||||||
logger.error("Cannot find success message")
|
logger.error("Cannot find success message")
|
||||||
return False
|
return None
|
||||||
|
|
||||||
logger.debug("Success: %s", success.text.strip())
|
logger.debug("Success: %s", success.text.strip())
|
||||||
|
|
||||||
|
|
@ -210,14 +209,18 @@ class BookingService(object):
|
||||||
logger.info(" To change or cancel use %s with the number %s and the code %s", BASE_URL + MANAGE_URL,
|
logger.info(" To change or cancel use %s with the number %s and the code %s", BASE_URL + MANAGE_URL,
|
||||||
result["processId"], result["authKey"])
|
result["processId"], result["authKey"])
|
||||||
|
|
||||||
return True
|
booking = BookingResult(name=result["name"], email=result["mail"],
|
||||||
|
process_id=result["processId"], auth_key=result["authKey"],
|
||||||
|
metadata=details)
|
||||||
|
return booking
|
||||||
|
|
||||||
def book(self, data: BookingData):
|
def book(self, data: BookingData) -> typing.Optional[BookingResult]:
|
||||||
logging.info("Look for appointments at %s", BASE_URL + self.start_url)
|
logging.info("Look for appointments at %s", BASE_URL + self.start_url)
|
||||||
|
|
||||||
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, data.name, data.email):
|
booking = self._book_appointment(slot_url, data.name, data.email)
|
||||||
yield data
|
if self.debug or booking is not None:
|
||||||
return
|
return booking
|
||||||
|
return None
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue