#!/usr/bin/env python2 # # Copyright 2006 Google, Inc. # All Rights Reserved. # # # A helper script that pushes an xml file to the feeder. # This example provides a simple feed function. # # This example is written in Python. # Code blocks are determined by indentation. # See www.python.org for more information. #Inspired by : http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306 # import mimetypes, sys,string,getopt,urllib2 def usage(): print """Usage: %s ARGS --datasource: name of the datasource --feedtype: full or incremental or metadata-and-url --url: xmlfeed url of the feedergate, e.g. http://gsabox:19900/xmlfeed --xmlfilename: The feed xml file you want to feed --help: output this message""" % sys.argv[0] def main(argv): """ Process command line arguments and send feed to the webserver """ try: opts, args = getopt.getopt(argv[1:], None, ["help", "datasource=", "feedtype=", "url=", "xmlfilename="]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) url = None datasource = None feedtype = None xmlfilename = None for opt, arg in opts: if opt == "--help": usage() sys.exit() if opt == "--datasource": datasource = arg if opt == "--feedtype": feedtype = arg if opt == "--url": url = arg if opt == "--xmlfilename": xmlfilename = arg params = [] if (url and xmlfilename and datasource and feedtype in ("full", "incremental", "metadata-and-url")): params.append(("feedtype", feedtype)) params.append(("datasource", datasource)) data=('data',xmlfilename,open(xmlfilename,'r').read()) request_url=post_multipart(url,params,(data,)) #print request_url.get_method() #print request_url.get_full_url() #print request_url.get_data() print urllib2.urlopen(request_url).read() else: usage() sys.exit(1) def post_multipart(theurl, fields, files): """ Create the POST request by encoding data and adding headers """ content_type, body = encode_multipart_formdata(fields, files) headers={} headers['Content-type']=content_type headers['Content-length']=str(len(body)) return urllib2.Request(theurl,body,headers) def encode_multipart_formdata(fields, files): """ Create data in multipart/form-data encoding """ BOUNDARY = '----------boundary_of_feed_data$' CRLF = '\r\n' L = [] for (key, value) in fields: L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="%s"' % key) L.append('') L.append(value) for (key, filename, value) in files: L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename)) L.append('Content-Type: %s' % get_content_type(filename)) L.append('') L.append(value) L.append('--' + BOUNDARY + '--') L.append('') body = CRLF.join(L) content_type = 'multipart/form-data; boundary=%s' % BOUNDARY return content_type, body def get_content_type(filename): """ Determine the content-type of data file """ return mimetypes.guess_type(filename)[0] or 'application/octet-stream' if __name__ == '__main__': main(sys.argv)