-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
341 lines (283 loc) · 14.4 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
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
import os
import csv
import time
import shutil
from dotenv import load_dotenv
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from utils.ftp_utils import connect_ftp, download_files, archive_files_on_ftp
from utils.selenium_setup import get_driver
from login import fnet_login
from scrape_tracking import scrape_tracking
from utils.email_utils import send_email
from utils.gsheet_setup import setup_google_sheets, add_po_num_fnet_num_to_sheet
import re
load_dotenv()
def extract_order_number(confirmation_text):
match = re.search(r"#(\d+)", confirmation_text)
if match:
return match.group(1)
else:
return None
def place_orders():
try:
#setup sheets
sheet = setup_google_sheets()
# track success/failure
successful_orders = []
failed_orders = []
# archive directory setup
archive_dir = os.path.join(os.getenv('LOCAL_ORDERS_DIR'), 'processed')
os.makedirs(archive_dir, exist_ok=True)
# download files from ftp
ftp = connect_ftp()
downloaded_files = []
if ftp:
try:
downloaded_files = download_files(ftp)
if downloaded_files:
print(f"downloaded files: {downloaded_files}")
archive_files_on_ftp(ftp, downloaded_files)
# print(f'archived files on ftp: {downloaded_files}')
else:
print("no files downloaded")
finally:
ftp.quit()
print("FTP connection closed")
else:
print('could not connect to ftp')
return
if not downloaded_files:
print('no files to download. exiting')
return
driver = get_driver() #init selenium driver
# selenium shortcuts
short_wait = WebDriverWait(driver, 10)
long_wait = WebDriverWait(driver, 30)
def short_wait_for_element(by, value, short_wait=short_wait):
return short_wait.until(EC.element_to_be_clickable((by, value)))
def long_wait_for_element(by, value, long_wait=long_wait):
return long_wait.until(EC.element_to_be_clickable((by, value)))
def exit_iframe():
driver.switch_to.default_content()
# login
username = os.getenv("LOGIN_USERNAME")
password = os.getenv("LOGIN_PASSWORD")
login_success = fnet_login(driver, username, password)
if not login_success:
print("login failed.")
driver.quit()
return
# process each file loop
for file in downloaded_files:
try:
file_path = os.path.join(os.getenv('LOCAL_ORDERS_DIR'), file)
with open(file_path, 'r') as f:
reader = csv.DictReader(f)
orders_data = list(reader)
# group orders by PO
grouped_orders = {}
for row in orders_data:
po_num = row["PO_num"]
if po_num not in grouped_orders:
grouped_orders[po_num] = {
"shipping_info": {
"fname": row["First Name"],
"lname": row["Last Name"],
"address1": row["Ship To Address"],
"address2": row.get("Ship To Address 2", ""),
"city": row["Ship To City"],
"state": row["Ship To State"],
"zip": row["Ship To Zip"],
},
"items": []
}
grouped_orders[po_num]["items"].append({
"sku": row["SKU"],
"quantity": int(row["Qty"])
})
# process each order loop
for po_num, order in grouped_orders.items():
try:
print(f"processing PO_num: {po_num}")
# oos_items = False #OOS flag
# add items to cart loop
for item in order["items"]:
sku = item["sku"]
quantity = item["quantity"]
print(f"searching for sku {sku}")
search_input = long_wait_for_element(By.ID, "searchInput")
search_input.clear()
search_input.send_keys(sku)
search_input.submit()
long_wait.until(EC.presence_of_element_located((By.ID, "brandTitle")))
#if item is OOS, skip it, send an email and go to next PO (if there is one)
# try:
# oos_message = short_wait.until(EC.presence_of_element_located((By.ID, 'oos_message')))
# if oos_message.is_displayed():
# print(f'SKU: {sku} is out of stock. Skipping order submission for PO: {po_num}.')
# subject = f'FNET Items OOS'
# body = f'SKU: {sku} from PO: {po_num} in {file} is OOS.'
# send_email(subject, body)
# oos_items = True
# break
# except NoSuchElementException:
# pass
if quantity > 1:
print("inputting item quantity")
plus_qty = driver.find_element(By.ID, "quantBox")
plus_qty.clear()
plus_qty.send_keys(quantity)
print('adding item to cart')
add_to_cart_button = short_wait_for_element(By.ID, "addBagButton")
time.sleep(1)
add_to_cart_button.click()
time.sleep(1)
print(f"Added {quantity} of {sku} to cart.")
# if oos_items:
# failed_orders.append((file, po_num, 'OOS items found, entire PO skipped'))
# continue
print(f"done attempting to add items for PO_num {po_num}")
# checkout process
print("navigating to checkout page")
driver.get(os.getenv('CHECKOUT_PAGE_URL'))
shipping_info = order["shipping_info"]
short_wait_for_element(By.ID, 'shippingFields')
# fill shipping info
print('filling shipping info')
fields = {
'fname': shipping_info["fname"],
'lname': shipping_info["lname"],
'address1': shipping_info["address1"],
'address2': shipping_info["address2"],
'zip': shipping_info["zip"],
'city': shipping_info["city"]
}
for field_id, value in fields.items():
field = short_wait_for_element(By.ID, field_id)
field.clear()
field.send_keys(value)
# state dropdown
print('filling state')
state_field = short_wait_for_element(By.ID, "ship_state_drop")
state_select = Select(state_field)
state_select.select_by_value(shipping_info["state"])
# continue throgh checkout
print('clicking continue to shipping button')
continue_to_shipping_btn = short_wait_for_element(By.ID, "shippingProceedButton")
continue_to_shipping_btn.click()
print('selecting dropship shipping option')
dropship_shipping_btn = driver.find_element(By.ID, "DSP")
driver.execute_script("arguments[0].click();", dropship_shipping_btn)
print('clicking continue to payment button')
continue_to_payment_btn = short_wait_for_element(By.ID, "proceedCheckButton")
continue_to_payment_btn.click()
# payment iframes
iframes = WebDriverWait(driver, 30).until(
EC.presence_of_all_elements_located((By.CLASS_NAME, "js-iframe"))
)
# fill payment info
print('filling payment info')
driver.switch_to.frame(iframes[0])
card_field = long_wait_for_element(By.ID, "encryptedCardNumber")
card_field.clear()
card_field.send_keys(os.getenv('CC_NUM'))
exit_iframe()
time.sleep(1)
driver.switch_to.frame(iframes[1])
exp_field = short_wait_for_element(By.ID, "encryptedExpiryDate")
exp_field.clear()
exp_field.send_keys(os.getenv('CC_EXP_NUM'))
exit_iframe()
time.sleep(1)
driver.switch_to.frame(iframes[2])
csv_field = short_wait_for_element(By.ID, "encryptedSecurityCode")
csv_field.clear()
csv_field.send_keys(os.getenv('CC_CSV'))
exit_iframe()
time.sleep(1)
# submit order
print('submitting order')
submit_order_btn = short_wait_for_element(By.ID, "submitOrder")
submit_order_btn.click()
# verify order confirmation
print("waiting for confirmation...")
order_confirmation = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "h2.panel-title"))
)
print(f"confirmation found: {order_confirmation.text}")
print(f'PO_num {po_num} processed successfully')
# add the order number to a google sheet for shipment tracking
print('adding order info to google sheet')
fnet_order_num = extract_order_number(order_confirmation.text)
if fnet_order_num:
add_po_num_fnet_num_to_sheet(sheet, po_num, fnet_order_num, po_num_col=1, fnet_num_col=2)
print(f"order number extracted: {fnet_order_num} and added to sheet")
else:
print('order number not found')
# append file & po_num to success tracking
successful_orders.append((file, po_num))
time.sleep(5)
except Exception as e:
failed_orders.append((file, po_num, str(e)))
print(f"error processing order {po_num} from {file}: {e}")
except Exception as e:
print(f"error processing file {file}: {e}")
# close browser
driver.quit()
print("browser closed")
# archive the order files
for file in downloaded_files:
src = os.path.join(os.getenv('LOCAL_ORDERS_DIR'), file)
dst = os.path.join(archive_dir, file)
try:
shutil.move(src, dst)
print(f"moved {file} to archive")
except Exception as e:
print(f"failed to move {file}: {str(e)}")
# files_to_archive = []
# # determine which files were fully successful
# for file in downloaded_files:
# if any(file == f for f, _, _ in failed_orders):
# print(f"skipping FTP archiving for {file} due to failed orders.")
# else:
# files_to_archive.append(file)
# archive files locally
# for file in files_to_archive:
# src = os.path.join(os.getenv('LOCAL_ORDERS_DIR'), file)
# dst = os.path.join(archive_dir, file)
# try:
# shutil.move(src, dst)
# print(f"moved {file} to local archive.")
# except Exception as e:
# print(f"failed to move {file}: {str(e)}")
# archive files on FTP only if they were fully successful
# ftp = connect_ftp()
# if ftp and files_to_archive:
# try:
# archive_files_on_ftp(ftp, files_to_archive)
# print(f"archived files on FTP: {files_to_archive}")
# finally:
# ftp.quit()
# print("FTP connection closed")
# send summary email
print('sending summary email')
subject = "FNET Order Summary"
successful_msg = ', '.join(f'{po_num} ({f})' for f, po_num in successful_orders) if successful_orders else "None"
# failed_msg = ', '.join(f'{po_num} ({f}): {e}' for f, po_num, e in failed_orders) if failed_orders else "None"
failed_msg = ', '.join(f'{po_num} ({f})' for f, po_num in failed_orders) if failed_orders else "None"
body = f"""
Successful orders: {len(successful_orders)}
{successful_msg}
Failed orders: {len(failed_orders)}
{failed_msg}"""
send_email(subject, body)
except Exception as e:
send_email("FNET Bot Failed", f"FNET bot failed with error: {str(e)}")
raise
if __name__ == '__main__':
place_orders()
scrape_tracking()