die Seilerwerks

Chronicling Life, Love, Linux and Oracle database administration.

Moto RAZR + Bluetooth + Linux + Python = ObexCopier

with 7 comments

While in Schaumburg, Illinois last week for the Oracle DBA Workshop II, I was taking some photos on my Motorola RAZR camera phone, in the hopes of posting them for my 5-year-old daughter to see. I needed to clear up some misconceptions, since she was under the impression that I “sleep at the school.” I soon learned, however, that transferring photos from the RAZR one-at-a-time over bluetooth to my Fedora 8 laptop became tedious, and waiting for a response from the GUI file browser was just frustrating. Enter: ObexFTP.

Thanks to a tip from a friend, I found ObexFTP and, in my quest to force myself to learn python, set about crafting a script to do the following:

  • Transfer files based on a date (default to today).
  • Transfer all files in the directory.

So I’m presenting to you my first stab at it. Some of the hard-codings depend on how the RAZR stores photos in the micro-SD card. If anyone wants to submit enhancements or critiques, I’m all ears. Right now it just works for what I needed it to do.

#!/usr/bin/env python

# Don Seiler, don@seiler.us 

import obexftp, ConfigParser, os
from xml.etree.ElementTree import XML
from optparse import OptionParser
from datetime import date

# This script is dependent on the Moto Razr convention of naming
# pictures in an MM-DD-YYYY_XXXX.jpg format

# Users need to create ~/.obexcopier.ini with these variables defined
# [ObexCopier]
# device = 1A:2B:3C:4D:5E:6F
# channel = 6
# source_dir = /MMC(Removable)/motorola/shared/picture
# dest_dir = /media/pictures

# Read config from ~/.obexcopier.ini
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~/.obexcopier.ini'))

# Probably a waste of precious memory to store these again
device = config.get('ObexCopier','device')
channel = config.getint('ObexCopier','channel')
source_dir = config.get('ObexCopier','source_dir')
dest_dir = config.get('ObexCopier','dest_dir')

# Get today for default date
today = date.today().strftime("%m-%d-%y")

# Command-line handling to allow for date
parser = OptionParser()
parser.add_option("-d", "--date", dest="date", default=today, help="Grab pictures from this date, defaults to today [default: %default]",metavar="MM-DD-YY")
parser.add_option("-a", "--all", action="store_true", dest="all", default=False, help="Copy all files, regardless of date [default: %default]")
(options, args) = parser.parse_args()

# Connect to the client
print "Connecting to %s on channel %d" % (device, channel)
cli = obexftp.client(obexftp.BLUETOOTH)
cli.connect(device, channel)

# Get the list of files from the SD card picture dir
if options.all:
        print "Copying all files to disk"
else:
        print "Copying files from %s" % options.date

files_xml = cli.list(source_dir)
folder_listing = XML(files_xml)
files = folder_listing.findall('./file/')
for file in files:
        # Only handle pictures taken on the specified date
        if options.all or file.get('name').startswith(options.date):
                print "Copying %s" % file.get('name')
                data = cli.get(source_dir + '/' + file.get('name'))
                localfile = open(dest_dir + '/' + file.get('name'), 'wb')
                localfile.write(data)
                localfile.close()

# Disconnect and delete the client
cli.disconnect()
cli.delete

Written by Don Seiler

March 25, 2008 at 12:18 pm

Posted in linux

Tagged with , , , , ,

7 Responses

Subscribe to comments with RSS.

  1. I hope you’ll enjoy learning Python. What did you use until now for scripting? why did you decide to start using Python instead?

    And how do you do this excellent code formatting?

    prodlife

    March 25, 2008 at 6:21 pm

  2. Chen, the sourcecode formatting is done using the tag described at http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/ . Thanks to Tyler Muth for pointing that out to me previously.

    Until now I’d been doing mostly spaghetti perl code. I now try to use shell (bash) scripts when I can to keep things simple. A lot of people, including myself, tend to underestimate what shell scripts can do.

    I decided to use python mainly out of peer pressure. A number of LUG friends are very vocal python advocates, and I liked what I saw from earlier peeks.

    Don Seiler

    March 25, 2008 at 7:30 pm

  3. i want to use it on my commercial environment. any copyrights?

    ismail cakir

    March 26, 2008 at 1:20 am

  4. I guess consider it LGPL. Use it for whatever you like. Keep attribution if you redistribute it. Use it wisely, use it in peace.

    Don Seiler

    March 26, 2008 at 9:25 am

  5. [...] fancy code formatting looks better when Don Seiler is using it. Maybe its time for a new look for the blog. Posted in tips [...]

  6. Wow, for your first python hack you certainly picked a cool project! I have recently picked up the language and was not that adventurous. Have fun with python

    Jerry Rude

    July 28, 2008 at 5:53 pm

  7. Un post qui m’interesse, merci a vous !

    jeux moto

    January 25, 2010 at 10:57 am


Leave a Reply