-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclockaway_check.py
249 lines (195 loc) · 7.97 KB
/
clockaway_check.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
'''
------MAPcheck.py------
Purpose: Uses .csv file to search https://www.clockway.com/
and compare MAP to seller price.
If a violation occurs, a screenshot
is captured and data is logged into
a .csv file.
'''
import csv
import datetime
import logging
import os
import random
import time
import mechanicalsoup
import subprocess
import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from re import sub
from decimal import Decimal
TEST = "server"
APP_NAME = "clockway"
logger = logging.getLogger()
LOG_LEVEL = logging.INFO
def setup_logger():
logger.setLevel(LOG_LEVEL)
logging.getLogger('requests').setLevel(logging.WARNING)
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
formatter = logging.Formatter(LOG_FORMAT)
ch = logging.FileHandler(APP_NAME + ".log", mode='a', encoding=None, delay=False)
ch.setLevel(LOG_LEVEL)
ch.setFormatter(formatter)
logger.addHandler(ch)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(LOG_LEVEL)
ch.setFormatter(formatter)
logger.addHandler(ch)
setup_logger()
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--no-sandbox")
# -----Main Functions-----
# The next five functions ("def getSOMETHING")
# assign the cols in .csv file being searched
# to variables.
def getASIN():
with open('csvs/Amazon Catalog.csv') as csvfile:
rdr = csv.DictReader(csvfile)
for row in rdr:
asins = row['ASIN']
yield asins
def getModel():
with open('csvs/Amazon Catalog.csv') as csvfile:
rdr = csv.DictReader(csvfile)
for row in rdr:
modelnum = row['MFG model number']
yield modelnum
def getUPC():
with open('csvs/Amazon Catalog.csv') as csvfile:
rdr = csv.DictReader(csvfile)
for row in rdr:
upc = row['ALTUPC']
yield upc
def getTitle():
with open('csvs/Amazon Catalog.csv') as csvfile:
rdr = csv.DictReader(csvfile)
for row in rdr:
title = row['TITLE']
yield title
def getMAP():
with open('csvs/Amazon Catalog.csv') as csvfile:
rdr = csv.DictReader(csvfile)
for row in rdr:
maps = (row['MAP'])
yield maps
# Pulls webpage data.
def pullPage(modelnum):
browser = mechanicalsoup.StatefulBrowser(soup_config={'features': 'lxml'})
browser.open('https://www.clockway.com/mm5/merchant.mvc?')
browser.select_form('form[action="/mm5/merchant.mvc?"]')
search_key = ""
if len(modelnum) == 6:
search_key = modelnum[0:3] + "-" + modelnum[3:6]
elif len(modelnum) == 4:
search_key = "ridgeway " + modelnum
browser["Search"] = search_key
page=browser.submit_selected()
soup = page.soup
return soup
# Captures screenshot of pullPage().
def screenShot(link, filename):
logger.info("Making screenshot to filename %s from link %s" % (filename, link))
if TEST == "server":
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path="/usr/local/bin/chromedriver")
else:
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path="/usr/lib/chromium-browser/chromedriver")
try:
driver.get(link)
driver.get_screenshot_as_file(filename)
driver.close()
except:
logger.exception("cannot create image")
driver.close()
try:
if TEST == "server":
aws_log = subprocess.check_output(
["aws", "s3", "cp", filename, 's3://mapviolations/%s' % filename],
stderr=subprocess.STDOUT)
logger.info(aws_log)
else:
subprocess.call('cp ' + filename + ' /home/mike/Pictures/', shell=True)
except:
logger.exception("cannot upload to s3")
time.sleep(5)
os.remove(str(filename).replace('"', ''))
# Writes MAP violation data to .csv file.
def writeLog(aprice, seller, asins, maps, modelnum, upc, title, url, fba, imgurl, clockaway_modelnum):
with open('csvs/HM Violations.csv', 'ab') as f:
writer = csv.writer(f)
dd = str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M'))
if fba == "FulfillmentbyAmazon":
row = (modelnum, upc, asins, title, seller, 'TRUE', maps, aprice, '', '', dd, url, imgurl)
else:
row = (modelnum, upc, asins, title, seller, 'FALSE', maps, aprice, '', '', dd, url, imgurl)
writer.writerow(row)
f.close()
def write_xref(modelnum, clockaway_modelnum):
with open('csvs/clockawayxref.csv', 'ab') as f:
writer = csv.writer(f)
row = (modelnum, clockaway_modelnum)
writer.writerow(row)
f.close()
global asins, maps, modelnum, upc, title
with open('csvs/clockawayxref.csv', 'w') as f:
f.close()
# Assigning input .csv data to variables
asins = list(getASIN())
maps = list(getMAP())
modelnum = list(getModel())
upc = list(getUPC())
title = list(getTitle())
# Main Loop: Loop through every row in .csv file.
count = len(asins)
for i in range(count):
# If MAP not available logger.infos alert and skips to next.
if maps[i] == '#N/A':
logger.info('no MAP for row: ' + str(i) + ' - ASIN:' + str(asins[i]))
else:
try:
# Pull webpage data needed.
logger.info(i)
model_number = modelnum[i].strip()
soup = pullPage(model_number)
search_results = soup.find("div", attrs={'class':'productcat'})
if search_results:
url = soup.find("a", class_="product_link").get("href")
filename = APP_NAME + '_' + model_number + '_' + str(
datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d_%H:%M')).replace(',',
' ') + '.png'
imgurl = 'https://s3-us-west-2.amazonaws.com/mapviolations/' + filename
#price = search_results.find("i").find("big").text
#price = Decimal(sub(r'[^\d.]', '', price))
price = float(str(search_results).split('Our Price:')[1].split('<')[0].split('$')[1].replace(',', ''))
clockaway_modelnum = search_results.find("span", class_="text_cat_product_id").text
map_price = float(maps[i])
write_xref(modelnum[i], clockaway_modelnum)
if price < map_price:
logger.info("%s: violation price %f : %f" % (modelnum[i], price, map_price))
writeLog(price, APP_NAME, asins[i], maps[i], modelnum[i], upc[i], title[i], url, 'N',
imgurl,clockaway_modelnum)
screenShot(url, filename)
else:
logger.info("%s: price ok %f : %f" % (modelnum[i], price, map_price))
else:
logger.info("%s model number can't find" % modelnum[i])
continue
# Pause to help prevent interruption/blocking from Amazon.
time.sleep(random.randint(1, 2))
# If a value error occurs, logger.infos alert and skips.
except ValueError as v:
logger.info(v)
# Pause to help prevent interruption/blocking from Amazon.
time.sleep(random.randint(1, 5))
# If a Attribute error occurs, logger.infos alert and skips.
except AttributeError as e:
logger.info(e)
# Pause to help prevent interruption/blocking from Amazon.
time.sleep(random.randint(1, 5))
# If error occurs within main process, logger.infos alert and skips.
except Exception as x:
logger.info(x)
# Pause to help prevent interruption/blocking from Amazon.
time.sleep(random.randint(1, 10))