#!/usr/bin/env python
import sys
import os
from urllib import urlopen

import gdata.photos.service
import gdata.media
import gdata.geo

def find_photo(photo, search_photos):
    """
    Search for this photo in the stack of photos.
    """
    for sp in search_photos:
        if sp.title.text == photo.title.text:
            return True
    return False
def sync_album(dest_client=None, src_client=None, src_album=None):
    if dest_client is None or src_client is None or src_album is None:
        raise Exception('dest_client and src_client and src_album must be set')
    # First step figure out if the album is already in the dest clients
    dest_album = None
    search_albums = dest_client.GetUserFeed(user=dest_client.email)
    for a in search_albums.entry:
        if a.title.text == src_album.title.text:
            album_exists = True
            dest_album = a
            break
    # If couldn't find album then lets create it
    if dest_album is None:
        dest_album = dest_client.InsertAlbum(title=src_album.title.text, summary=src_album.summary.text)
        
    dest_feed_uri = '/data/feed/api/user/%s/albumid/%s?kind=photo' % (dest_client.user_name, dest_album.gphoto_id.text)
    dest_photos = dest_client.GetFeed(dest_feed_uri)
    # Alright now need to sync one image at a time.
    feed_uri = '/data/feed/api/user/%s/albumid/%s?kind=photo' % (src_client.user_name, src_album.gphoto_id.text)
    print('Feed uri is: %s' % feed_uri)
    src_photos = src_client.GetFeed(feed_uri)
    
    album_url = dest_album.GetFeedLink().href
    for photo in src_photos.entry:
        #print 'Photo: %s dir is %s' % (photo.title.text, dir(photo))
        # Figure out if photo exists or not 
        if not find_photo(photo, dest_photos.entry):
            
            print("Link to source location %s" % photo.content.src)
            # Ok could find document voodoo magic to get the download link photo.content.src
            tmp_file_path = '/tmp/%s' % photo.title.text
            urlfile = urlopen(photo.content.src)
            f = open(tmp_file_path, 'w')
            f.write(urlfile.read())
            f.close()
            
            # Finally add photo to destination album
            dest_photo = dest_client.InsertPhotoSimple(album_url, photo.content.src, '', tmp_file_path, content_type='image/jpeg')
            dest_photo.title.text = photo.title.text
            dest_photo.summary.text = photo.summary.text
            dest_photo.geo = photo.geo
            
            dest_client.UpdatePhotoMetadata(dest_photo)
            
            print('Ok uploaded %s' % photo.content.src)
            
     
src_client = gdata.photos.service.PhotosService()
src_client.email = sys.argv[1]
src_client.password = sys.argv[2]
src_client.user_name = sys.argv[3]
src_client.source = 'hackingthought-picasa-copy-account'
src_client.ProgrammaticLogin()


#print(dir(tmp_album))
    
dest_client = gdata.photos.service.PhotosService()
dest_client.email = sys.argv[4]
dest_client.password = sys.argv[5]
dest_client.user_name = sys.argv[6]
dest_client.source = 'hackingthought-picasa-copy-account'
dest_client.ProgrammaticLogin()
 
tmp_album = None
albums = src_client.GetUserFeed(user=src_client.email)
for album in albums.entry:
    
    print 'title: %s, number of photos: %s, id: %s' % (album.title.text,album.numphotos.text, album.gphoto_id.text)
    
    print "oh oh summary is %s" % album.summary.text
    sync_album(dest_client=dest_client, src_client=src_client, src_album=album)

    
