-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
136 lines (103 loc) · 4.33 KB
/
main.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
import warnings
warnings.filterwarnings("ignore")
import os, sys
import pandas as pd
from datetime import datetime
from time import sleep
from playsound import playsound
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options
DEBUG = False
CANADA_COMPUTERS_GPU_PAGE = f'https://www.canadacomputers.com/search/results_details.php?language=en&keywords=gpu&cpath=43'
#CANADA_COMPUTERS_GPU_PAGE = 'https://www.canadacomputers.com/search/results_details.php?language=en&keywords=cpu'
def main():
cycles = 0
while True:
try:
check()
except KeyboardInterrupt as e:
print(f'Cycles: {cycles}')
break
except Exception as e:
print(f'Cycles: {cycles}')
print(datetime.utcnow(), e)
pass
cycles += 1
if cycles % 100 == 0:
print(f'Cycles: {cycles}')
sleep(60)
def check():
driver = headless_chrome_driver()
try:
links = get_product_links(driver, CANADA_COMPUTERS_GPU_PAGE)
data = product_info(driver, links)
if len(data) > 0:
df = pd.read_csv('availability.csv', index_col=0)
df = df.append(data)
df.to_csv('availability.csv')
driver.close()
except KeyboardInterrupt as e:
driver.close()
raise KeyboardInterrupt
except:
driver.close()
raise Exception
def product_info(driver, links):
columns = ['time', 'name', 'cost', 'stock', 'link']
df = pd.DataFrame([], columns=columns)
for link in links:
try:
driver.get(link)
data = {'time':datetime.utcnow(), 'name':None, 'cost':None, 'stock':None, 'link':link}
for element in driver.find_elements_by_tag_name('h1'):
if element.get_attribute('class') is not None and element.get_attribute('class') == 'h3 mb-0':
data['name'] = element.find_element_by_tag_name('strong').text
break
if '3060' in data['name'] or \
'3070' in data['name'] or \
'3070' in data['name'] or DEBUG:
pass
else:
continue
for element in driver.find_elements_by_tag_name('span'):
if element.get_attribute('class') is not None and element.get_attribute('class') == 'h2-big':
data['cost'] = element.find_element_by_tag_name('strong').text
break
for element in driver.find_elements_by_tag_name('p'):
if element.get_attribute('class') is not None and \
element.get_attribute('id') is not None and \
element.get_attribute('class') == 'font-weight-bold stocklevel mb-0' and \
element.get_attribute('id') == 'storeinfo':
if 'IN STOCK' in str(element.text):
data['stock'] = True
elif 'OUT OF STOCK' in str(element.text):
data['stock'] = False
else:
data['stock'] = False
break
if data['stock']:
df2 = pd.read_csv('availability.csv', index_col=0)
if data['link'][80] not in df2['link'].values:
print(f"{data['time']}, {data['cost']}, {data['link']}")
for _ in range(5):
playsound(f'sounds/glass_ping.mp3')
df = df.append(data, ignore_index=True)
except Exception as e:
print(link, e)
pass
return df
def get_product_links(driver, link):
driver.get(link)
product_links = []
for element in driver.find_elements_by_tag_name('a'):
if element.get_attribute('href') is not None and 'product_info' in element.get_attribute('href'):
product_links.append(element.get_attribute('href'))
return set(product_links)
def headless_chrome_driver():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
return webdriver.Chrome(chrome_options=chrome_options)
if __name__ == '__main__':
main()