1
0
Fork 0

commerce: Use modal form to download CSV

The Commerce Bank website no longer allows navigating directly to
`Download.ashx`; doing so just returns a generic "we're sorry" error.
They appear to have added some CSRF protection or something that makes
this not work.  As a result, we have to go fill out the form on the
*Download Transactions* modal dialog in order to get the download to
work correctly.
master
Dustin 2023-07-13 18:01:34 -05:00
parent 3b432fc7d6
commit 805aa40e20
1 changed files with 18 additions and 10 deletions

View File

@ -421,21 +421,29 @@ class CommerceBank:
self, from_date: datetime.date, to_date: datetime.date self, from_date: datetime.date, to_date: datetime.date
) -> Path: ) -> Path:
log.info('Downloading transactions from %s to %s', from_date, to_date) log.info('Downloading transactions from %s to %s', from_date, to_date)
idx = self.page.url.rstrip('/').split('/')[-1] datefmt = '%m/%d/%Y'
href = ( self.page.get_by_role('link', name='Download Transactions').click()
f'Download.ashx?Index={idx}' self.page.wait_for_timeout(random.randint(750, 1250))
f'&From={from_date}&To={to_date}' modal = self.page.locator('#download-transactions')
f'&Type=csv' input_from = modal.locator('input[data-qaid=fromDate]')
'&DurationOfMonths=6' input_from.click()
) self.page.keyboard.press('Control+A')
log.debug('Navigating to %s', href) self.page.keyboard.press('Delete')
self.page.keyboard.type(from_date.strftime(datefmt))
input_to = modal.locator('input[data-qaid=toDate]')
input_to.click()
self.page.keyboard.press('Control+A')
self.page.keyboard.press('Delete')
self.page.keyboard.type(to_date.strftime(datefmt))
modal.get_by_role('button', name='Select Type').click()
self.page.get_by_text('Comma Separated').click()
with self.page.expect_download() as di: with self.page.expect_download() as di:
self.page.evaluate(f'window.location.href = "{href}";') self.page.get_by_role('button', name='Download').click()
log.debug('Waiting for download to complete') log.debug('Waiting for download to complete')
self.page.wait_for_timeout(random.randint(1000, 3000))
path = di.value.path() path = di.value.path()
assert path assert path
log.info('Downloaded transactions to %s', path) log.info('Downloaded transactions to %s', path)
modal.get_by_label('Close').click()
return path return path
def firefly_import(self, csv: Path, account: int, token: str) -> None: def firefly_import(self, csv: Path, account: int, token: str) -> None: