Wednesday, October 17, 2012

I had built an Android application that regularly archives call logs and SMSs sometime back. The application can be found at Google Play here.

The application allows one to set up a schedule to archive and upload call logs and SMS messages to Google Drive. If one has chosen a weekly or daily archive, there would be a lot of files in Google Drive. Though this is not a problem and Google Drive handles this perfectly, sometimes one may want to merge them for aesthetics sake.

I have been experimenting with python and the Google Drive API and have created a small python app that does just that. It is not really packaged to be used as an end-user, but I am just putting it up here to serve as reference. If you are interested in developing it further, I would be glad to help you in whichever way possible. In the worst case, this may serve you as a simple learning app for Google Drive Python API.

Here's what the script does:

If this is the first run, it authorizes to Google Drive. Since this is a console app, it would instruct a URL to be invoked in the browser and the result to be given to the app. It stores the credentials in a file so that it can be reused in future without you having to bother you.

It gets the list of files in the appropriate folder of Google Drive matching the year and month that it needs to work on.

Downloads each file in "ods" format.

Runs unoconv to convert them to csv.

Merges the csv files

Uploads the merged files back into Google Drive

Moves the old files in Google Drive to trash.

If you are ready to try out the script, here's how to go about it.

The program needs at least Python 2.6. For python 3, it would need slight modifications. If you do not have python, install it from http://python.org/

For converting spreadsheets to csv files, I have used openoffice and a script called unoconv. Openoffice can be invoked in a headless environment (no UI) with command line parameters to be used just as a document converter. And unoconv.py provides an easy platform independent interface to detect and invoke openoffice.

You will need to enable the Google Drive API for your account and enable the Google Drive python library. Follow the steps mentioned in the Google Drive SDK quick start here. You may follow the quickstart to test out whether your setup works.

Download the archives merge script from github here and replace the values of CLIENT_ID and CLIENT_SECRET with what you created in step 3.

Place unoconv.py in the same folder as the above script.

Now you can run the script with year (YYYY) and month (MM) numbers you wish to merge. But before you do that, you may want to go through the next section to know what the script does.