-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdose_finder_part.py
184 lines (138 loc) · 5.73 KB
/
dose_finder_part.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
print("---------")
from selenium.webdriver import Firefox
from selenium.webdriver import ActionChains
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.select import Select
import time
import requests
import pandas as pd
import json
from datetime import datetime
from datetime import timedelta
import sys
import locale
import numpy as np
try:
locale.setlocale(locale.LC_TIME, "fr_FR")
except:
print("Error locale")
pass
def search_slot(url):
opts = Options()
opts.headless = True
browser = Firefox(options=opts)
browser.get(url)
browser.implicitly_wait(1)
# Bouton Cookies
btn = browser.find_elements_by_xpath("//button[contains(.,'Accepter & Fermer')]")
btn[0].click()
# Sélectionner motif
select_element = browser.find_element_by_id("booking_motive")
select_object = (Select(select_element))
text_select = select_object.options[1].text
select_object.select_by_visible_text(text_select)
time.sleep(1)
# Bouton prochaines dispos
try:
btn = browser.find_elements_by_xpath("//button[contains(.,'Prochain RDV')]")
btn[0].click()
except:
print("Pas de bouton Prochain RDV")
time.sleep(0)
# Premier slot dispo
slots = browser.find_elements_by_class_name("availabilities-slot")
slot = slots[0].get_attribute("title")
browser.close()
return slot
def fetch_centres():
url = "https://www.data.gouv.fr/fr/datasets/r/5cb21a85-b0b0-4a65-a249-806a040ec372"
data = requests.get(url)
with open('data/input/centres-vaccination.csv', 'wb') as f:
f.write(data.content)
def import_last_metadata(dep_min):
try:
with open("data/output/temp/metadata{}.json".format(dep_min), "r") as f:
dict_json = json.load(f)
except:
print("Metada not found. Starting from empty dict.")
dict_json = {"last_dep_updated": "no"}
if(len(dict_json)==0):
dict_json = {"last_dep_updated": "no"}
return dict_json
def sort_data(slots, noms, urls):
#slots_datetime = [datetime.strptime(date[:-6] + " 2021", "%a. %d %b. %Y") for date in slots]
print(datetime.now().strftime("%a %d %b. %Y"))
#idx = np.argsort(slots_datetime)
#slots = list(np.array(slots)[idx])
#noms = list(np.array(noms)[idx])
#urls = list(np.array(urls)[idx])
return slots, noms, urls
def export_data(dep, slots, urls, noms, departements, departements_noms, dep_min, noms_pas_de_rdv, urls_pas_de_rdv, noms_autres, urls_autres):
dict_json = {}
slots, noms, urls = sort_data(slots, noms, urls)
dict_json = {"slots": slots, "urls": urls, "noms": noms, "scan_time": (datetime.now() + timedelta(seconds=2*3600)).strftime("%d/%m/%Y à %Hh%M"),\
"urls_pas_de_rdv": urls_pas_de_rdv, "noms_pas_de_rdv": noms_pas_de_rdv,\
"urls_autres": urls_autres, "noms_autres": noms_autres}
dict_json["last_dep_updated"] = dep
with open("data/output/temp/{}.json".format(dep), "w+") as outfile:
outfile.write(json.dumps(dict_json))
dict_metadata = {"last_dep_updated": dep}
with open("data/output/temp/metadata{}.json".format(dep_min), "w+") as outfile:
outfile.write(json.dumps(dict_metadata))
def get_last_updated_dep(dep_min):
dict_json = import_last_metadata(dep_min)
return dict_json["last_dep_updated"]
def import_departements():
df = pd.read_csv('data/input/departements-france.csv')
return df.code_departement.astype(str).to_list(), df.nom_departement.astype(str).to_list()
def main():
dep_min = int(sys.argv[1])
dep_max = int(sys.argv[2])
print("Starting...")
fetch_centres()
print("Centres fetched")
df = pd.read_csv('data/input/centres-vaccination.csv', sep=";", dtype={'com_cp': 'object'})
df["com_cp"] = df["com_cp"].astype("str")
departements_all, departements_noms = import_departements()
departements = departements_all[max(0, dep_min) : min(len(departements_all)-1, dep_max)]
print("departements", departements)
last_updated_dep = get_last_updated_dep(dep_min)
print("last_updated_dep", last_updated_dep)
if(last_updated_dep=="no"):
print("if1")
id_last_updated = dep_min
else:
print("else")
print("index", departements_all.index(last_updated_dep))
id_last_updated = departements_all.index(last_updated_dep) + 1
if(id_last_updated > dep_max):
print("if2")
id_last_updated = dep_min
print("id_last_updated", id_last_updated)
for dep in [departements_all[id_last_updated]]:
print("DEP ======", dep)
df_dep_all = df[df.com_cp.str.match(r'(^{}.*)'.format(dep))==True]
df_dep = df_dep_all[df_dep_all.rdv_site_web.str.match(r'(.*doctolib.*)')==True]
print("nb centres :", len(df_dep))
slots=[]
urls=[]
noms=[]
noms_pas_de_rdv = []
urls_pas_de_rdv = []
for (idx, url) in enumerate(df_dep["rdv_site_web"].values):
print("Centre n°", idx)
try:
slot = search_slot(url)
slots += [slot]
urls += [url]
noms += [df_dep["nom"].values[idx]]
print(slot)
except:
noms_pas_de_rdv += [df_dep["nom"].values[idx]]
urls_pas_de_rdv += [url]
print("not found")
df_dep_liens_autre = df_dep_all[~df_dep_all.rdv_site_web.isna()]
df_dep_liens_autre = df_dep_liens_autre[df_dep_liens_autre.rdv_site_web.str.match(r'(.*doctolib.*)')==False]
noms_autres, urls_autres = df_dep_liens_autre.nom.tolist(), df_dep_liens_autre.rdv_site_web.tolist()
export_data(dep, slots, urls, noms, departements, departements_noms, dep_min, noms_pas_de_rdv, urls_pas_de_rdv, noms_autres, urls_autres)
main()