This script uploads the pics in the specified directory into the specified album in picasa. The script requires gdata libraries installed. Check code.google.com.
I had written and tested the code in linux platform. Hasn't been tested in windows.
Any suggestions to add more functionality, or add a UI or any improvements would be great.

#!/usr/bin/python

###
# This script serves to upload the pictures from the specified directory to the specified album in picasa.
# Usage : python picasaUploader -l <source directory> 
###
import gdata.photos.service
import gdata.media
import gdata.geo
import os
import dircache
import sys
import getopt

###
# Method that performs all initializations and insertions into the album
###
def init(path):
	print 'Uploading pics from path ', (path)
	DIR_UPLOAD=path
	gd_client = gdata.photos.service.PhotosService()
	gd_client.email = 'jason.cosmo' #type your username here
	gd_client.password = os.environ['PASSWRD'] # store your password in an environment variable called PASSWD
	gd_client.source = 'python uploader'
	gd_client.ProgrammaticLogin()
	username=gd_client.email
	index = 0
	albums = gd_client.GetUserFeed(user=username)
	albumidlist = []
	# add each album in the list
	for album in albums.entry:
		index = index + 1
		albumidlist.append(album.gphoto_id)
		print '%d %s' %(index,album.title.text)  

	#This choice would be the index of the album chosen from the list
	choice = int(raw_input('Choose the album you want to insert the pics in:'))
	album_url = '/data/feed/api/user/%s/albumid/%s' %(username, albumidlist[choice-1].text)

	#store the references to the pics in the
	#directory in the list
	try:
		pics_list = dircache.listdir(DIR_UPLOAD)
	except:
		print 'Enter a valid directory'		
		usage()
	count = 0
	#upload the pics one by one
	for pic in pics_list:
		filename = DIR_UPLOAD+'/'+pic
		try:
			photo = gd_client.InsertPhotoSimple(album_url, pic, str(count)+'JPG', filename,content_type='image/jpeg')
			count = count + 1
			print count
		except:
			print 'Count not upload', Photo

###
# Method that shows the script usage
###
def usage():
	print 'Usage : python picasaUploader.py -l <directory path>'
	exit(0)


if __name__=='__main__':
	path = ''
	try:
		opts, args = getopt.getopt(sys.argv[1:], "l:v",["directory"])
		for option in opts:
			if option[0] == '-l':
				path = option[1]				
	except:
		usage()

	init(path)

Dani AI

Generated

A few practical notes to bring this 2010-era solution up to a safe, maintainable state.

The approach in ’s script (username/password + ProgrammaticLogin and gdata) relied on older Google services that have been retired or changed—Picasa Desktop/Web and parts of the Picasa Web Albums API were retired when Google consolidated on Google Photos. See Google’s announcement: Moving on from Picasa. (googlephotos.blogspot.com)

Storing a Google password in an environment variable (asked by ) is better than hard‑coding, but it’s still unsafe for long‑term or production use. Google no longer accepts ClientLogin (username/password) for modern APIs; apps should use OAuth 2.0 and the Google identity libraries instead. Use an OAuth client (desktop/web flow) and keep the refresh token in a protected store (OS keyring or a file with 0600 permissions). Official OAuth guidance is here. (cloud.google.com)

If the goal is programmatic uploads today, migrate to the Google Photos Library API: upload raw bytes to the uploads endpoint, receive an upload token, then call mediaItems.batchCreate to add the item to the user’s library/album. The API docs explain the two‑step flow, accepted MIME types, batching limits (50 items per batch), resumable uploads for large files, and retry/backoff guidance—follow those best practices for robust behaviour. (developers.google.com)

Minimal practical checklist (in order): create a Google Cloud project, enable the Photos Library API, create OAuth credentials, run an OAuth flow (google-auth / google-auth-oauthlib), upload bytes to /v1/uploads and call mediaItems.batchCreate, and add file filtering, MIME checks, logging, error handling and exponential backoff. Example sketch (not a drop‑in) to show the basic flow:

from google_auth_oauthlib.flow import InstalledAppFlow
import requests, json

flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', scopes=['https://www.googleapis.com/auth/photoslibrary.appendonly'])
creds = flow.run_local_server(port=0)

# Step 1: upload bytes -> uploadToken
r = requests.post('https://photoslibrary.googleapis.com/v1/uploads',
    headers={'Authorization':f'Bearer {creds.token}','Content-type':'application/octet-stream','X-Goog-Upload-Protocol':'raw'},
    data=open('photo.jpg','rb').read())
upload_token = r.text

# Step 2: create media item with uploadToken (use batchCreate)

If continuing to support legacy users of Picasa data, instruct them to migrate or access the Album Archive; otherwise rework tools around the Google Photos Library API and OAuth flow documented above. (googlephotos.blogspot.com)

Recommended Answers

All 2 Replies

Is it secure to store the password as an environment variable?

Is it secure to store the password as an environment variable?

It sure isn't. But as the above script was written quickly and I thought it would be better to have it as an environment variable instead of putting it in the code, I couldn't think of a better alternative.
You are safe though, if you terminate the shell as soon as the program ends :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.