Saturday, May 26, 2007

Learning Python

I've been teaching myself Python the last few weeks. It's the scripting language of choice for Google and a lot of their APIs have Python interfaces. My first attempt to play around with it was replacing my Outlook calendar VBScripts with Python scripts that read Google calendar. We're now syncing our Treos to Google Calendar, so we have no need for Outlook's calendar. The Google Calendar API page provides sample code to interface to it and I modified it to fit our needs.

The next thing I did was to replace my Perl script that renames pictures from our digital cameras with a Python script. It requires Exiftool to read the metadata from the photo file. It renames all the generic IMG_0001.jpg and DSC_0001.jpg file names with names based on the date they were created (061231-001.jpg, 061231-002.jpg, etc.). Script is below:

#
# renames original file names from camera files
# DSC.*.jpg or IMG.*.jpg to a name based on the date, ie 071231-001.jpg
#

import os
import re

#
# formats the extension to 3 characters based on the number of pictures taken that date
# ie 001, 010, 100, etc
#
def imageCount(value):
if value<10:
return '00'+str(value)
elif value<100:
return '0'+str(value)
else:
return str(value)

#
# set work directory
#
os.chdir('c:\\images')
#
# list all files
#
dirContents = os.listdir('.')
#
# set the image file name pattern
#
imagePattern = re.compile('^[img|dsc].*.jpg',re.IGNORECASE)

#
# create list of images based matching the above pattern
#
imageList = [image for image in dirContents if imagePattern.search(image)]

#
# iterate through the original names to rename the files
#
for oldName in imageList:
#
# shell call to get exif info creation date of image
#
exifOut = os.popen('c:\\bin\\exiftool -CreateDate '+ oldName + ' -d %y%m%d').readlines()

#
# above output comes back as list, so iterate through list
#
for exif in exifOut:
#
# get rid of trailing newline
#
exif = exif.replace('\n','')
#
# split to get just the creation date
#
exifDate = exif.split(' : ',2)

#
# now loop through and check to see if we have other images with the
# filename we want to rename to. assume we won't go over 100
# pictures from that day
#
for i in range(100):
#
# create new file name
#
newName = exifDate[1] + '-' + imageCount(i+1) + '.jpg'
#
# now test if the proposed filename exists since we don't want
# to rename over an existing file
#
fileStat = os.path.exists(newName)
#
# if it doesn't exist we can rename
#
if fileStat==False:
print 'renaming ' + oldName + ' to ' + newName
#
# rename and break out of loop
#
os.rename(oldName,newName)
break

Saturday, May 12, 2007

google Calendar API

I'm now using OggSync for Windows Mobile to sync my phone's calendar to my gcal. My next step is to remove my dependence on Outlook for calendaring. I started playing around the with the Google Calendar API using Python. I've been meaning to learn Python so here's a good opportunity. I've since removed a couple scripts from my system for accessing Outlook and replaced them with Python scripts that interact with gcal.

I had some problems with sCU beta testing and X10 reliability. Turns out the problem was the phone cable used to connect my TC+ to the powerline interface. I replaced it and all's well again.