Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Created July 31, 2013 03:27
Show Gist options
  • Save gogsbread/6119065 to your computer and use it in GitHub Desktop.
Save gogsbread/6119065 to your computer and use it in GitHub Desktop.
Lifehacker series 1) Remove empty albums( with zero photos) from Picasa 1) Upload your photos to Picassa(Google +)
#-- Algorithm---
# recurse thru all albums
# delete albums that have 0 photos
#-----------------
import os
import sys
import gdata.photos.service
import gdata.media
USERNAME = '' #TODO:Fill the username
gclient = gdata.photos.service.PhotosService()
gclient.email = '' #TODO:google email
gclient.password = '' #TODO:google password
gclient.source = 'lifehacker-delete empty albums'
gclient.ProgrammaticLogin()
albums = gclient.GetUserFeed(user=USERNAME)
for album in albums.entry:
if int(album.numphotos.text) == 0:
print '{0} deleted'.format(album.title.text)
gclient.Delete(album)
#-- Algorithm---
# recurse thru all subfolders inside the pictures folder
# create a album in google drive.
# name the album as the name of the folder
# go thru all files in the folder and add that to the album
#-----------------
import os
import sys
import gdata.photos.service
import gdata.media
import mimetypes
USERNAME = '' #TODO:Fill the username
BLACKLIST = []#TODO: List subdirectories that you don't want to upload
gclient = gdata.photos.service.PhotosService()
gclient.email = '' #TODO:google email
gclient.password = '' #TODO:google password
gclient.source = 'lifehacker-upload photos'
gclient.ProgrammaticLogin()
BASE_ALBUM_LOCATION = '' #TODO:Root of your pictures folder
albums = gclient.GetUserFeed(user=USERNAME)
BLACKLIST.extend([album.title.text.strip() for album in albums.entry]) # Prevents albums of the same name from being added.
for root,_,filenames in os.walk(BASE_ALBUM_LOCATION):
if filenames:
album_name = os.path.basename(root).strip()
print 'working on album:{0}'.format(album_name)
if album_name not in BLACKLIST:
try:
galbum = gclient.InsertAlbum(title=album_name,summary=album_name,access='private') #Creates a private album. Change to 'public' if needed.
galbum_url = '/data/feed/api/user/{0}/albumid/{1}'.format(USERNAME, galbum.gphoto_id.text)
for fname in filenames:
filepath = os.path.join(root,fname)
mime_type,_ = mimetypes.guess_type(filepath)
if mime_type:
print 'working on photo:{0}'.format(fname)
try:
gclient.InsertPhotoSimple(galbum_url,fname,fname,filepath,content_type=mime_type)
print 'completed photo:{0}'.format(fname)
except gdata.photos.service.GooglePhotosException as pexception:
print 'Error uploading photo:{0} reason:{1}'.format(filepath,pexception.message)
else:
print 'could not determine mimetype for {0}'.format(filepath)
print 'completed album:{0}'.format(album_name)
except gdata.photos.service.GooglePhotosException as pexception:
print 'Error during album upload {0} reason:{1}'.format(album_name,pexception.message)
except:
print 'unknown error during upload of album:{0}'.format(album_name)
else:
print 'skipping {0} as already found'.format(album_name)
print 'upload completed.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment