-
Notifications
You must be signed in to change notification settings - Fork 0
/
promo_content_refactor.py
356 lines (290 loc) · 12.8 KB
/
promo_content_refactor.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
import requests
import csv
import json
import teradata
import time
from bs4 import BeautifulSoup as BS
import os
# doc = "promo_html.html"
# soup = BS(open(doc), 'html.parser') # static file
# stage_uri = "https://www.stage.shutterfly.com/"
prod_uri = "https://www.shutterfly.com/"
response = requests.get(prod_uri)
soup = BS(response.text, 'html.parser')
# print(soup.prettify())
'''
[
{'device': 'desktop', 'promo_location': 'desktop-offer-banner-1', 'offer_banner': 1, 'offer_code': 'SHIP39', 'offer_details': 'FREE SHIPPING', 'promo_start': '20-AUG-2018', 'promo_end': '21-AUG-2018', 'offer_url': 'https://www.shutterfly.com/promotions_details/#economy', 'offer_terms': 'really long text', 'offer_template': 'MASTER_TEMPLATE_001'},
{'device': 'desktop', 'promo_location': 'desktop-offer-banner-2', 'offer_banner': 2, 'offer_code': 'SUMMERTIME', 'offer_details': '50% OFF HARDCOVER BOOKS, GIFTS, & HOME DECOR + 40% ALL ELSE* ', 'promo_start': '22-AUG-2018', 'promo_end': '22-AUG-2018', 'offer_url': 'https://www.shutterfly.com/promotions_details/#economy', 'offer_terms': 'long long text', 'offer_template': 'MASTER_TEMPLATE_002'},
{'device': 'mobile', 'promo_location': 'mobile-offer-banner-1', 'offer_banner': 1, 'offer_code': 'SHIP39', 'offer_details': 'Free shipping on $39+', 'promo_start': '', 'promo_end': '', 'offer_url': 'https://www.shutterfly.com/special-offers', 'offer_terms': '', 'offer_template': ''}
]
'''
promo_master = list() # master list to store promo json
device_type = ['desktop'] # desktop or mobile banner, add 'mobile later
# chang to device_type
# fetch available banner ids and load to list
banner_ids = list()
data = list()
data_tup = tuple()
for device in device_type:
# clear out contents
banner_ids.clear() # clear list before picking next promo type
ofr_template = ''
ofr_terms = ''
ofr_exp = ''
ofr_start = ''
ofr_code = ''
ofr_details = ''
ofr_url = ''
ofr_promoid = ''
for tag in soup.find_all(class_=str(device) + '-offer-banner'):
# print("id = %s" % tag.get('id'))
banner_ids.append(tag.get('id'))
for x, item in enumerate(banner_ids):
# for content in soup.find_all('div', attrs={'id': item}):
# print(item)
# pass
ofr_banner = x + 1
# print("promo = %s" % device)
# print("x = %d" % x)
# print("item = %s"% item)
# print("banner_ids = %s"% banner_ids)
for offer_url in soup.find_all('a', attrs={
'id': str(device) + "-offer-url-" + str(x + 1)}): # list indices start at 0
# print("promo banner %s" %(x+1))
ofr_url = "https://www.shutterfly.com" + offer_url['href']
# print(str(device) + " offer url " + str(x + 1) + " : "+ofr_url)
# print(str(item)+" http://www.shutterfly.com"+offer_url['href'])
# promo_json.append(offer_url['href'])
pass
for offer_details in soup.find_all('span', attrs={'id': str(device) + "-offer-details-" + str(x + 1)}):
ofr_details = offer_details.text
# print(str(device) + " offer details " + str(x + 1) + ": %s" % offer_details.text)
# print(str(item)+" "+offer_details.text)
# promo_json.append(offer_details.text)
pass
for offer_code in soup.find_all('span',
attrs={'id': str(device) + "-offer-code-" + str(x + 1), 'class': 'hidden'}):
ofr_code = offer_code.text
# print("offer code: %s" % ofr_code)
for offer_start in soup.find_all('span', attrs={'id': str(device) + "-offer-start-" + str(x + 1)}):
# if offer_start.text == '':
# ofr_start = '01-Jan-1900' # default null date to something
# else:
ofr_start = offer_start.text
# print("offer start: %s" % ofr_start)
for offer_exp in soup.find_all('span', attrs={'id': str(device) + "-offer-expiration-" + str(x + 1)}):
# if offer_exp.text == '' or offer_exp.text None:
# ofr_exp = '31-Dec-9999'
# else:
ofr_exp = offer_exp.text
# print("offer expiration: %s" % ofr_exp)
for offer_terms in soup.find_all('span', attrs={'id': str(device) + "-offer-terms-" + str(x + 1)}):
ofr_terms = offer_terms.text
# print("offer terms = %s" % ofr_terms)
for offer_template in soup.find_all('span', attrs={'id': str(device) + "-offer-template-" + str(x + 1)}):
ofr_template = offer_template.text
# print("offer template = %s" % ofr_template)
# print("---------------------\n")
for offer_template in soup.find_all('span', attrs={'id': str(device) + "-offer-promoid-" + str(x + 1)}):
ofr_promoid = offer_template.text
# promo_json = {"device": device, "promo_location": item, "promo_banner": ofr_banner, "promo_code": ofr_code,
# "promo_details": ofr_details, "promo_start": ofr_start, "promo_end": ofr_exp, "promo_url": ofr_url,
# "promo_terms": ofr_terms, "promo_template": ofr_template}
# promo_master.append(promo_json)
# data_part = (device, item, ofr_banner, ofr_code, ofr_details,
# ofr_start, ofr_exp, ofr_url, ofr_terms, ofr_template)
data_part = (ofr_code, ofr_promoid, ofr_start, ofr_exp)
data.append(data_part)
data_tup = tuple(data)
# 10/11/18 - Remove 1st element from the tup (left eyebrow promo)
l = list(data_tup) # tup is immutable, convert to list
l.pop(0) # remove 1st element
data_tup = tuple(l) # convert back to tup
# print(data_tup) # for qa
'''
data_tup = (
('desktop', 'desktop-offer-banner-1', 1, 'SHIP39', 'FREE SHIPPING', '29-SEP-2018', '31-DEC-9999', 'https://www.shutterfly.com/promotions_details/#economy', 'Terms and conditions, really long text', '1'),
('desktop', 'desktop-offer-banner-2', 2, 'no code needed', '50% off Hardcover Photo Books Plus, 30-50% Off Everything Else*', '29-SEP-2018', '02-OCT-2018', 'https://www.shutterfly.com/promotions_details/#economy', 'Terms and conditions, really long text', '2'),
('mobile', 'mobile-offer-banner-1', 1, 'SHIP39', 'Free shipping on $39+', '', '', 'https://www.shutterfly.com/special-offers', '', '')
)
'''
print(data_tup)
print("parsing completed")
# print(promo_master)
# Prep teradata insert
# insert_data = data_tup
dsn = 'TDDB'
APP_CONFIG_FILE = os.getcwd() + "/tdPyInterface.ini"
udaExec = teradata.UdaExec(appName="tdPyInterface", version="1.0",
logConsole=False, appConfigFile=APP_CONFIG_FILE)
session = udaExec.connect(dsn)
# CRIPT_PROMO_STG
'''
--OLD--
CT CRMP.CRIPT_PROMO_STG (
DEVICE varchar(32),
PROMO_LOCATION varchar(64),
PROMO_BANNER integer,
PROMO_CODE varchar(128),
PROMO_DETAILS varchar(4000),
PROMO_START DATE format 'dd-MMM-YYYY' DEFAULT CURRENT_DATE,
PROMO_END DATE format 'dd-MMM-YYYY' DEFAULT DATE '9999-01-01',
PROMO_DETAILS_URL varchar(1024),
PROMO_TERMS varchar(4000),
PROMO_TEMPLATE varchar(512),
CREATED_DATE date DEFAULT CURRENT_DATE,
MODIFIED_DATE date DEFAULT NULL,
CREATED_BY VARCHAR(64) DEFAULT 'CRIPT_SCRAPER',
MODIFIED_BY VARCHAR(64) DEFAULT 'CRIPT_SCRAPER'
);
--NEW--
CT CRMP.CRIPT_PROMO_STG (
--DEVICE varchar(32),
--PROMO_LOCATION varchar(64),
--PROMO_BANNER integer,
PROMO_CODE varchar(128),
PROMO_ID varchar(512) DEFAULT '40ALL_DEFAULT',
--PROMO_DETAILS varchar(4000),
PROMO_START VARCHAR(64),
PROMO_END VARCHAR(64) DEFAULT '9999-01-01',
--PROMO_DETAILS_URL varchar(1024),
--PROMO_TERMS varchar(4000),
--PROMO_TEMPLATE varchar(512),
CREATED_DATE date DEFAULT CURRENT_DATE,
MODIFIED_DATE date DEFAULT NULL,
CREATED_BY VARCHAR(64) DEFAULT 'CRIPT_SCRAPER',
MODIFIED_BY VARCHAR(64) DEFAULT 'CRIPT_SCRAPER'
);
'''
# print("Deleting CRMP.CRIPT_PROMO_STG")
#
del_stg = "delete CRMP.CRIPT_PROMO_STG"
cursor = session.execute(del_stg)
# print ("cursor_stg = %s" % cursor.error)
time.sleep(3)
# ins_stg = """INSERT INTO CRMP.CRIPT_PROMO_STG (DEVICE, PROMO_LOCATION, PROMO_BANNER, PROMO_CODE, PROMO_DETAILS,
# PROMO_START, PROMO_END, PROMO_DETAILS_URL, PROMO_TERMS, PROMO_TEMPLATE) VALUES (?, ?, ?, ?, ?, ?,
# ?, ?, ?, ?)"""
ins_stg = """INSERT INTO CRMP.CRIPT_PROMO_STG (PROMO_CODE, PROMO_ID, PROMO_START, PROMO_END) VALUES (?, ?, ?, ?)"""
cursor = session.executemany(ins_stg, data_tup, batch=True) # batch insert
print("stage inserted")
# CRIPT_PROMO_MASTER
'''
--OLD---
CREATE SET TABLE CRMP.CRIPT_PROMO_MASTER ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
ID INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
NO CYCLE),
DEVICE VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_LOCATION VARCHAR(64) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_BANNER INTEGER,
PROMO_CODE VARCHAR(128) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_DETAILS VARCHAR(4000) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_START VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_END VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_DETAILS_URL VARCHAR(1024) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_TERMS VARCHAR(4000) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_TEMPLATE VARCHAR(512) CHARACTER SET LATIN NOT CASESPECIFIC,
CREATED_DATE DATE FORMAT 'YY/MM/DD' DEFAULT DATE ,
MODIFIED_DATE DATE FORMAT 'YY/MM/DD',
CREATED_BY VARCHAR(64) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT 'CRIPT_SCRAPER',
MODIFIED_BY VARCHAR(64) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT 'CRIPT_SCRAPER')
PRIMARY INDEX ( PROMO_CODE );
--NEW---
CREATE SET TABLE CRMP.CRIPT_PROMO_MASTER ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
ID INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
NO CYCLE),
-- DEVICE VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC,
-- PROMO_LOCATION VARCHAR(64) CHARACTER SET LATIN NOT CASESPECIFIC,
-- PROMO_BANNER INTEGER,
PROMO_CODE VARCHAR(128) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_ID VARCHAR(128) CHARACTER SET LATIN NOT CASESPECIFIC,
-- PROMO_DETAILS VARCHAR(4000) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_START VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC,
PROMO_END VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC,
-- PROMO_DETAILS_URL VARCHAR(1024) CHARACTER SET LATIN NOT CASESPECIFIC,
-- PROMO_TERMS VARCHAR(4000) CHARACTER SET LATIN NOT CASESPECIFIC,
-- PROMO_TEMPLATE VARCHAR(512) CHARACTER SET LATIN NOT CASESPECIFIC,
CREATED_DATE DATE FORMAT 'YY/MM/DD' DEFAULT DATE ,
MODIFIED_DATE DATE FORMAT 'YY/MM/DD',
CREATED_BY VARCHAR(64) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT 'CRIPT_SCRAPER',
MODIFIED_BY VARCHAR(64) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT 'CRIPT_SCRAPER')
PRIMARY INDEX ( PROMO_ID );
'''
# time.sleep(3)
# print("Merging into CRMP.CRIPT_PROMO_MASTER ")
merge_master = """
MERGE INTO
CRMP.CRIPT_PROMO_MASTER AS TGT
USING
(SELECT PROMO_CODE, PROMO_ID, PROMO_START, PROMO_END, CREATED_DATE FROM CRMP.CRIPT_PROMO_STG) AS SRC
ON
(
TGT.PROMO_CODE = SRC.PROMO_CODE
and TGT.PROMO_ID = SRC.PROMO_ID
and TGT.CREATED_DATE = SRC.CREATED_DATE
)
WHEN MATCHED THEN
UPDATE SET
MODIFIED_DATE = CURRENT_TIMESTAMP
WHEN NOT MATCHED THEN
INSERT
(PROMO_CODE, PROMO_ID, PROMO_START, PROMO_END)
VALUES
(SRC.PROMO_CODE, SRC.PROMO_ID, SRC.PROMO_START, SRC.PROMO_END);
"""
# cursor = session.execute(merge_master)
#print("Merge into CRMP.CRIPT_PROMO_MASTER complete.")
'''
# write json to file
with open('promo_data.json', 'w') as outfile:
json.dump(promo_master, outfile)
f = open('promo_data.json')
data = json.load(f)
f.close()
f = csv.writer(open('promo_extract.csv', 'w+'))
# add header
f.writerow(['DEVICE'
, 'PROMO_LOCATION'
, 'PROMO_BANNER'
, 'PROMO_CODE'
, 'PROMO_DETAILS'
, 'PROMO_START'
, 'PROMO_END'
, 'PROMO_DETAILS_URL'
, 'PROMO_TERMS'
, 'PROMO_TEMPLATE'
])
# add content
for item in data:
f.writerow([item['device']
, item['promo_location']
, item['promo_banner']
, item['promo_code']
, item['promo_details']
, item['promo_start']
, item['promo_end']
, item['promo_url']
, item['promo_terms']
, item['promo_template']]
)
# file produced at local
'''