-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBUILD
305 lines (274 loc) · 13 KB
/
BUILD
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
#!/usr/bin/env python
"""
This webscraper opens a headless chrome window and seaches for line in the
tingfinder.csv file on the danish trading platforms dba.dk, gulgratis.dk and
auction house Lauritz.com (experimental)
"""
from time import sleep # We use time to do waits between pages
import datetime
import pickle # Pickle is a simple database, to store the number of ads
import csv # CSV is the fileformat of the product sheet.
from rich.console import Console
import notify # we use notify to send notifications to the user
from selenium import webdriver # Selenium is what opens up the browser, and does the stuff
from selenium.webdriver.chrome.options import Options # Options are passed to the Chrome browser
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys # Keys is so we can send keys to inputs.
import yaml
# Full paths generated on install
FILE_NAME_PATH
DATA_FOLDER_PATH
CONFIG_FOLDER_PATH
#Import settings
config_file = CONFIG_FOLDER + "config.yml"
config = yaml.safe_load(open(config_file))
# Rich console for extra flavor
console = Console()
# Chrome settings
chrome_options = Options()
chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
#Initialize the browser and declare where to go
driver = webdriver.Chrome(options=chrome_options)
MIN_VALUE = 0
MAX_VALUE = 0
SEARCH_TERM = ""
COOKIE = False
POSTAL_CODE = config["settings"]["postal_code"]
SEARCH_RADIUS = config["settings"]["search_radius"]
def slugify(string):
"""
Turns the seach term into a system friendly format.
Used to create the database filename for each product/search term.
"""
slugged = string.replace(" ","-")
return slugged.replace("--","-")
def add_plus(string):
"""
Replaces spaces with plus signs in the passed text string.
Is used to create a direct search query URL to GulGratis.
"""
return string.replace(" ","+")
def den_blaa_avis():
"""
Opens dba.dk, reject cookies, input search terms and prices.
Notifies user if the number of hits is larger than last search.
"""
dba_url = "https://dba.dk"
try:
# See if we have any data on the given product if not db antal = 0
db_antal = pickle.load(open(DATA_FOLDER + \
slugify(SEARCH_TERM) + "_dba.dat", "rb"))
except:
db_antal = 0
driver.get(dba_url) # Open dba_url.dk in the chrome browser
driver.implicitly_wait(2)
sleep(1)
try:
# Try to click the reject COOKIEs button if it's there.
REJECT = driver.find_element_by_id("onetrust-reject-all-handler")
REJECT.click()
except NoSuchElementException:
COOKIE = False
search = driver.find_element_by_id("searchField")
search.send_keys(SEARCH_TERM) # send search term / product name
search.send_keys(Keys.RETURN)
sleep(2)
driver.implicitly_wait(2)
if POSTAL_CODE is not None:
km_radius = driver.find_element_by_id("GeoSearchRadius") # Find the input field for the search radius
km_radius.send_keys(SEARCH_RADIUS) # Send it our search radius
sleep(1)
postal_code = driver.find_element_by_id("geosearch-zipcode")
postal_code.send_keys(POSTAL_CODE) # Send our postal code
postal_code.send_keys(Keys.RETURN) # Hit enter
sleep(2)
driver.implicitly_wait(2)
driver.find_element_by_xpath("//h4[contains(text(), 'Pris')]").click() # Set our price wishes
price = driver.find_element_by_class_name("rangeFrom") # Min value
price.send_keys(MIN_VALUE)
price = driver.find_element_by_class_name("rangeTo") # Max value
price.send_keys(MAX_VALUE)
price.send_keys(Keys.RETURN)
driver.implicitly_wait(2)
sleep(2)
try: # if the td containing "annoncer" exists, it means we have hits on our search.
antal_annoncer_string = driver.find_element_by_xpath("//td[contains(text(),\
'annoncer')]").get_attribute("innerHTML").strip()
# Extract the integer from the text
antal = [int(i) for i in antal_annoncer_string.split() if i.isdigit()]
diff = int(antal[0]) - db_antal # Calculate the difference, if any.
if diff > 0:
# If our diff is a positive number:
string = str(antal[0]) + ' annoncer fundet på DBA.dk. ' + \
str(diff) + '+ ift forrige søgning.'
print(str(antal[0]) + ' annoncer fundet. ' + str(diff) + '+ ift forrige søgning.')
print("- URL: " + driver.current_url)
# Dump the new number of items into the database
pickle.dump(int(antal[0]), open(DATA_FOLDER + \
slugify(SEARCH_TERM) + "_dba.dat", "wb"))
notify.notification(string,title=SEARCH_TERM) # Send notification
elif diff == 0:
# If the number of hits is the same as last search:
print("- Ingen nye annoncer. (" + str(antal[0]) + " fundet)")
print("- URL: " + driver.current_url)
else:
# If there are fewer hits than earlier:
print("- " + str(antal) + " fundet. " + str(diff) + " færre end sidste søgning")
print("- URL: " + driver.current_url)
# Dump the new number of items into the database
pickle.dump(int(antal[0]), open(DATA_FOLDER + \
slugify(SEARCH_TERM) + "_dba.dat", "wb"))
except NoSuchElementException:
print("- " + SEARCH_TERM + " ikke fundet i prisklassen.")
# Check the same on GulGratis
def gul_og_gratis():
"""
Creates a search query URL for the passed search term and opens
the URL, accept potential cookies and grabs the number of posts/ads
and then notifies the users if diff is higher than last search.
"""
try: # See if we have any data on the given product else db antal is 0
db_antal = pickle.load(open(DATA_FOLDER + \
slugify(SEARCH_TERM) + "_gg.dat", "rb"))
except:
db_antal = 0
# On this page we use the search query directly in the URL we're fetching.
query = add_plus(SEARCH_TERM)
gulgratis_url_base = "https://www.guloggratis.dk/s/q-"
gulgratis_url_complete = gulgratis_url_base+query+"?price="+MIN_VALUE+"-"+MAX_VALUE #Build the complete URL
driver.get(gulgratis_url_complete) # Open search page
driver.implicitly_wait(2)
try:
# If we can find the accept cookie button, click it
ACCEPT = driver.find_element_by_id("onetrust-accept-btn-handler")
ACCEPT.click()
except NoSuchElementException:
COOKIE = False
driver.implicitly_wait(2)
sleep(2)
try: # If there is an H1 tag containing the text "Søgeresultat for" we continue
gg_antal = int(driver.find_element_by_xpath("//h1[contains(text(),\
'Søgeresultat for')]/following-sibling::span").get_attribute("innerHTML"))
diff = gg_antal - db_antal # Compare fresh number of hits with stored ones
if diff > 0:
# If diff is a positive number, we got new ads.
string = str(gg_antal) + " fundet på GulGratis.dk. " + \
str(diff) + "+ ift. forrige søgning."
print("- " + str(gg_antal) + " fundet. " + str(diff) + \
"+ ifh. forrige søgning") # Print result
print("- URL: " + driver.current_url) # Print URL to products
notify.notification(string,title=SEARCH_TERM) # Notify the user
# Dump the new number of items into the database
pickle.dump(gg_antal, open(DATA_FOLDER + \
slugify(SEARCH_TERM) + "_gg.dat", "wb"))
elif diff == 0: # If there is products to show, but the number of products is the same as last search.
print("- Ingen nye annoncer. (" + str(gg_antal) + " fundet)")
print("- URL: " + driver.current_url)
else: # If diff is negative, some products have been removed/sold
print("- " + str(gg_antal) + " fundet. " + str(diff) + " ift forrige søgning.")
print("- URL: " + driver.current_url)
# Dump the new number of items into the database
pickle.dump(gg_antal, open(DATA_FOLDER + \
slugify(SEARCH_TERM) + "_gg.dat", "wb"))
except NoSuchElementException:
print("- " + SEARCH_TERM + " ikke fundet i prisklassen.")
def Lauritz_com():
"""
Search lauritz.com for our products.
"""
try: # See if we have any data on the given product else db antal is 0
db_antal = pickle.load(open(DATA_FOLDER + \
slugify(SEARCH_TERM) + "_l.dat", "rb"))
except:
db_antal = 0
driver.get("https://lauritz.com")
driver.implicitly_wait(2)
try: # Decline cookies
DECLINE = driver.find_element_by_id("CybotCookiebotDialogBodyLevelButtonLevelOptinDeclineAll")
DECLINE.click()
except:
COOKIE = True
driver.implicitly_wait(2)
search = driver.find_element_by_id("SearchTextBox")
search.send_keys(SEARCH_TERM)
search.send_keys(Keys.ENTER)
sleep(2)
driver.implicitly_wait(2)
if int(MAX_VALUE)<=2000: # Dial in the price. I use the MAX_VALUE and compare to the filtering options.
filter = driver.find_element_by_xpath("//option[contains(text(),\
'Under 2,000 DKK')]")
elif int(MAX_VALUE)<=5000:
filter = driver.find_element_by_xpath("//option[contains(text(),\
'Under 5,000 DKK')]")
elif int(MAX_VALUE)<=10000:
filter = driver.find_element_by_xpath("//option[contains(text(),\
'Under 10,000 DKK')]")
elif int(MAX_VALUE)<=20000:
filter = driver.find_element_by_xpath("//option[contains(text(),\
'Under 20,000 DKK')]")
else:
filter = driver.find_element_by_tag_name("body")
filter.click() # Click the filtering option
sleep(2)
driver.implicitly_wait(2)
try:
antal_annoncer = driver.find_element_by_id("List_TotalItemCount")
antal_annoncer_string = antal_annoncer.get_attribute("innerHTML")
# Single out the integer
l_antal = [int(i) for i in antal_annoncer_string.split() if i.isdigit()]
diff = int(l_antal[0]) - db_antal
if diff > 0:
# If diff is a positive number, we got new ads.
string = str(l_antal[0]) + " fundet på Lauritz.com. " + \
str(diff) + "+ ift. forrige søgning."
print("- " + str(l_antal[0]) + " fundet. " + str(diff) + \
"+ ifh. forrige søgning")
print("- URL: " + driver.current_url)
notify.notification(string,title=SEARCH_TERM)
# Dump the new number of items into the database
pickle.dump(l_antal[0], open(DATA_FOLDER + slugify(SEARCH_TERM) + "_l.dat", "wb"))
elif diff == 0:
print("- Ingen nye annoncer. (" + str(l_antal[0]) + " fundet)")
print("- URL: " + driver.current_url)
else:
print("- " + str(l_antal[0]) + " fundet. " + str(diff) + " ift forrige søgning.")
print("- URL: " + driver.current_url)
pickle.dump(l_antal[0], open(DATA_FOLDER + slugify(SEARCH_TERM) + "_l.dat", "wb"))
except NoSuchElementException:
print("- " + SEARCH_TERM + " ikke fundet i prisklassen.")
with console.status("[bold green] Søger") as status:
print(datetime.datetime.now())
print(" ")
with open(FILE_NAME, "r") as csvfile:
datareader = csv.reader(csvfile)
# Run once for each line in our search agent csv file.
for row in datareader:
SEARCH_TERM = row[0]
MIN_VALUE = row[1]
MAX_VALUE = row[2]
# Printing output to std.out
if config["platforms"]["den_blaa_avis"] is False and config["platforms"]["gul_gratis"] is False and config["platforms"]["lauritz_com"] is False:
print("No platforms is set in the config file. Please adjust and run again.")
console.print("[bold green]Produkt:[/bold green] \"" + SEARCH_TERM + "\"")
console.print("[bold green]Pris: [/bold green]" + MIN_VALUE + "-" + MAX_VALUE + " DKK")
print(" ")
if config["platforms"]["den_blaa_avis"] is True:
console.print("[bold green]DBA.DK")
den_blaa_avis() # Output from dba.dk
print(" ")
if config["platforms"]["gul_gratis"] is True:
console.print("[bold green]GulGratis.dk ")
gul_og_gratis() # Output from GulGratis.dk
print(" ")
if config["platforms"]["lauritz_com"] is True:
console.print("[bold green]Lauritz.com: ")
Lauritz_com() # Output from Lauritz.com
print(" ")
console.print("[bold red]-----------------------------------------")
print(" ")
if COOKIE == True:
COOKIE = False
else:
COOKIE = True
driver.quit()