#! /usr/bin/python import os.path, sys, time, mimetypes, xmlrpclib, pprint, base64 pp = pprint.PrettyPrinter() config = { 'url': 'http://example.com/services/xmlrpc', 'username': 'developer', 'password': 'bZdKNTv7XB8C', } # Make initial connection to service, then login as developer server = xmlrpclib.Server(config['url'], allow_none=True); connection = server.system.connect(); session = server.user.login(connection['sessid'], config['username'], config['password']); sessid = session['sessid']; user = session['user']; timestamp = str(int(time.time())) ## Load a movie file - get size, mimetype filename = 'testfile.MOV' filesize = os.stat(filename).st_size filemime = mimetypes.guess_type(filename) fd = open(filename, 'rb') video_file = fd.read() fd.close() # Create file_obj with encoded file data file_obj = { 'file': base64.b64encode(video_file), 'filename': filename, 'filepath': 'sites/default/files/' + filename, 'filesize': filesize, 'timestamp': timestamp, 'uid': user['uid'], 'filemime': filemime, } # Save the file to the server try: f = server.file.save(sessid, file_obj) except xmlrpclib.Fault, err: print "A fault occurred" print "Fault code: %d" % err.faultCode print "Fault string: %s" % err.faultString else: pp.pprint(f) # DEBUG # Get the new file from the server (verify) DEBUG try: ff = server.file.get(sessid, f) except xmlrpclib.Fault, err: print "A fault occurred" print "Fault code: %d" % err.faultCode print "Fault string: %s" % err.faultString else: # We have the new file info - we need this in case the name of the file was changed when sotred on the server # pp.pprint(ff) # DEBUG del ff['file'] # We don't need this - free it node_files = { 'fid': f, 'filemime': filemime, 'filename': ff['filename'], 'filepath': ff['filepath'], 'filesize': filesize, 'status': '1', 'timestamp': timestamp, 'uid': user['uid'], } # Create the node object and refernce the new fid just created node = { 'type': 'video_local', 'status': 1, 'type': 'video_local', 'title': 'Remote Test ' + timestamp, 'body': 'This is a test created from a remote app', 'uid': user['uid'], 'name': user['name'], 'changed': timestamp, 'files': {}, 'field_video_overlay' : [ {'value': 'Here is some CCK field text'}, ], } # my latest attempt to figure out how to get node->files in the right format for the node.save method # please don't laugh - I am just grasping at straws here - ive tried all the lists and dictionary formats. node['files'][f] = node_files pp.pprint(node) # DEBUG try: n = server.node.save(sessid, node) nn = server.node.get(sessid,int(n),[]) except xmlrpclib.Fault, err: print "A fault occurred" print "Fault code: %d" % err.faultCode print "Fault string: %s" % err.faultString else: pp.pprint(n) # DEBUG pp.pprint(nn) # DEBUG