Skip to content

Commit

Permalink
Amazon: Special logic for Amazon Fresh orders, which lack a direct link
Browse files Browse the repository at this point in the history
to invoice on the Orders page.
  • Loading branch information
Zburatorul committed Nov 11, 2021
1 parent 70e9385 commit 5ca428b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion finance_dl/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def invoice_finder():

order_ids = set()
for invoice_link in invoices:
if "nvoice" not in invoice_link.text:
# Amazon Fresh, and regular orders respectively
if invoice_link.text not in ("View order", "View invoice"):
continue
href = invoice_link.get_attribute('href')
m = re.match('.*[&?]orderID=((?:D)?[0-9\\-]+)(?:&.*)?$', href)
Expand Down Expand Up @@ -264,8 +265,22 @@ def get_source():
source = self.driver.page_source
if 'Grand Total:' in source:
return source
if 'Final Details for Order' not in source:
return source
return None

page_source, = self.wait_and_return(get_source)
if "Final Details for Order" not in page_source:
# every invoice has this phrase
logger.info(" Found possible Amazon Fresh order. Falling back to direct invoice URL.")
tokens = href.split("/")
tokens = tokens[:4]
tokens[-1] = f"gp/css/summary/print.html?orderID={order_id}"
href = "/".join(tokens)
logger.info(f"Looking for invoice at: {href}")
with self.wait_for_page_load():
self.driver.get(href)

page_source, = self.wait_and_return(get_source)
if order_id not in page_source:
raise ValueError('Failed to retrieve information for order %r'
Expand Down

0 comments on commit 5ca428b

Please sign in to comment.