-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
231 lines (168 loc) · 8.29 KB
/
bot.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
import requests
from selenium import webdriver
from lxml import html
from typing_extensions import final
import util
from util import exception_safe
@exception_safe
def check_ldlc(urls):
out_results = []
for url in urls:
tree = util.get_tree(url)
nb_resultats = tree.xpath('/html/body/div[3]/div/div[3]/div[1]/div/div[2]/div[1]/div[1]/text()')[0]
nb = util.make_num(nb_resultats)
#48 is the maximum of items in a page
if int(nb) > 48:
nb = 48
results = []
for i in range(1,int(nb) + 1):
prix_ = tree.xpath(f"//*[@id='listing']//*[@data-position='{i}']/div[2]/div[4]/div[1]/div/text()")[0]
prix = util.make_num(prix_)
if(int(prix) >= 850):
continue
titre = tree.xpath(f"//*[@id='listing']//*[@data-position='{i}']/div[2]/div[1]/div[1]/h3/a/text()")[0]
if('water' in titre.lower() or 'hydro' in titre.lower()):
continue
dispo = tree.xpath(f"//*[@id='listing']//*[@data-position='{i}']/div[2]/div[3]/div/div[2]/div/span/text()")[0]
dispo_p2 = tree.xpath(f"//*[@id='listing']//*[@data-position='{i}']/div[2]/div[3]/div/div[2]/div/span/em/text()")
if len(dispo_p2) >= 1 :
dispo = dispo + ' ' + dispo_p2[0]
results.append(('LDLC.com ' + util.clean_string(titre), util.clean_string(dispo), util.clean_string(prix)))
out_results += results
return out_results
@exception_safe
def check_top_achat(urls):
out_results = []
for url in urls:
tree = util.get_tree(url)
nb_resultats = tree.xpath('//*[@id="content"]/nav[1]/ul/li[4]/text()')[0]
nb = util.make_num(nb_resultats)
results = []
liste_prix_ = tree.xpath("//section[@class = 'produits list']//div[@itemprop= 'price']/text()")
liste_titres = tree.xpath("//section[@class = 'produits list']//div[@class = 'libelle']/a/h3/text()")
liste_dispos = tree.xpath("//section[@class = 'produits list']//section[last()]/@class")
for i in range(0,int(nb)):
prix_ = liste_prix_[i][0:-4]
prix = util.make_num(prix_)
if(int(prix) >= 850):
continue
titre = liste_titres[i]
geforce_ad = " + 1 an d'abonnement GeForce Now offert ! ".lower()
call_of_ad = "+ Call of Duty: Black Ops Cold War offert ! ".lower()
if('water' in titre.lower() or 'hydro' in titre.lower()):
continue
elif(geforce_ad in titre.lower()):
titre = titre[0:len(titre) - len(geforce_ad)]
elif(call_of_ad in titre.lower()):
titre = titre[0:len(titre) - len(call_of_ad)]
raw_dispo = liste_dispos[i]
dispo = ""
if(raw_dispo == 'en-rupture'):
dispo = 'Rupture'
elif(raw_dispo == 'dispo-sous-7-jours'):
dispo = 'sous 7 jours'
elif(raw_dispo == 'dispo-entre-7-15-jours'):
dispo = 'entre 7-15 jours'
elif(raw_dispo == 'dispo-plus-15-jours'):
dispo = '+ de 15 jours'
else:
dispo = raw_dispo
results.append(('topachat.com ' + util.clean_string(titre), dispo, util.clean_string(prix)))
out_results += results
return out_results
@exception_safe
def check_pc_componentes(urls):
out_results = []
for url in urls:
tree = util.get_tree(url)
titres = tree.xpath(f"//div[@class = 'c-product-card__content']/header/h3/a/text()")
prixs = tree.xpath(f"//div[@class = 'c-product-card__content']/div[2]/div/span/text()")
dispos = tree.xpath(f"//div[@class = 'c-product-card__content']/div[3]/text()")
results = []
for titre, prix, dispo in zip(titres, prixs, dispos):
if(',' in prix):
prix = util.make_num(prix[0:-4])
else:
prix = util.make_num(prix)
if(int(prix) >= 850):
continue
if 'rtx' not in titre.lower():
continue
avoid_bool = False
avoid_words = ['reacondicionado', 'recondicionado', 'water', 'hydro', 'ekwb', 'intel', 'ryzen', '2080', '2070', 'i7', 'i5', 'Vector']
for a in avoid_words:
if a in util.clean_string(titre.lower()):
avoid_bool = True
break
if avoid_bool:
continue
if(util.clean_string(dispo).lower() == "sin fecha de entrada"):
dispo = "Rupture"
else:
dispo = "Check dispo"
results.append(('pccomponentes.com ' + util.clean_string(titre), dispo, util.clean_string(prix)))
out_results += results
return out_results
@exception_safe
def ldlc_targeted(url):
tree = util.get_tree(url)
name = tree.xpath("/html/body/div[3]/div[2]/div[1]/h1/text()")[0]
dispo = tree.xpath("/html/body/div[3]/div[2]/div[2]/div[3]/aside/div[4]/div[1]/div[2]/div/span/text()")[0]
prix_ = tree.xpath("/html/body/div[3]/div[2]/div[2]/div[3]/aside/div[1]/div/text()")[0][0:-1]
prix = util.make_num(prix_)
return (util.clean_string(name), util.clean_string(dispo), util.clean_string(prix))
#---------------Functions where the sites require javascript-----------------
def open_web_driver():
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
try:
driver = webdriver.Firefox(executable_path ='./geckodriver.exe', options = options)
except:
print(" -- Couldn't find 'geckodriver.exe'")
return None
return driver
@exception_safe
def check_nvidia(url, web_driver):
web_driver.get(url)
num = int(util.make_num(web_driver.find_element_by_xpath('/html/body/app-root/product/div[1]/div[1]/div[2]/div/suggested-product/div/div').text))
results = []
name = web_driver.find_element_by_xpath('//featured-product/div/div/div[2]/div[2]/h2').text
dispo = web_driver.find_element_by_xpath('//featured-product/div/div/div[2]/div[3]/div[1]/div[2]/a').text
prix = util.make_num(web_driver.find_element_by_xpath('//featured-product/div/div/div[2]/div[3]/div[1]/div[1]/div/span[1]').text)
if dispo == "RUPTURE DE STOCK":
dispo = "Rupture"
results.append(("FE " + util.clean_string(name), util.clean_string(dispo), util.clean_string(prix)))
if num == None:
num = 2
for i in range(1,num):
name = web_driver.find_element_by_xpath(f'//*[@id="resultsDiv"]/div/div[{i}]/div[2]/h2').text
dispo = web_driver.find_element_by_xpath(f'//*[@id="resultsDiv"]/div/div[{i}]/div[3]/div[2]/div[2]/a').text
prix = util.make_num(web_driver.find_element_by_xpath(f'//*[@id="resultsDiv"]/div/div[{i}]/div[3]/div[2]/div[1]/div/span[1]').text)
if dispo == "RUPTURE DE STOCK":
dispo = "Rupture"
results.append(("FE " + util.clean_string(name), util.clean_string(dispo), util.clean_string(prix)))
return results
@exception_safe
def check_materiel(url_list, web_driver):
output_results = []
for url in url_list:
web_driver.get(url)
nb_resultats = web_driver.find_element_by_xpath('//*[@id="tabProducts"]').text
nb = util.make_num(nb_resultats)
if int(nb) > 48:
nb = 48
results = []
for i in range(1, int(nb) + 1):
prix_ = web_driver.find_element_by_xpath(f"//*[@data-position = '{i}']/div[4]/div[1]/span").text[0:-2]
prix = util.make_num(prix_)
if(int(prix) >= 850):
continue
titre = web_driver.find_element_by_xpath(f"//*[@data-position = '{i}']/div[2]/a/h2").text
if('water' in titre.lower() or 'hydro' in titre.lower()):
continue
dispo = web_driver.find_element_by_xpath(f"//*[@data-position = '{i}']/div[3]/div/span[2]").text
if dispo == 'RUPTURE':
dispo = "Rupture"
results.append(('Materiel.net ' + util.clean_string(titre), util.clean_string(dispo), util.clean_string(prix)))
output_results += results
return output_results