Skip to content

Instantly share code, notes, and snippets.

@dangarbri
Last active December 24, 2022 16:54
Show Gist options
  • Select an option

  • Save dangarbri/9e993373aba85e7107b7a224426fb158 to your computer and use it in GitHub Desktop.

Select an option

Save dangarbri/9e993373aba85e7107b7a224426fb158 to your computer and use it in GitHub Desktop.
Continuously integrate transactions from American Express into the EveryDollar budgeting app.
"""
Uses selenium based APIs for American Express and Everydollar
continuously import transactions into everydollar.
Since everydollar doesn't natively support linking an American Express account
"""
from amex_selenium import AmexAPI
from everydollar_api import EveryDollarAPI
from time import sleep
from datetime import datetime, timedelta
# Create your own creds.py with these variables
from creds import amex_username, amex_password, everydollar_username, everydollar_password
UPDATE_FREQUENCY_SECONDS = 86400 # every 24 hours
pending_transactions = [] # Watch for when pending transactions clear
first_run = True # First run ignores any pending transactions and adds all cleared ones
iteration = 1
merchant_prefix = "Amex "
def matching(a, b):
"""
Check if transactions match
"""
if a.date == b.date and a.merchant == b.merchant and a.amount == b.amount:
return True
return False
def apply_prefix(merchant):
return "Amex " + merchant
while True:
print(f"Running iteration {iteration}")
iteration = iteration + 1
# Get webdrivers
amex = AmexAPI()
everydollar = EveryDollarAPI()
# Log in to each site
amex.login(amex_username, amex_password)
everydollar.login(everydollar_username, everydollar_password)
# Get transactions from amex
transactions = amex.get_recent_transactions()
for new_transaction in transactions:
# Ignore pending transactions.
# Only add them when they clear
if not new_transaction.pending:
if first_run:
print(f"Adding transaction {new_transaction}")
everydollar.add_transaction(new_transaction.date, apply_prefix(new_transaction.merchant), new_transaction.amount)
sleep(1) # wait for webdriver to be done... TODO: Move this into everydollar api
else:
# if it's not the first run, check if the transaction was pending
# If it wasn't pending, then it's already been added to everydollar
for transaction in pending_transactions:
if matching(transaction, new_transaction):
# Transaction has gone from pending to cleared, add it to everydollar
print(f"Adding transaction {new_transaction}")
everydollar.add_transaction(new_transaction.date, apply_prefix(new_transaction.merchant), new_transaction.amount)
sleep(1)
# Remove the pending transaction from the list
pending_transactions.remove(transaction)
else:
# Watch pending transactions so we can add them when they're cleared
pending_transactions.append(new_transaction)
# Close browsers/logout TODO: Add proper logout button
amex.close()
everydollar.close()
first_run = False
# Wait until it's time to run again
sleep(UPDATE_FREQUENCY_SECONDS)
@dangarbri
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment