2026-05-09 04:39:54 +00:00
|
|
|
from playwright.sync_api import sync_playwright, Playwright
|
2026-05-09 05:24:37 +00:00
|
|
|
import os
|
|
|
|
|
from dotenv import load_dotenv
|
2026-05-13 04:44:54 +00:00
|
|
|
import re
|
|
|
|
|
from datetime import datetime
|
2026-05-09 05:24:37 +00:00
|
|
|
|
|
|
|
|
load_dotenv()
|
2026-05-13 04:31:12 +00:00
|
|
|
class Scraper:
|
|
|
|
|
def __init__(self, playwright: Playwright, headless: bool = True):
|
|
|
|
|
self.playwright = playwright
|
|
|
|
|
self.firefox = self.playwright.firefox # or "firefox" or "webkit".
|
|
|
|
|
self.browser = self.firefox.launch(headless=headless)
|
|
|
|
|
self.page = self.browser.new_page()
|
|
|
|
|
self.response = self.page.goto(os.getenv("URL"))
|
|
|
|
|
self.page.fill("input#ctl00_ContentPlaceHolder1_txtLoginID", os.getenv("USERNAME"))
|
|
|
|
|
self.page.fill("input#ctl00_ContentPlaceHolder1_txtPassword", os.getenv("PASSWORD"))
|
|
|
|
|
self.page.click("input#ctl00_ContentPlaceHolder1_btnLogin")
|
2026-05-09 04:39:54 +00:00
|
|
|
|
2026-05-13 04:31:12 +00:00
|
|
|
def get_balance(self):
|
|
|
|
|
current_balance = self.page.locator("xpath=/html/body/form/div[3]/div[3]/div[2]/div[3]/div[5]/span[2]").inner_text()
|
|
|
|
|
return current_balance
|
2026-05-09 04:39:54 +00:00
|
|
|
|
2026-05-13 04:31:12 +00:00
|
|
|
def get_transactions(self):
|
|
|
|
|
self.page.click("xpath=/html/body/form/div[3]/div[3]/div[2]/div[3]/div[1]/span[2]/a")
|
|
|
|
|
transaction_body = self.page.locator("xpath=/html/body/form/div[3]/div[3]/div[2]/div/div[2]/div[3]/table/tbody").inner_text()
|
|
|
|
|
return transaction_body
|
2026-05-13 04:01:14 +00:00
|
|
|
|
2026-05-13 04:44:54 +00:00
|
|
|
def parse_transactions(self, raw: str = None) -> list[dict[str, any]]:
|
|
|
|
|
"""
|
|
|
|
|
Parse raw transactions text into a list of dicts:
|
|
|
|
|
[{'date': date, 'description': str, 'amount': float, 'balance': float}, ...]
|
|
|
|
|
Lines containing 'INSUFF FUNDS' (case-insensitive) are ignored.
|
|
|
|
|
If raw is None, get_transactions() is called to fetch the data.
|
|
|
|
|
"""
|
|
|
|
|
if raw is None:
|
|
|
|
|
raw = self.get_transactions() or ""
|
|
|
|
|
lines = [ln.strip() for ln in raw.splitlines() if ln.strip()]
|
|
|
|
|
pattern = re.compile(
|
|
|
|
|
r'^(?P<date>\d{2}-\d{2}-\d{4})\s+(?P<desc>.*?)\s+(?P<amount>[-\$\d,\.]+)\s+(?P<balance>[-\$\d,\.]+)\s*$'
|
|
|
|
|
)
|
|
|
|
|
parsed: list[dict[str, any]] = []
|
|
|
|
|
for ln in lines:
|
|
|
|
|
m = pattern.match(ln)
|
|
|
|
|
if not m:
|
|
|
|
|
# skip lines that don't match the expected pattern
|
|
|
|
|
continue
|
|
|
|
|
desc = m.group('desc').strip()
|
|
|
|
|
if 'INSUFF FUNDS' in desc.upper():
|
|
|
|
|
continue
|
|
|
|
|
def _to_float(s: str) -> float:
|
|
|
|
|
return float(s.replace('$', '').replace(',', '').strip())
|
|
|
|
|
try:
|
|
|
|
|
amount = _to_float(m.group('amount'))
|
|
|
|
|
balance = _to_float(m.group('balance'))
|
|
|
|
|
date_obj = datetime.strptime(m.group('date'), '%d-%m-%Y').date()
|
|
|
|
|
except Exception:
|
|
|
|
|
continue
|
|
|
|
|
parsed.append({
|
|
|
|
|
'date': date_obj,
|
|
|
|
|
'description': desc,
|
|
|
|
|
'amount': amount,
|
|
|
|
|
'balance': balance,
|
|
|
|
|
})
|
|
|
|
|
return parsed
|
|
|
|
|
|
2026-05-13 04:31:12 +00:00
|
|
|
def close(self):
|
|
|
|
|
self.browser.close()
|
2026-05-13 04:01:14 +00:00
|
|
|
|
2026-05-13 04:31:12 +00:00
|
|
|
#xpathbody=/html/body/form/div[3]/div[3]/div[2]/div/div[2]/div[3]/table/tbody
|
|
|
|
|
#xpathaccountbutton = /html/body/form/div[3]/div[3]/div[2]/div[3]/div[1]/span[2]/a
|
2026-05-13 04:01:14 +00:00
|
|
|
#xpath = /html/body/form/div[3]/div[3]/div[2]/div[3]/div[5]/span[2]
|
|
|
|
|
|