#!/usr/bin/env python
'''
PyFBUploader is a Python script for uploading Photos using the Facebook Platform
PyFBUploader is copyright 2008 Eric Stein

PyFBUploader is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Bash is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
License for more details.  The GPL version 2 is included as LICENSE.

$Id: fbupload 8 2008-05-19 01:36:41Z eastein $
'''

import facebook
import sys

# the following line is rewritten by the makefile
#SYSPATH#

import interact
import session

if __name__ == '__main__' :
	ntr = interact.Interact()

	# process arguments
	a = sys.argv
	if len(a) < 2 :
		print 'usage:\n\tfbupload 20080517_00{1,4,7}.jpg\n\tfbupload jims_wedding_*'
		sys.exit(1)
	
	# list files to upload
	print 'files:'
	files = []
	for i in range(1, len(a)) :
		print '  ' + a[i] 
		files.append(a[i])

	# create facebook platform connection
	fb = facebook.Facebook('564a94a584998894e3818cddd68375ff', '1296c50a5b85a8bd3c82d7d8435b1be5')
	fb.auth.createToken()

	# attempt to load a saved session
	s = session.getsession()

	# check session
	if s == None :
		# no existing session, get one
		fb.login()
		s = fb.auth.getSession()
		session.setsession(s)
	else :
		# load previous session
		fb.secret = s[u'secret']
		fb.session_key = s[u'session_key']
		fb.session_key_expires = 0

	# create a photo proxy and load list of albums
	pp = fb.photos
	albums = pp.getAlbums()

	# get user's choice of album
	cdic = {}
	adic = {}
	a = 0
	for ad in albums :
		a = a + 1
		aid = ad[u'aid']
		name = ad[u'name']
		adic[a] = aid
		cdic[a] = name

	print ''
	c = ntr.choices_menu('pick an album', cdic)
	aid = adic[c]

	print "uploading to " + str(aid) + ": " + cdic[c]

	# upload photos
	for f in files :
		print 'uploading: ' + f
		pp.upload(f, aid)
