-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.py
103 lines (85 loc) · 3.71 KB
/
orders.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
from robocorp.tasks import task
from robocorp import browser
from RPA.HTTP import HTTP
from RPA.Tables import Tables
from RPA.PDF import PDF
from RPA.Archive import Archive
import os
@task
def order_robots_from_RobotSpareBin():
"""
Orders robots from RobotSpareBin Industries Inc.
Saves the order HTML receipt as a PDF file.
Saves the screenshot of the ordered robot.
Embeds the screenshot of the robot to the PDF receipt.
Creates ZIP archive of the receipts and the images.
"""
open_robot_order_website()
# TODO: Archive the receipts into individual archives?
# archive_receipts()
def open_robot_order_website():
"""Navigates to the given URL and accesses the orders file"""
browser.goto("https://robotsparebinindustries.com/#/robot-order/")
close_annoying_modal()
orders = get_orders()
for order in orders:
fill_the_form(order)
def get_orders():
"""Downloads the orders file"""
http = HTTP()
http.download(url="https://robotsparebinindustries.com/orders.csv", overwrite=True)
library = Tables()
return library.read_table_from_csv(
"orders.csv", columns=["Order number", "Head", "Body", "Legs", "Address"]
)
def close_annoying_modal():
"""Closes the forced modal window upon entering the robot-ordering page"""
page = browser.page()
page.click(".btn-danger")
def fill_the_form(order):
"""Fills the order form, previews, and submits the order"""
page = browser.page()
page.select_option("#head", str(order["Head"]))
page.check("#id-body-"+str(order["Body"]))
# page.fill("placeholder=\"Enter the part number for the legs\"", str(order["Legs"]))
# "//button[@routerlink='/web/click']"
# page.locator("placeholder=Enter the part number for the legs").fill(str(order["Legs"]))
# page.fill("//button[@placeholder=\"Enter the part number for the legs\"]", str(order["Legs"]))
page.fill("input[placeholder='Enter the part number for the legs']", order["Legs"])
page.fill("#address", str(order["Address"]))
page.click("#preview")
while True:
page.click("#order")
if page.query_selector("#order-another"):
screenshot_path = screenshot_robot(str(order["Order number"]))
pdf_path = store_receipt_as_pdf(str(order["Order number"]))
embed_screenshot_to_receipt(screenshot=screenshot_path, pdf_file=pdf_path)
page.click("#order-another")
close_annoying_modal()
break
def store_receipt_as_pdf(order_number):
"""Saves the order as a pdf file"""
page = browser.page()
pdf = PDF()
order_html = page.locator("#receipt").inner_html()
pdf_path = os.path.join("output", "receipts", order_number, '.pdf')
pdf.html_to_pdf(order_html, pdf_path)
return pdf_path
def screenshot_robot(order_number):
"""Saves a screenshot of the order"""
# TODO: Currently, the browser is too small to screenshot the entire image of a robot
page = browser.page()
screenshot_path = os.path.join("output", "receipts", order_number, '.png')
page.locator("#robot-preview-image").screenshot(path=screenshot_path)
return screenshot_path
def embed_screenshot_to_receipt(screenshot, pdf_file):
"""Embeds a screenshot of the robot into the pdf file of the receipt"""
pdf = PDF()
pdf.add_watermark_image_to_pdf(screenshot, pdf_file, pdf_file)
# TODO: deal with ValueError: No files found to archive
# def archive_receipts():
# """Archives the receipts"""
# lib = Archive()
# # receipt_path = os.path.join(".", "output", "receipts")
# # archive_path = os.path.join(".", "output", "receipts.zip")
# lib.archive_folder_with_zip("./output/receipts", "./output/receipts.zip")