Last active
December 24, 2022 16:54
-
-
Save dangarbri/9e993373aba85e7107b7a224426fb158 to your computer and use it in GitHub Desktop.
Continuously integrate transactions from American Express into the EveryDollar budgeting app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Imports are from:
amex_selenium: https://github.com/Appsolutely-Wonderful/amex-selenium-api
everydollar_api: https://github.com/Appsolutely-Wonderful/everydollar-selenium-api