Runar Ovesen Hjerpbakk

Science-based software development

Python

Finding financial transactions in Gmail using Python

I’ve created a simple Python script that parses emails in your Gmail inbox, finds financial transactions and adds them to a simple .csv file. The .csv file is easy to import in financial software such as MoneyWiz or iBank.

Transactions

For now only iTunes transactions are parsed, but the script can easily be extended to other receipts. I think Amazon Kindle receipts are next in line.

The code is available on GitHub.

Instructions

easy_install beautifulsoup4

  • Add Gmail username, password and a financial account name
  • Run the script

python ExportTransactionsFromGmailToCsv.py

Read More

Send an email via Gmail using Python

Sending an email programmatically from a Gmail account is easy using Python and the smtplib library. The following snippet shows a function written in Python that sends an email with a chosen subject and a body as plain text.

#!/usr/bin/python

import smtplib
import string

def send_email(to_address, subject, body):
	from_address = ''
	username = from_address
	password = ''
	email = string.join((
        'From: %s' % from_address,
        'To: %s' % to_address,
        'Subject: %s' % subject,
        '',
        body
        ), '\r\n')
	server = smtplib.SMTP('smtp.gmail.com:587')
	server.ehlo()
	server.starttls()
	server.login(username, password)
	server.sendmail(from_address, to_address, email)
	server.quit()

Line 7 through 16 creates the email message. Since the from_address will be used as the Gmail username, the from_address and password need to be valid Gmail credentials. The to_address, subject and mail body is given to the function as parameters.

Line 17 gets the SMTP connection and ehlo() identifies us to the ESMTP server using EHLO (Extended HELLO). starttls() puts the SMTP connection in TLS (Transport Layer Security) mode. All SMTP commands that follow will be encrypted.

Line 20 through 22 logs us in, sends the email and finally terminates the SMTP session.

Read More

Optimize SVG images for retina displays

As high DPI screens are becoming more and more common, it is necessary to improve the images around the web to look great on these new screens and not just the low DPI screens of the past. Guides like this can be used when dealing with ordinary raster images.

VectorBitmapExample

Another solution is to create vector graphics, for example in the SVG format. Vector images can look equally great in any resolution and at any DPI. The caveat is that the image depicted must be usable even when rendered using few pixels on low DPI displays. Thus the format is best used for icons and images without a lot of stuff going on. In the example to the right you see that the upper text on the bottle is unreadable when zoomed out.

After you have created your SVG image, you should make sure that it has the smallest file size possible. Just as we have done with raster images since forever by using tools like ImageOptim and its like.

To minimize the SVG's file size, I use the python script Scour - an SVG scrubber.

Scour is an open-source Python script that aggressively cleans SVG files, removing a lot of 'cruft' that certain tools or authors embed into their documents

This will decrease the file size with as much as 50%. Since I use the script on all of my SVG images before uploading them to my website, I've created an OS X service to run Scour from the Finder.

for f in "[email protected]"
do
    if [ ${f: -4} == ".svg" -o ${f: -4} == ".SVG" ]
    then			
    python /Applications/Utilities/scour/scour.py --enable-comment-stripping --create-groups --enable-id-stripping --shorten-ids --set-precision=5 --quiet --indent=none -i "$f" -o "$f 2"       
    mv "$f 2" "$f"
    fi
done

The service contains a shell script that checks if the image file selected really is an SVG image and runs the Scour with a couple of optional parameters. Then it replaces the original image with the optimized version. If you want to keep the original image, this step can be deleted.

Optimize SVG Automator Service

To use the service as is, Download the OS X service and Scour. The Scour script should be copied to /Applications/Utilities/scour/, before opening the service to install it. After the service is installed, you can right click on any SVG image in the Finder and choose Optimize SVG.

Optimize SVG Automator Service

Read More