Reply
Thread Tools
brontide's Avatar
Posts: 868 | Thanked: 474 times | Joined on Oct 2007 @ Capital District, NY, USA
#1
I hate keeping files in sync. I much prefer it happen automagically. So I have created a script to download my picasa albums automatically. This is the first step in creating a full application, but I figured I would share it in case other developers find it useful or inspiring. It's not shabby for a few hours of hacking. The gdata stuff is really quite slick.

You will need the python2.5-runtime and python-gdata to run this.

Code:
#!/usr/bin/python2.5
import sys
import os
import urllib2

# Try importing it from the system and then from the named subdir
try:
        import gdata.photos.service
        import gdata.media
        import gdata.geo
except:
        sys.path.append('gdata.py-1.1.0/src')
        import gdata.photos.service
        import gdata.media
        import gdata.geo


gd_client = gdata.photos.service.PhotosService()

def sync ( username, prefix, limit = None ):
        prefix = os.path.join(prefix, username)
        if not os.path.isdir(prefix):
                print "Creating directory %s" % (prefix)
                os.makedirs(prefix)
        
        album_list = gd_client.GetUserFeed(user = username, limit = limit)
        for album in reversed(album_list.entry):
                sync_album(album, prefix)
        

def sync_album ( album, prefix ):
        album_dir = os.path.join(prefix, album.name.text);
        if not os.path.isdir(album_dir):
                print "Creating directory %s" % (album_dir)
                os.makedirs(album_dir)

        current_dir = set(os.listdir(album_dir))
        new_files = []

        photos = gd_client.GetFeed(album.GetPhotosUri())
        for photo in photos.entry:
                if not os.path.exists(os.path.join(album_dir, photo.title.text)):
                        print "Downloading: %s -> %s" % (photo.content.src, os.path.join(album_dir, photo.title.text) )
                        download(photo.content.src, os.path.join(album_dir, photo.title.text))
                new_files.append(photo.title.text)

        unlink = current_dir - set(new_files)
        for name in unlink:
                print "Deleting unused file %s" % (os.path.join(album_dir,name))
                os.remove(os.path.join(album_dir,name))

def download ( url, destination ):
        out = open(destination, 'w')
        out.write(urllib2.urlopen(url).read())
To execute a sync. The first argument is the picasa username, the second is the path where you want it to store the files, and the third is how many recent albums to take ( left blank = all ).

Code:
~ $ python2.5 -i PicSync.py 
>>> sync('ericewarnke','/media/mmc1/Photos/',1)
Creating directory /media/mmc1/Photos/ericewarnke
Creating directory /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ
Downloading: http://lh4.ggpht.com/ericewarnke/SEylJSZCv5I/AAAAAAAACF0/qNqPfjVNSrQ/20080608-CRW_0811.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0811.jpg
Downloading: http://lh3.ggpht.com/ericewarnke/SEylLKRiLpI/AAAAAAAACF4/I-KKY8Cj3co/20080608-CRW_0813.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0813.jpg
Downloading: http://lh6.ggpht.com/ericewarnke/SEylNPbZVmI/AAAAAAAACF8/ypo8b0Wj2gw/20080608-CRW_0821.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0821.jpg
Downloading: http://lh4.ggpht.com/ericewarnke/SEylPCvHQpI/AAAAAAAACGA/1KJU8aLqh0w/20080608-CRW_0823.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0823.jpg
Downloading: http://lh5.ggpht.com/ericewarnke/SEylRVDHCZI/AAAAAAAACGE/Y5-ECNzhYE0/20080608-CRW_0826.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0826.jpg
Downloading: http://lh3.ggpht.com/ericewarnke/SEylS9oDQXI/AAAAAAAACGI/p4KVric6Cio/20080608-CRW_0834.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0834.jpg
Downloading: http://lh4.ggpht.com/ericewarnke/SEylVFBDeOI/AAAAAAAACGQ/_1pzf9TLR0c/20080608-CRW_0840.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0840.jpg
Downloading: http://lh4.ggpht.com/ericewarnke/SEylXB6_lII/AAAAAAAACGU/91F6rY3zG0k/20080608-CRW_0845.jpg -> /media/mmc1/Photos/ericewarnke/GraftonLakesBBQ/20080608-CRW_0845.jpg
I'm planning on developing this into a full fledged background daemon that gets kicked off via cron, checks for updates and downloads them. It can't do private albums right now, pre-resized images, or many other tricks but they are all on my list.
 

The Following 3 Users Say Thank You to brontide For This Useful Post:
Greyghost's Avatar
Posts: 415 | Thanked: 44 times | Joined on Apr 2007 @ Austin, Texas
#2
Brontide, this would be awesome! I was using MaemoPicasa till it broke and would just love to have something like this. It seems like most people are using flikr, but I really like th google toolset and this is one of the key ones for me.

Given the quality of your work on GC Dialer (Brontide is what I call it) I look forward to seeing what you do with this. KISS, if you please
 
brontide's Avatar
Posts: 868 | Thanked: 474 times | Joined on Oct 2007 @ Capital District, NY, USA
#3
Yeah, I'm going to use this as a good excuse to delve into osso rpc stuff. I see three parts to this very simple application. A "service" that syncs picasa based on a simple config file ( path, username, number of albums, resolution limited ). All it will do is run through one pass with a few thrown off rpc messages about status. It would be little more then what you see above.

The second part will be a cronable/alarmd app that wakes up, tests the network connectivity and starts the sync.

The third is a front end for editing the config and starting/stopping a sync.

Feel free to use the tiny script as-is. It's braindead simple and required no authentication. Only a superficial understanding of python will get you sync'ing down a directory of images.


Anyone know a good application for viewing images stored in directory as album format? I've posted a bug/feedback against canola for not sorting the albums and quiver is a little awkward to me.

Last edited by brontide; 2008-07-09 at 03:53. Reason: copy-n-paste errot
 
Reply


 
Forum Jump


All times are GMT. The time now is 11:05.