maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Sync Picasaweb to Directory (https://talk.maemo.org/showthread.php?t=27504)

brontide 2009-03-14 21:27

Sync Picasaweb to Directory
 
Here is a python script that can run on the device and will syncronize a users images with the device. It pulls the server side resized images at your preferential size and will not re-download images already on the device.

It doesn't delete images or handle half-downloaded images, but for the most part it works, just rerun when you have new images to download.

Code:

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

# Try importing python gdata bindings from the system and then libs subdirectory
try:
        import gdata.photos.service
        import gdata.media
        import gdata.geo
except:
        sys.path.append('libs')
        import gdata.photos.service
        import gdata.media
        import gdata.geo


gd_client = gdata.photos.service.PhotosService()

def sync ( username, prefix, limit = None, imgmax = 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, imgmax)
       

def sync_album ( album, prefix, imgmax ):
        album_dir = os.path.join(prefix,"%d" % (album.timestamp.datetime().year), "%d-%02d-%02d %s" % (album.timestamp.datetime().year, album.timestamp.datetime().month, album.timestamp.datetime().day, album.title.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 = []

        if not imgmax:
                photos = gd_client.GetFeed(album.GetPhotosUri())
        else:
                photos = gd_client.GetFeed("%s&imgmax=%s" % (album.GetPhotosUri(), imgmax ) )

        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())

# users files to sync, where to load them on the device and the sclaing you want used.  See the gdata docs for possible resizing strings.
sync('ericewarnke', '/media/mmc1/Pictures/', imgmax = '800u')

Hopefully someone can make use of this script.


All times are GMT. The time now is 14:45.

vBulletin® Version 3.8.8