fixed a typo 'tody', added type hints, PEP8'd all the things

This commit is contained in:
2017-05-23 22:11:55 -04:00
parent 580a6e7252
commit 1bc6010e75

View File

@@ -1,39 +1,52 @@
import datetime import datetime
import os import os
import time import time
from typing import List
from twilio.rest import Client as TwilioClient from twilio.rest import Client as TwilioClient
import plaid from plaid import Client as PlaidClient
CHECK_FOR_NEW_TRANSACTIONS_EVERY_X_MINUTES = 5 CHECK_FOR_NEW_TRANSACTIONS_EVERY_X_MINUTES = 5
ALERT_FOR_TRANSACTIONS_GTE = 500 ALERT_FOR_TRANSACTIONS_GTE = 500
MY_CELL = os.getenv('MY_CELL') MY_CELL = os.getenv('MY_CELL')
MY_TWILIO_NUM = os.getenv('MY_TWILIO_NUM') MY_TWILIO_NUM = os.getenv('MY_TWILIO_NUM')
PLAID_ENV = os.getenv('PLAID_ENV') or 'sandbox'
plaid_client = plaid.Client(client_id=os.getenv('PLAID_CLIENT_ID'), secret=os.getenv('PLAID_SECRET'),
public_key=os.getenv('PLAID_PUBLIC_KEY'), environment=os.getenv('PLAID_ENV'))
twilio_client = TwilioClient(os.getenv('TWILIO_SID'), os.getenv('TWILIO_TOKEN'))
# this is an access ID from Plaid for a specific user and bank/credit account # this is an access ID from Plaid for a specific user and bank/credit account
PLAID_ACCESS_ID = os.getenv('PLAID_ACCESS_ID') PLAID_ACCESS_ID = os.getenv('PLAID_ACCESS_ID')
plaid_client = PlaidClient(client_id=os.getenv('PLAID_CLIENT_ID'),
secret=os.getenv('PLAID_SECRET'),
public_key=os.getenv('PLAID_PUBLIC_KEY'),
environment=PLAID_ENV)
twilio_client = TwilioClient(os.getenv('TWILIO_SID'),
os.getenv('TWILIO_TOKEN'))
transaction_ids = set() transaction_ids = set()
def get_latest_transactions(): def get_latest_transactions() -> List[dict]:
today = datetime.date.tody().strftime('%Y-%m-%d') """get 100 most recent transactions"""
today = datetime.date.today().strftime('%Y-%m-%d')
return [transaction return [transaction
for transaction in plaid_client.Transactions.get(PLAID_ACCESS_ID, '1972-01-01', today)['transactions'] for transaction in plaid_client.Transactions.get(
PLAID_ACCESS_ID, '1972-01-01', today)['transactions']
if transaction['transaction_id'] not in transaction_ids] if transaction['transaction_id'] not in transaction_ids]
def alert(transaction): def alert(transaction: dict) -> None:
message = (f'hey, a transaction hit your account that exceeds ${ALERT_FOR_TRANSACTIONS_GTE}: ' message = (
f'{transaction["date"]} {transaction["name"]} ${transaction["amount"]}') 'hey, a transaction hit your account that exceeds '
twilio_client.api.account.messages.create(to=MY_CELL, from_=MY_TWILIO_NUM, body=message) f'${ALERT_FOR_TRANSACTIONS_GTE}: '
f'{transaction["date"]} {transaction["name"]} ${transaction["amount"]}')
twilio_client.api.account.messages.create(to=MY_CELL,
from_=MY_TWILIO_NUM,
body=message)
def main(): def main() -> None:
while True: while True:
for transaction in get_latest_transactions(): for transaction in get_latest_transactions():
transaction_ids.add(transaction['transaction_id']) transaction_ids.add(transaction['transaction_id'])