eodsfetch / eodsfetch /

#!/usr/bin/env python

# $ Id: $

import urllib2, sys
from ClientForm import ParseResponse
from optparse import OptionParser
from urlparse import urljoin
import logging

def getArgs( ):
	parser = OptionParser()
	parser.add_option( "-u", "--url", dest="url", default="", help="URL for the admin site" )
	parser.add_option( "-n", "--username", default="admin", dest="username", help="Username with export priveleges" )
	parser.add_option( "-p", "--password", default="pa55word", dest="password", help="Password" )
	parser.add_option( "-l", "--log-level", dest="log_level", default="info", help="level should be DEBUG, INFO, WARNING, ERROR" )
	parser.add_option( "-c", "--retrieve-command", default="newhires/", help="Web site path to retrieval command (relative to url)" )
	parser.add_option( "-s", "--save-file", help="filename to save data" )
	return parser.parse_args()

def setupLogging():
	global log
	log = logging.getLogger( 'eodsfetch' )
	logging.basicConfig( level = getattr( logging, options.log_level.upper() ) )

def getLoginPage():
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor)
	urllib2.install_opener( opener ) 'Retrieving %s for login', options.url )
	login_page = urllib2.urlopen( options.url )
		login_form = ParseResponse( login_page, backwards_compat=False )[0]
	except IndexError:
		log.exception( 'Did not find any forms on the login page (%s)', options.url ) "Check for <br/> elements, which don't follow the spec (use <br />)." )
	log.debug( 'login form follows' )
	log.debug( str( login_form ) )
	return login_form

def submitLoginPage( login_form ):
	login_form['userName'] = options.username
	login_form['password'] = options.password
	log.debug( 'Submitting login form' )
	# consider checking the response above for some confirmation value (string "Main Menu" ?)

def runRetrievalCommand():
	command = urljoin( options.url, options.retrieve_command ) 'Assuming login was successful, retrieving %s', command )
	result = urllib2.urlopen( command ).read()
		output = open( options.save_file, 'w' ) 'Writing response to file %s', options.save_file )
	except TypeError:
		log.warning( 'Writing response to console' )
		output = sys.stdout
	output.write( result )

def main():
	global options
	options, args = getArgs()
	login_form = getLoginPage()
	submitLoginPage( login_form )

if __name__ == '__main__':