A script that serves two functions.
- Scrapes Mint for daily spend and income
- Sends me a text with my aggregrate and itemized spend/income every night at 10PM
I wanted to get better insight into my daily spending. There are many spending apps out there, but they offer more features than I want and lack the simple notifications I do want. This began as a weekend hack project that eventually grew into a more robust app driven by Plaid.
This solution is hacky! And I love hacky things :)
This is a general overview of how the script interacts with Mint
- The script simulates a browser with Capybara/Selenium
- The script visits the Mint homepage
- The script logs in with username/password
- Mint will then text you a login token to your phone
- The script finds the Mint text by looking through your local iMessage chats
- The script inputs the login token and completes authentication
- The script navigates to the transactions page
- The script collects all transactions for the current date
- The script creates a summary message of the transactions and uses Twilio to send you the summary message
- Ruby and Bundle
- Mint Account
- Twilio Account
- Macbook, iPhone, and a shared iMessage account between the two devices
- Clone the repo
$ git clone XXXXXXXXXXXXX
- Install gems
$ bundle i
-
Download both the chromedriver and geckodriver, and put them somewhere in your
PATH
. At the time of writing this doc, I'm using version 2.46 of the chromedriver but you can find the latest version here.If you want to run this app without making any changes, add these two executables to the directory
$HOME/.chromedriver
. Then add that directory to yourPATH
. -
Copy the env template and fill it out with your values
$ cp .env.template .env
There are two main ways to run this program.
Run Manually
This command will immediately send a text with today's spend/income. This is useful to test that everything is setup correctly.
$ bundle exec ruby main.rb
Run Periodically
The real reason I built this script was to receive a text every night with my spend/income. To do that I used cron.
When a script is run via cron it doesn't have the same PATH or other environment variables as when you are running the script yourself. For example, if I used this cron command...
0 0 22 * * bundle exec ruby /Users/codemang/Personal/mint/main.rb
It would use the system ruby. This would break for me, because I use a different version of ruby when developing. Gems that have been installed are scoped to the ruby version, so my system ruby would not find the required gems for this script. It would also likely complain that it couldn't find the chromedriver executable.
To solve this problem, I created a bash script main.sh which would first configure the environment. The three things that needed to be done were...
-
Use the correct ruby build. I use chruby to manage my ruby versions so you can see calls to their startup scripts.
-
Add chromedriver to the path
-
Call the script
# Add chromedriver to path
PATH=/Users/codemang/.chromedriver:$PATH
# Switch to using chruby ruby so gems can be found
source /usr/local/share/chruby/chruby.sh
source /usr/local/share/chruby/auto.sh
# Call the script
cd "$( dirname "$0" )"
bundle exec ruby main.rb schedule
Now I can add this to my crontab and the script will run successfully.
0 0 22 * * /Users/codemang/Personal/mint_notifier/main.sh
Lastly, since I only want to receive one text at 10PM, I could try to setup my cron as above. But what if I had my computer closed? The script wouldn't run and no text would be sent for the day.
To make this more resilient, I run the script every five minutes and track when texts have been sent to avoid double texting. If my computer is closed at 10PM but I open at 11PM, it will send the text then. Additionally, if my computer has been closed for multiple days, it will send texts for those missed days.
To enable this functionality, you just have to pass in schedule
as a command
line argument, which you can see in
main.sh. The
final cron command is...
*/5 * * * * /Users/codemang/Personal/mint_notifier/main.sh
If you want to view the browser as it is being automated, just call the script
with VISIBLE_BROWSER=true