#!/usr/bin/python """ Picasa Upload - A simple script to upload photos to Google Picasaweb service. Usage: picasa_upload.py [options] [file2.jpg]... Options -r Recurse through local sub-folders -c Use current directory name for folder name -h, --help Show this help -v, --version Version Examples: picasa_upload.py My_Photos image.jpg Uploads the file image.jpg to the folder 'My_Photos' picasa_upload.py My_Photos *.jpg Uploads all jpg-files in the current directory to the folder My_Photos picasa_upload.py -r -c *.jpg Uploads all jpg-files in the current directory and sub-directories to a folder with the same name as the current directory Homepage: http://wanted.eu.org/en/computers/linux/uploading_photos_to_picasaweb (en) http://wanted.eu.org/pl/komputery/linux/wrzucanie_zdjec_na_picasaweb (pl) """ __author__ = ('jscudder#google.com (Jeff Scudder), ' 'wanted#linux.gda.pl (Marcin Sochacki), ' 'ulrik.stervbo#gmail.com (Ulrik Stervbo)') __version__ = 'Version: 0.9' __date__ = 'Date: 2007/06/13 23:28:23' __license__ = 'Apache License 2.0' import gdata.service import gdata import atom import gdata.base import sys, os import getopt username = '' password = '' def usage(): print __doc__ def bad_options(): print('Error: Ambiguous options; folder or file name not specified.') def find_files(filesToUpload, dirname, files): for f in [os.path.join(dirname, file) for file in files]: if os.path.splitext(f)[1] in ('.jpg', '.JPG'): filesToUpload.append(f) def upload_picasa(folderName, fileList): gd_client = gdata.service.GDataService() gd_client.email = username gd_client.password = password gd_client.service = 'lh2' gd_client.source = 'GDataService upload script' try: gd_client.ProgrammaticLogin() except gdata.service.CaptchaRequired: fail('Required Captcha') except gdata.service.BadAuthentication: fail('Bad Authentication') except gdata_client.Error: fail('Login Error') # create the album print('Creating album %s' % folderName) gd_entry = gdata.GDataEntry() gd_entry.title = atom.Title(text=folderName) gd_entry.category.append(atom.Category( scheme='http://schemas.google.com/g/2005#kind', term='http://schemas.google.com/photos/2007#album')) album_entry = gd_client.Post(gd_entry, 'http://picasaweb.google.com/data/feed/api/user/' + username) # Create media-only ms = gdata.MediaSource() for file in fileList: try: print('Sending file %s' % file) ms.setFile(file, 'image/jpeg') media_entry = gd_client.Post(None, album_entry.GetFeedLink().href, media_source = ms) except gdata.service.RequestError: print('Failed sending %s' % file) def main(argv): fileList = [] folderName = '' try: opts, args = getopt.getopt(argv, 'hvrc', ['help', 'version', 'recursive']) except getopt.GetoptError: usage() sys.exit(2) if len(args) < 2 and len(opts) == 0: usage() sys.exit(2) # If there are two or more arguments to the script, we guess that it is # a folder name and one or more files. If the -r or -c flags are used # 'folderName' and 'fileList' will be appropiately set if len(args) >= 2 and len(opts) == 0: folderName = args[0] fileList = args[1:] for opt, arg in opts: if opt in ('-h', '--help'): usage() sys.exit() if opt in ('-v', '--version'): print __version__, __date__ sys.exit() if opt == '-c': folderName = os.path.basename(os.getcwd()) if len(args) > 0: fileList = args elif opt in ('-r', '--recursive'): os.path.walk(os.getcwd(), find_files, fileList) if len(args) == 1: folderName = args[0] if not (folderName and fileList): bad_options() sys.exit(2) upload_picasa(folderName, fileList) if __name__ == '__main__': main(sys.argv[1:])