-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_RDV_Prefecture.py
More file actions
119 lines (80 loc) · 3.46 KB
/
Get_RDV_Prefecture.py
File metadata and controls
119 lines (80 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 14 18:38:34 2022
This script is designed to check for available
appointments at the Prefecture. If one is available,
the appointment will be requested on the webserver
(that is opened by this script), and an email will
be sent to myself. Upon receiving the email, I must
go to the webserver to complete the process. The
laptop needs to stay open for this, unless running
on a server.
A password needs to be added in the parameter 'app_generated_password'.
It was removed from this script for security.
@author: maticka
"""
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from datetime import datetime
import smtplib
from email.message import EmailMessage
# In[]:
n_min = 7 # min, number of minutes to wait before checking again.
# In[]: define function to send email
def sendMail():
# server = smtplib.SMTP("smtp.gmail.com", 587)
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.ehlo()
# app generated password from gmail site (insert between quotes)
app_generated_password = ""
server.login("smaticka@gmail.com", app_generated_password)
subject = "RDV Available at PREFECTURE"
bodyText = "Hi Sam,\n Go here: https://www.herault.gouv.fr/booking/create/15259/0\n see you soon,\nSam"
message = EmailMessage()
message["Subject"] = subject
message["From"] = "smaticka@gmail.com"
message["To"] = "smaticka@gmail.com"
message.set_content(bodyText)
server.sendmail("smaticka@gmail.com", "smaticka@gmail.com", message.as_string())
# In[]: Count number of attempts
print('Started at: {}'.format(datetime.now()))
status = 'Unavailable'
count = 0 # count the number of times checked
# In[]: Open URL in Chrome
# webdriver to talk to
driver = webdriver.Chrome(ChromeDriverManager().install())
# make request to the driver
driver.get('https://www.herault.gouv.fr/booking/create/15259/0')
# In[]: continue to check if appointment every 10 minutes
# keep log of time checked and count.
while status != 'Complete':
# get element of the box to click
click_box = driver.find_element_by_xpath('//*[@id="condition"]')
# click the box
click_box.click()
# get element for the go box
go_button = driver.find_element_by_xpath('//*[@id="submit_Booking"]/input[1]')
# click go
go_button.click()
# check if resulting page says no appointments
body_text = driver.find_element_by_xpath('//*[@id="FormBookingCreate"]').text
# check if the message tells me there are no more appointments
if "Il n'existe plus de plage horaire libre pour votre demande de rendez-vous" in body_text:
# print out how many times searched so far
count += 1
print(count)
# end the search and try again
terminate_button = driver.find_element_by_xpath('//*[@id="submit_Booking"]/input')
# click end
terminate_button.click()
time.sleep(n_min*60) # check every n_min (number of minutes)
else:
# send alarm to myself
sendMail()
# Print out when the search finished, how many times it was searched, and the new results on the site when trying to book
print('\nFinished at: {}'.format(datetime.now()))
print('\nCheck {} times'.format(count))
print('\nNew Status: {}'.format(body_text))
status = 'Complete'