-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_methods.py
404 lines (310 loc) · 15.8 KB
/
browser_methods.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
from Params import Params
from selenium import webdriver
import time
from datetime import date, datetime
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.chrome.options import Options as OptionsChrome
from selenium.webdriver.support.ui import WebDriverWait
import os
from pathlib import Path
import requests
import wget
import locale
class browser_methods:
def wait_for_element(self, method, argument, timeout=10, condition="visible"):
try:
if condition == "clickable":
cond = ec.element_to_be_clickable((getattr(By, method), argument))
elif condition == "invisible":
cond = ec.invisibility_of_element_located((getattr(By, method), argument))
else:
print(getattr(By, method), argument)
cond = ec.presence_of_element_located((getattr(By, method), argument))
WebDriverWait(self.driver, timeout).until(cond)
except TimeoutException as e:
raise e
else:
if condition != "invisible":
return self.driver.find_element(getattr(By, method), argument)
else:
return
def __init__(self, choix, reports="normal"):
self.chosen_bot = ""
if(choix == "1"):
self.chosen_bot = "uber"
locale.setlocale(locale.LC_ALL, "en")
elif(choix == "2"):
self.chosen_bot = "uber_eat"
locale.setlocale(locale.LC_ALL, "fr")
else:
print("Erreur de robot")
# If page == -1 robot is done
self.page_uber= 0
self.page_uber_eat = 0
# Table with all receipts
self.list_of_receipt_uber = []
self.list_of_receipt_uber_eat = []
self.config = Params()
self.url_uber = self.config.robot["url_uber"]
self.url_uber_eat = self.config.robot["url_uber_eat"]
self.day = date.today()
self.day_folder = self.day.strftime("%d-%m-%Y")
self.end_day = "2019-09-16"
# self.chosen_bot = "uber_eat"
# self.chosen_bot = "uber"
# Start of Uber receipt robot
if(self.chosen_bot == "uber"):
# connect:
dirpath = os.getcwd()
chrome_options = OptionsChrome()
# chrome_options.add_argument(executable_path=r'C:/Program Files (x86)/Google/Chrome/Application/Driver/ChromeDriver.exe')
chrome_options.add_argument("user-data-dir=selenium")
self.my_dir_path = dirpath+"\\my_receipts\\RECEIPT_"+self.day_folder
prefs = {
"download.default_directory": self.my_dir_path,
"download.prompt_for_download": False,
"download.directory_upgrade": True
}
chrome_options.add_experimental_option('prefs', prefs)
self.driver = webdriver.Chrome(executable_path=r'C:/Program Files (x86)/Google/Chrome/Application/Driver/ChromeDriver.exe', chrome_options=chrome_options )
self.driver.get(self.url_uber)
print("Bienvenue dans le robot extracteur de recu uber")
self.end_day = input("Veuillez entrer la date du recu le plus ancien que vous voulez recevoir impérativement au format AAAA-MM-JJ (2019-09-16) ")
print("Le robot travaille sur la page : "+self.driver.current_url)
print("Veuillez vous connecter à uber puis REVENIR et appuyez sur ENTRER")
input("APPUYEZ SUR ENTRER SSI VOUS ETES CONNECTé!")
while self.page_uber != -1:
self.find_element_uber()
print("Le robot télécharge tous les élements dans le repertoire "+ self.my_dir_path +", veuillez patienter")
for obj in self.list_of_receipt_uber:
title = obj.date.replace(":","-")
download_url = "https://riders.uber.com/trips/"+ obj.id +"/pdf-receipt/"
self.driver.get(download_url)
# PAUSE IMPORTANTE SINON BUG
time.sleep(2)
for filename in os.listdir(self.my_dir_path):
filename_ = ""
if(filename.endswith(".tmp")):
filename_ = self.deleteEndOfWord(filename, 4)
if(filename.endswith(")")):
filename_ = self.deleteEndOfWord(filename, 8)
if(filename.endswith(".crdown")):
filename_ = self.deleteEndOfWord(filename, 7)
filename_ = filename
work_file_name = self.deleteEndOfWord(filename_[8:], 4)
for receipt in self.list_of_receipt_uber:
if (receipt.id == work_file_name):
try:
os.rename(self.my_dir_path+"/"+filename, self.my_dir_path+"/"+receipt.date.replace(":","-")+". at "+ receipt.price +".pdf")
except:
print("Un des receipt n'a pas été téléchargé!")
else:
print()
if(self.chosen_bot == "uber_eat"):
# connect:
dirpath = os.getcwd()
chrome_options = OptionsChrome()
# chrome_options.add_argument(executable_path=r'C:/Program Files (x86)/Google/Chrome/Application/Driver/ChromeDriver.exe')
chrome_options.add_argument("user-data-dir=selenium")
self.my_dir_path = dirpath+"\\my_receipts\\RECEIPT_"+self.day_folder+"\\uber_eat"
prefs = {
"download.default_directory": self.my_dir_path,
"download.prompt_for_download": False,
"download.directory_upgrade": True
}
chrome_options.add_experimental_option('prefs', prefs)
self.driver = webdriver.Chrome(executable_path=r'C:/Program Files (x86)/Google/Chrome/Application/Driver/ChromeDriver.exe', chrome_options=chrome_options )
self.driver.get(self.url_uber_eat)
print("Bienvenue dans le robot extracteur de recu uber")
self.end_day = input("Veuillez entrer la date du recu le plus ancien que vous voulez recevoir impérativement au format AAAA-MM-JJ (2019-09-16) ")
print("Le robot travaille sur la page : "+self.driver.current_url)
print("Veuillez vous connecter à uber EAT puis REVENIR et appuyez sur ENTRER")
input("APPUYEZ SUR ENTRER SSI VOUS ETES CONNECTé!")
while self.page_uber_eat != -1:
self.find_element_uber_eat()
print("Le robot télécharge tous les élements dans le repertoire "+ self.my_dir_path +", veuillez patienter")
for obj in self.list_of_receipt_uber_eat:
# title = obj.date.replace(":","-")
download_page_url = obj.page_url
self.driver.get(download_page_url)
self.downloadUberEatReceipt()
print("Fin dl recu.")
# PAUSE IMPORTANTE SINON BUG
time.sleep(2)
print(self.my_dir_path)
for filename in os.listdir(self.my_dir_path):
filename_ = ""
print("file name :")
print(filename)
if(filename.endswith(".tmp")):
filename_ = self.deleteEndOfWord(filename, 4)
if(filename.endswith(")")):
filename_ = self.deleteEndOfWord(filename, 8)
if(filename.endswith(".crdown")):
filename_ = self.deleteEndOfWord(filename, 7)
filename_ = filename
work_file_name = self.deleteEndOfWord(filename_[18:], 4)
for receipt in self.list_of_receipt_uber_eat:
if (receipt.page_id == work_file_name):
try:
os.rename(self.my_dir_path+"/"+filename, self.my_dir_path+"/"+receipt.date.replace(":","-")+".pdf")
except:
print("Un des receipt n'a pas été téléchargé!")
else:
print("fin")
def wait(self, t):
time.sleep(t)
def find_fill(self, name, content):
# elt_param = self.config.robot["champ_"+name]
element = self.driver.find_element_by_name(name)
element.send_keys(content)
# element.send_keys(Keys.TAB)
def find_element_uber(self):
WebDriverWait(self.driver, 10).until(
ec.presence_of_element_located((By.ID, "root"))
)
elements = self.driver.find_elements_by_css_selector("div[data-identity='trip-container']")
if(elements is not None):
for index, element in enumerate(elements):
receipt_id = element.get_attribute("data-trip-id")
ride_date_element = element.find_element_by_css_selector("div.b1 > div:nth-child(1)")
ride_date = ride_date_element.get_attribute("innerText")
receipt_price_element = element.find_element_by_css_selector("div.b1 > div:nth-child(2)")
receipt_price = receipt_price_element.get_attribute("innerText")
date_object = self.getDateObjetFromString(ride_date).strftime('%Y-%m-%d')
if(date_object > self.end_day):
# Inclusion de la date dans l'array d'objet
self.list_of_receipt_uber.append(MyUberReceipt(receipt_id, ride_date, receipt_price))
print("Travail en cours sur element n°"+str(index))
if(index == len(elements)-1):
print("Fin non sur première page")
self.page_uber += 10
self.driver.get("https://riders.uber.com/trips?offset="+str(self.page_uber))
# data-identity="pagination-next"
else:
# renommage des fichiers
self.page_uber = -1
# 18 September 2019, 5:30pm
else:
print("Element not found")
def find_element_uber_eat(self):
WebDriverWait(self.driver, 10).until(
ec.presence_of_element_located((By.ID, "root"))
)
# wrapper > div.bd.be.bf.aq.bg > div:nth-child(2)
# Change TO DO
elements = self.driver.find_elements_by_css_selector("#wrapper > div:nth-child(2) > div:nth-of-type(even)")
# elements = self.driver.find_element_by_css_selector("div:first-child")
if(elements is not None):
for index, element in enumerate(elements):
# receipt_id = element.get_attribute("href")
ride_date_element = element.find_element_by_css_selector("div:nth-child(3) > div:first-child > div:nth-child(2) > div")
page_url_element = ride_date_element.find_element_by_css_selector("a")
# ride_date = ride_date_element.get_attribute("innerText")
ride_date = element.get_attribute("innerText")
page_url = page_url_element.get_attribute("href")
page_id = page_url.split("=")[2].split("&")[0]
date_scrape = ride_date.split("•")
print("only _date variable is :")
print(date_scrape[1])
only_date = date_scrape[1]
# date_object = self.getDateObjetFromString(ride_date).strftime('%Y-%m-%d')
if(index < len(elements)):
date_object = self.getUberEatDateObjetFromString(only_date).strftime('%Y-%m-%d %H:%M')
# date_object = self.getUberEatDateObjetFromString(only_date).strftime('%Y-%m-%d %H:%M')
if(date_object > self.end_day):
# Inclusion de la date dans l'array d'objet
self.list_of_receipt_uber_eat.append(MyUberEatReceipt( page_id, date_object, page_url ))
print("Travail en cours sur element n°"+str(index))
if(index == len(elements)-1):
print("Fin non sur première page")
load_more = self.driver.find_element_by_css_selector("#wrapper > div:nth-child(2) > button")
load_more.click()
# data-identity="pagination-next"
else:
# renommage des fichiers
for index, obj in enumerate( self.list_of_receipt_uber_eat ):
print(str(index)+" : "+str(obj.date)+ "/ url: "+obj.page_url)
self.page_uber_eat = -1
# 18 September 2019, 5:30pm
else:
print("Element not found")
self.page_uber_eat = -1
# Insérer à partir de la fin d'une string
def insert_str(self,string, str_to_insert, index):
index_ = len(string) - index
return string[:index_] + str_to_insert + string[index_:]
def deleteEndOfWord(self, string, index):
index_ = len(string) - index
return string[:index_]
# 17 September 2019, 5:18pm => Sep 15 2018 00:00:00
def getDateObjetFromString(self, date):
date = date.split(",")
s_= self.insert_str(date[0], ",", 5)
d = datetime.strptime(s_, '%d %B, %Y')
return d
# Conversion date uber eat to date de: • 01 oct. à 20:20 • ==> Oct 01 2019
def getUberEatDateObjetFromString(self, date):
if " à" in date:
date = date.split(" à")
if ". à" in date:
date = date.split(". à")
# s_= self.insert_str(date[0], ",", 0)
print("DATE TO CONVERT IS")
this_year = datetime.today().year
print("Affichage split date :")
print(date[0])
print(date[1])
date[0] = date[0].replace(u'\xa0', u' ').strip()
date[1] = date[1].replace(u'\xa0', u' ').strip()
time_data = self.adjustMonth(date[0])+" "+str(this_year)+" "+date[1]
# time_data = date[0]+" "+date[1]
d = datetime.strptime(time_data, '%d %b %Y %H:%M')
return d
def quit(self):
self.driver.quit()
def adjustMonth(self, ma_date):
work_bit = ma_date.split(" ")
# if( len(work_bit[1]) > 3 and work_bit[1] != "août" ):
# work_bit[1] = work_bit[1][:3]
work_bit = work_bit[0] + " " + work_bit[1].capitalize()
return work_bit
def downloadUberEatReceipt(self):
# xpath_selector = '//*[@id="wrapper"]/div[4]/div/div[2]/div[2]/div/div/a'
# dl_link_element = WebDriverWait(self.driver, 10).until(
# ec.presence_of_element_located((By.XPATH, xpath_selector))
# )
# dl_link_element.click()
time.sleep(1.7)
css_selector ="div > div > div > div > div > a"
bouton_telechargement = self.driver.execute_script("""
selection = document.querySelectorAll(arguments[0])
for (elt of selection){
if(elt.innerText == "Télécharger le PDF"){
elt.click()
}
}
""", css_selector)
print("BOUTON TROUVé : ")
print(bouton_telechargement)
# bouton_telechargement.click()
class MyUberReceipt():
def __init__(self, id, date, price):
self.id = id
self.date = date
self.price = price
class MyUberEatReceipt():
def __init__(self, page_id, date, page_url):
self.page_id = page_id
self.page_url = page_url
self.date = date
# self.price = price
# my_objects = []
# for i in range(100):
# my_objects.append(MyClass(i))
# # later
# for obj in my_objects:
# print obj.number