# # LICENSE: # # Copyright (c) © 2009, Isaac Greenspan # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. # * Neither the name of 2718.us nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # import xmlrpclib import md5 import sys import pickle import types class pyLJxmlrpc: def fixBinaryResult(self,theResult): if theResult.__class__ == types.DictType: return dict([(k,self.fixBinaryResult(v)) for k,v in theResult.items()]) elif theResult.__class__ == types.ListType: return [self.fixBinaryResult(v) for v in theResult] elif theResult.__class__ == xmlrpclib.Binary: return unicode(theResult.data,'utf-8') else: return theResult def fixDictInput(self,theInput): if theInput.__class__ == types.DictType: return dict([(k,self.fixDictInput(v)) for k,v in theInput.items()]) #elif theInput.__class__ == objc.lookUpClass('NSCFDictionary'): #return self.fixDictInput(dict(theInput)) elif theInput.__class__ == types.ListType: return [self.fixDictInput(v) for v in theInput] elif theInput.__class__ == xmlrpclib.Binary: return theInput else: return theInput def call_withParams_atURL_forUser_withPassword_(self, methodName, params, serverURL, username, password): #print(u"--entering call_withParams_atURL_forUser_withPassword_") paramDict = self.fixDictInput(params) proxy = xmlrpclib.ServerProxy(serverURL) challenge = proxy.LJ.XMLRPC.getchallenge()["challenge"] hexdpass = md5.new(password).hexdigest() response = md5.new(challenge + hexdpass).hexdigest() paramDict.update({u'auth_method': u'challenge', u'username': username, u'auth_challenge': unicode(challenge), u'auth_response': unicode(response), u'ver': 1}) # for reasons that aren't entirely clear, pickling and unpickling the paramDict # seems to clean up any left-over objc datatypes and make xmlrpclib happier try: paramPickled = pickle.dumps(paramDict) paramDict = pickle.loads(paramPickled) except TypeError, err: print(u"couldn't pickle: %r" % paramDict) pass else: try: fn = getattr(proxy.LJ.XMLRPC,methodName) theResult = fn(paramDict) except xmlrpclib.Fault, err: print("XMLRPClib Fault") print("Fault code: %d" % err.faultCode) print("Fault string: %s" % err.faultString) except xmlrpclib.ProtocolError, err: print("A protocol error occurred") print("URL: %s" % err.url) print("HTTP/HTTPS headers: %s" % err.headers) print("Error code: %d" % err.errcode) print("Error message: %s" % err.errmsg) else: #print(u"---\n%r\n---" % theResult) return self.fixBinaryResult(theResult) #return theResult