Posts Tagged With: Code

Google Finance killed my weekend project

One annoying thing about Google Finance is that there’s no way to see how your portfolio’s value has changed over time. You can see your cost basis and the current value but none of the intermediate values. Way back when I used to log into MSN Money and record the value in a spreadsheet every day. That didn’t last very long.

Over the weekend I built a tool that keeps track of how much your portfolio’s worth. Every night at midnight it would grab the current value of all your portfolios and store it in my database, and then when you visited my site again, it would display a chart with all of the daily values of your portfolios. I got the entire backend working, and got to the point where the app was displaying all of the daily values on the page in plain text. Here’s my cost basis and a week’s worth of daily values, with some numbers blurred.

This weekend I was going to turn those numbers into a pretty Annotated Time Line using the Google Chart API. Unfortunately (or fortunately), I checked Google Finance today and it turns out that they just built the portfolio value chart into their main product.

Google Finance portfolio view

It’s pretty cool that they put the chart in there; obviously it would have been nice to have visitors to my own site, and I built it so that anyone can log in and check the values, but also a little deflating, that my project won’t be useful for people. Guess I’ll have more time to work on my other projects. In the meantime though, I got better at XML parsing, learned a ton about OAuth, implemented an app with user-specific data for the first time, and got better at programming.

You can grab the code I was using here, or check your own portfolio here.

To learn how you can outperform the Dow by 10%, or to learn more about the app, you should email me here.

Liked what you read? I am available for hire.

Escaping from your email

I only download my email once every four hours. Otherwise I check it too much. This works really well and my "need to check" email addiction has totally shut off.

For this to work on your own email, you want to set up a Gmail account with an address that no one knows, as well as access to a cron task scheduler. Cron is an application that runs tasks on a set schedule, so every minute or every year or every Monday at 5pm, etc. If you have Mac or Linux, and you're the type of person that keeps their computer on 24/7, you can run "cron jobs" from your local machine (you want to edit the /etc/crontab file - here are some instructions). I use my web hosting provider's cron service, because it will run even when my laptop is shut down.

Here's the script:

 
import imaplib
from datetime import datetime
import email
import smtplib
import sys
import os
import random
import base64

def main():
    #check user
    if os.isatty(sys.stdin.fileno()):
        #i'm trying to get my email by cheating, not a cron
        print "Are you sure you want to do this?"
        ans = raw_input()
        if ans != "yes":
            return
        no_of_mults = 3
        for i in range(no_of_mults):
            a = random.randint(1000,9999)
            b = random.randint(1000,9999)
            print "What is " + str(a) + " * " + str(b) + "?"
            ans = float(raw_input())
            if ans != a*b:
                print "Sorry, incorrect answer."
                return

    imap_domain = "imap.gmail.com"
    imap_port = 993
    imap_username =  base64.b64decode('my_gmail_username_encoded')
    imap_password = base64.b64decode('my_gmail_pass_encoded')

    #smtp settings
    smtp_domain = "my_smtp_host.com"
    smtp_port = 587
    smtp_username = "my_smtp_user"
    smtp_password = "my_smtp_pass"

    email_recipients = ['kburkeorg@gmail.com']

    imap_server = imaplib.IMAP4_SSL(imap_domain, imap_port)
    imap_server.login(imap_username, imap_password)

    server = smtplib.SMTP(smtp_domain, smtp_port)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(smtp_username, smtp_password)

    imap_server.select('INBOX')

    status, email_ids = imap_server.search(None, '(UNSEEN)')

    recipient = email_recipients

    count = 0
    for e in email_ids[0].split(' '):
        if e is not '':
            try:
                raw_msg = imap_server.fetch(e, '(RFC822)')
                msg = email.message_from_string(raw_msg[1][0][1])

                #modify reply-to so we preserve email address
                if not msg['Reply-To']:
                    msg['Reply-To'] = msg['From']

                result = server.sendmail(msg['From'], recipient, msg.as_string())
                count += 1

            except smtplib.SMTPRecipientsRefused, e:
                print "SMTPRecipientRefused" + str(e)
                continue
            except Exception, err:
                print err
                continue

    # so I can see how many messages were printed
    print "Sent " + str(count) + " messages."
    filename = "/home/kevinburke/webapps/b/sent.txt"
    with open(filename, 'w') as f:
        f.write(str(count))
    print datetime.now()

    server.quit()

if __name__ == "__main__":
    main()

Then in your cron file, you want to add the following line:

0 */4 * * * /usr/bin/python /path/to/email/file.py

where

/path/to/email/file
is the filepath to wherever you saved the script above. The 0 means run the script at the top of the hour, and the */4 means run it once every 4 hours.

Liked what you read? I am available for hire.

Losing all your work

You've been typing away on an essay in Microsoft Word when the whole thing crashes, and you lose all your work. You wail, gnash your teeth, yell at the CMC tech staff and then commiserate with everyone you know. Losing all your work is probably a positive development however, if you have some extra time. It forces you through a second iteration of your work, which should be clearer and sharper than your first because you are visiting the ideas for a second time. Losing all your work on paper though doesn't mean you lose it in your head, and you also probably have an outline or some other reference document to go on. The other option is to write all of your essays from the command line and use version control to save all of your drafts. It's much less prone to crashes than Word.

Liked what you read? I am available for hire.