Wednesday, October 17, 2012

Python Script to Merge "My Archives" Call and SMS Logs in Google Drive


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:

  1. 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.
  2. It gets the list of files in the appropriate folder of Google Drive matching the year and month that it needs to work on.
  3. Downloads each file in "ods" format.
  4. Runs unoconv to convert them to csv.
  5. Merges the csv files
  6. Uploads the merged files back into Google Drive
  7. 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.

  1. 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/
  2. 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.
  3. 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.
  4. 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.
  5. Place unoconv.py in the same folder as the above script.
  6. 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.


2 comments:

Vaibhav Panwar said...

AWSM !! I have downloaded your app. and I would later review. Thanks

Vaibhav Panwar said...

Wow it Worked !! nice app. bro ! liked it totally.
I Need to talk about python language, can i have ur email-add. ?