Skip to content

Instantly share code, notes, and snippets.

@goldblatt
Created September 15, 2015 03:03
Show Gist options
  • Save goldblatt/b08c9b5b8ef852a2186a to your computer and use it in GitHub Desktop.
Save goldblatt/b08c9b5b8ef852a2186a to your computer and use it in GitHub Desktop.
basic code for the text message budget spending code
from twilio.rest import TwilioRestClient
import datetime
import settings
import mintapi # TODO: look into this repo
class TwilioHandler:
def __init__(self, mint_email, mint_password):
self._client = TwilioRestClient(settings.TWILIO_ACCOUNT, settings.TWILIO_TOKEN)
# TODO: I should def hash this password b/c this is unsafe
self.mint_data = MintData(mint_email, mint_password)
def send_text(self):
body = "Total Spend For Month: %s" % self._get_total_spend_for_month()
self._client.messages.create(
body=body,
to=settings.CELL_PHONE,
from_=settings.TWILIO_NUMBER
)
def _get_total_spend_for_month(self):
return self.mint_data.get_spending_up_till_day()
class MintData:
def __init__(self, email, password):
self.email = email
# TODO: I should def hash this password b/c this is unsafe
self.password = password
# TODO: do i even need this _cached_spending. yeah right?
self._cached_spending = None
self._connection = mintapi.Mint(self.email, self.password)
def _get_connection(self):
# TODO: this seems like a silly way to handle connections
if not self._connection:
self._connection = mintapi.Mint(self.email, self.password)
return self._connection
def get_spending_up_till_day(self):
if self._cached_spending:
return self._cached_spending
else:
return self._get_today_spending()
def _get_today_spending(self):
mint_conn = self._get_connection()
# TODO: store this in DB and only get if data is older than a day
transactions = mint_conn.get_transactions()
first_day_of_month = '%s-%s-01' % (datetime.datetime.now().year, datetime.datetime.now().month)
all_transactions_for_month = transactions[transactions['date'] > first_day_of_month]
sum = all_transactions_for_month['amount'].sum()
return sum
twilio_handler = TwilioHandler(settings.MINT_EMAIL, settings.MINT_PASSWORD)
twilio_handler.send_text()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment