Skip to content

Instantly share code, notes, and snippets.

@lidio601
Created May 18, 2015 12:49
Show Gist options
  • Save lidio601/45bea662807a4ead0053 to your computer and use it in GitHub Desktop.
Save lidio601/45bea662807a4ead0053 to your computer and use it in GitHub Desktop.
It was an alpha version of a script to mirror a remote Picasa album with a local directory
#!/usr/bin/python
"""
Alpha program to mirror a remote Picasa Album
with local directory
"""
__author__ = "fabiocigliano"
import gdata.photos
import gdata.media
import gdata.geo
import gdata.photos.service
import sys
import os
import cerealizer
# Configuration
conf = {
'username': 'YOUR-GOOGLE-ACCOUNT',
'password': 'YOUR-GOOGLE-PASSWORD',
'localPath': '/where/my/photos/are/on/local',
'objectFilename': '.picasa_sync.py.obj'
}
# this is bad =(
global gd_client
global albums
def getRemoteAlbums():
"""
List the remote albums
"""
global gd_client
als = gd_client.GetUserFeed(user=gd_client.email,kind='album',limit=1)
ris = {}
for album in als.entry:
ris[album.gphoto_id.text] = album
return ris
class picasaSyncMedia:
"""
Object to describe a remote media to synchronize
"""
lname = None
uid = '0'
ruri = None
kind = 'photo'
def __init__(self, fname, uniqid, remote_uri, filekind):
self.lname = fname
self.uid = uniqid
self.ruri = remote_uri
self.kind = filekind
class picasaSyncAlbum:
"""
Object to store/load a list of media to synchronize
"""
lname = None
lpath = None
gid = '0'
rname = None
rpath = None
photo = 0
media = []
def load(fname):
if not os.path.exists(fname):
return None
fp = fopen(fname,'r')
if not fp or fp == None:
return None
dumper = cerealizer.Dumper()
if not dumper or dumper == None:
return None
try:
toRet = dumper.undump(fp)
except Error:
return None
finally:
return toRet
def dump(self, fname):
fp = open(fname,'w')
if not fp or fp == None:
return False
dumper = cerealizer.Dumper()
cerealizer.register(picasaSyncAlbum)
if not dumper or dumper == None:
return False
try:
toRet = dumper.dump(self,fp)
except:
fp.close()
return False
finally:
fp.close()
return True
def writeSyncFile(directory, album_id):
"""
Crawl the remote album to mirror it with a local directory
"""
global albums
if not os.path.exists(directory):
return False
if not albums.has_key(album_id):
return False
so = picasaSyncAlbum()
so.lname = os.path.basename(directory)
so.lpath = directory
so.gid = album_id
photos = gd_client.GetFeed('/data/feed/api/user/%s/albumid/%s?kind=photo' % (conf['username'],so.gid))
so.media = []
for photo in photos.entry:
so.media.append(picasaSyncMedia(photo.title.text, str(photo.id.text.split('/')[-1]), photo.link[1].href, 'photo'))
so.rname = str(albums[album_id].title.text)
so.rpath = albums[album_id].link[0].href
so.photo = albums[album_id].numphotos.text
return so
def main():
if not os.path.exists(conf['localPath']):
print 'Picasa_Sync.py: Configuration Object says that localPath is "', conf['localPath'], '", but it doesn\'t exists!\nExiting...'
exit()
# Autenticazione di base su Picasa Web Album
global gd_client
gd_client = gdata.photos.service.PhotosService()
gd_client.email = conf['username']
gd_client.password = conf['password']
gd_client.source = 'picasa-sync-fabiocigliano-net'
gd_client.ProgrammaticLogin()
#gd_client.ClientLogin(conf['username'], conf['password'])
global albums
albums = getRemoteAlbums()
obj = writeSyncFile('YOUR-LOCAL-ALBUM-FOLDER','ALBUM-ID')
print obj
print obj.dump('/tmp/dump.txt')
print 'Scanning localPath for Albums Directories...'
#for root, dirs, files in os.walk(conf['localPath']):
# print root
#
# TODO: implement the rest of the code
#
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment