Пример #1
0
 def complete(response) :
     if response.error:
         return callback(None)
     obj = json.loads(response.buffer.getvalue())
     pc = PubnubCrypto()
     out = []
     if self.cipher_key :
         if requestType == "history" :
             if type(obj) == type(list()):
                 for item in obj:
                     if type(item) == type(list()):
                         for subitem in item:
                             encryptItem = pc.decrypt(self.cipher_key, subitem )
                             out.append(encryptItem)
                     elif type(item) == type(dict()):
                         outdict = {}
                         for k, subitem in item.iteritems():
                             encryptItem = pc.decrypt(self.cipher_key, subitem )
                             outdict[k] = encryptItem
                             out.append(outdict)
                     else :         
                         encryptItem = pc.decrypt(self.cipher_key, item )
                         out.append(encryptItem)
                 callback(out)
             elif type( obj ) == type(dict()):
                 for k, item in obj.iteritems():
                     encryptItem = pc.decrypt(self.cipher_key, item )
                     out.append(encryptItem)
                 callback(out)    
         else :
             callback(obj)
     else :        
         callback(obj)        
Пример #2
0
        def complete(data):
            try    : obj = json.loads(data)
            except : obj = None

            pc = PubnubCrypto()
            out = []
            if self.cipher_key :
                if requestType == "history" :
                    if type(obj) == type(list()):
                        for item in obj:
                            if type(item) == type(list()):
                                for subitem in item:
                                    encryptItem = pc.decrypt(self.cipher_key, subitem )
                                    out.append(encryptItem)
                            elif type(item) == type(dict()):
                                outdict = {}
                                for k, subitem in item.iteritems():
                                    encryptItem = pc.decrypt(self.cipher_key, subitem )
                                    outdict[k] = encryptItem
                                    out.append(outdict)
                            else :         
                                encryptItem = pc.decrypt(self.cipher_key, item )
                                out.append(encryptItem)
                        callback(out)
                    elif type( obj ) == type(dict()):
                        for k, item in obj.iteritems():
                            encryptItem = pc.decrypt(self.cipher_key, item )
                            out.append(encryptItem)
                        callback(out)    
                else :
                    callback(obj)
            else :        
                callback(obj)        
Пример #3
0
            def sub_callback(response):
                ## STOP CONNECTION?
                if not self.subscriptions[channel]['connected']:
                    return

                ## CONNECTED CALLBACK
                if not self.subscriptions[channel]['first']:
                    self.subscriptions[channel]['first'] = True
                    connectcb()

                ## PROBLEM?
                if not response:

                    def time_callback(_time):
                        if not _time:
                            reactor.callLater(time.time() + 1, substabizel)
                            return errorback("Lost Network Connection")
                        else:
                            reactor.callLater(time.time() + 1, substabizel)

                    ## ENSURE CONNECTED (Call Time Function)
                    return self.time({'callback': time_callback})

                self.subscriptions[channel]['timetoken'] = response[1]
                substabizel()

                pc = PubnubCrypto()
                out = []
                for message in response[0]:
                    if self.cipher_key:
                        if type(message) == type(list()):
                            for item in message:
                                encryptItem = pc.decrypt(self.cipher_key, item)
                                out.append(encryptItem)
                            message = out
                        elif type(message) == type(dict()):
                            outdict = {}
                            for k, item in message.iteritems():
                                encryptItem = pc.decrypt(self.cipher_key, item)
                                outdict[k] = encryptItem
                                out.append(outdict)
                            message = out[0]
                        else:
                            message = pc.decrypt(self.cipher_key, message)
                    else:
                        message

                    callback(message)
Пример #4
0
            def sub_callback(response):
                if not self.subscriptions[channel]['first']:
                    self.subscriptions[channel]['first'] = True
                    connectcb()

                ## STOP CONNECTION?
                if not self.subscriptions[channel]['connected']:
                    return

                ## PROBLEM?
                if not response:

                    def time_callback(_time):
                        if not _time:
                            self.timeout(1, _subscribe)
                            return errorback("Lost Network Connection")
                        else:
                            self.timeout(1, _subscribe)

                    ## ENSURE CONNECTED (Call Time Function)
                    return self.time({'callback': time_callback})

                self.timetoken = response[1]
                _subscribe()

                pc = PubnubCrypto()
                out = []
                for message in response[0]:
                    callback(self.decrypt(message))
Пример #5
0
    def history(self, args):
        """
        #**
        #* History
        #*
        #* Load history from a channel.
        #*
        #* @param array args with 'channel' and 'limit'.
        #* @return mixed false on fail, array on success.
        #*

        ## History Example
        history = pubnub.history({
            'channel' : 'hello_world',
            'limit'   : 1
        })
        print(history)

        """
        ## Capture User Input
        limit = args.has_key('limit') and int(args['limit']) or 10
        channel = str(args['channel'])

        ## Fail if bad input.
        if not channel:
            print('Missing Channel')
            return False
        ## Get History
        pc = PubnubCrypto()
        return self._request(
            ['history', self.subscribe_key, channel, '0',
             str(limit)], args['callback'])
Пример #6
0
            def sub_callback(response):
                ## STOP CONNECTION?
                if not self.subscriptions[channel]['connected']:
                    return

                ## CONNECTED CALLBACK
                if not self.subscriptions[channel]['first'] :
                    self.subscriptions[channel]['first'] = True
                    connectcb()

                ## PROBLEM?
                if not response:
                    def time_callback(_time):
                        if not _time:
                            reactor.callLater( 1, substabizel )
                            return errorback("Lost Network Connection")
                        else:
                            reactor.callLater( 1, substabizel )

                    ## ENSURE CONNECTED (Call Time Function)
                    return self.time({ 'callback' : time_callback })

                self.subscriptions[channel]['timetoken'] = response[1]
                substabizel()

                pc = PubnubCrypto()
                out = []
                for message in response[0]:
                     if self.cipher_key :
                          if type( message ) == type(list()):
                              for item in message:
                                  encryptItem = pc.decrypt(self.cipher_key, item )
                                  out.append(encryptItem)
                              message = out
                          elif type( message ) == type(dict()):
                              outdict = {}
                              for k, item in message.iteritems():
                                  encryptItem = pc.decrypt(self.cipher_key, item )
                                  outdict[k] = encryptItem
                                  out.append(outdict)
                              message = out[0]
                          else:
                              message = pc.decrypt(self.cipher_key, message )
                     else :
                          message
                        
                     callback(message)
Пример #7
0
    def decrypt(self, message):
        if self.cipher_key:
            pc = PubnubCrypto()
            if type(message) == type(list()):
                for item in message:
                    encryptItem = pc.decrypt(self.cipher_key, item)
                    out.append(encryptItem)
                message = out
            elif type(message) == type(dict()):
                outdict = {}
                for k, item in message.iteritems():
                    encryptItem = pc.decrypt(self.cipher_key, item)
                    outdict[k] = encryptItem
                    out.append(outdict)
                message = out[0]
            else:
                message = pc.decrypt(self.cipher_key, message)

        return message
Пример #8
0
    def decrypt(self, message):
        if self.cipher_key:
            pc = PubnubCrypto()
            if type( message ) == type(list()):
                for item in message:
                    encryptItem = pc.decrypt(self.cipher_key, item )
                    out.append(encryptItem)
                message = out
            elif type( message ) == type(dict()):
                outdict = {}
                for k, item in message.iteritems():
                    encryptItem = pc.decrypt(self.cipher_key, item )
                    outdict[k] = encryptItem
                    out.append(outdict)
                message = out[0]
            else:
                message = pc.decrypt(self.cipher_key, message)

        return message
Пример #9
0
 def complete(response):
     if response.error:
         return callback(None)
     obj = json.loads(response.buffer.getvalue())
     pc = PubnubCrypto()
     out = []
     if self.cipher_key:
         if requestType == "history":
             if type(obj) == type(list()):
                 for item in obj:
                     if type(item) == type(list()):
                         for subitem in item:
                             encryptItem = pc.decrypt(
                                 self.cipher_key, subitem)
                             out.append(encryptItem)
                     elif type(item) == type(dict()):
                         outdict = {}
                         for k, subitem in item.iteritems():
                             encryptItem = pc.decrypt(
                                 self.cipher_key, subitem)
                             outdict[k] = encryptItem
                             out.append(outdict)
                     else:
                         encryptItem = pc.decrypt(self.cipher_key, item)
                         out.append(encryptItem)
                 callback(out)
             elif type(obj) == type(dict()):
                 for k, item in obj.iteritems():
                     encryptItem = pc.decrypt(self.cipher_key, item)
                     out.append(encryptItem)
                 callback(out)
         else:
             callback(obj)
     else:
         callback(obj)
Пример #10
0
        def complete(data):
            if ( type(data) == type(str()) ):
                if self.resulting_is:
                    d = zlib.decompressobj(16+zlib.MAX_WBITS)

            try     :   data = d.decompress(data) # try/catch here, pass through if except
            except  :   data = data

            try    : obj = json.loads(data)
            except : obj = None

            pc = PubnubCrypto()
            out = []
            if self.cipher_key :
                if requestType == "history" :
                    if type(obj) == type(list()):
                        for item in obj:
                            if type(item) == type(list()):
                                for subitem in item:
                                    encryptItem = pc.decrypt(self.cipher_key, subitem )
                                    out.append(encryptItem)
                            elif type(item) == type(dict()):
                                outdict = {}
                                for k, subitem in item.iteritems():
                                    encryptItem = pc.decrypt(self.cipher_key, subitem )
                                    outdict[k] = encryptItem
                                    out.append(outdict)
                            else :
                                encryptItem = pc.decrypt(self.cipher_key, item )
                                out.append(encryptItem)
                        callback(out)
                    elif type( obj ) == type(dict()):
                        for k, item in obj.iteritems():
                            encryptItem = pc.decrypt(self.cipher_key, item )
                            out.append(encryptItem)
                        callback(out)
                else :
                    callback(obj)
            else :
                callback(obj)
Пример #11
0
    def encrypt(self, message):
        if self.cipher_key:
            pc = PubnubCrypto()
            out = []
            if type( message ) == type(list()):
                for item in message:
                    encryptItem = pc.encrypt(self.cipher_key, item ).rstrip()
                    out.append(encryptItem)
                message = json.dumps(out)
            elif type( message ) == type(dict()):
                outdict = {}
                for k, item in message.iteritems():
                    encryptItem = pc.encrypt(self.cipher_key, item ).rstrip()
                    outdict[k] = encryptItem
                    out.append(outdict)
                message = json.dumps(out[0])
            else:
                message = json.dumps(pc.encrypt(self.cipher_key, json.dumps(message)).replace('\n',''))
        else :
            message = json.dumps(message)

        return message;
Пример #12
0
    def encrypt(self, message):
        if self.cipher_key:
            pc = PubnubCrypto()
            out = []
            if type(message) == type(list()):
                for item in message:
                    encryptItem = pc.encrypt(self.cipher_key, item).rstrip()
                    out.append(encryptItem)
                message = json.dumps(out)
            elif type(message) == type(dict()):
                outdict = {}
                for k, item in message.iteritems():
                    encryptItem = pc.encrypt(self.cipher_key, item).rstrip()
                    outdict[k] = encryptItem
                    out.append(outdict)
                message = json.dumps(out[0])
            else:
                message = json.dumps(
                    pc.encrypt(self.cipher_key,
                               json.dumps(message)).replace('\n', ''))
        else:
            message = json.dumps(message)

        return message
Пример #13
0
        def complete(data):
            if (type(data) == type(str())):
                if self.resulting_is:
                    d = zlib.decompressobj(16 + zlib.MAX_WBITS)

            try:
                data = d.decompress(
                    data)  # try/catch here, pass through if except
            except:
                data = data

            try:
                obj = json.loads(data)
            except:
                obj = None

            pc = PubnubCrypto()
            out = []
            if self.cipher_key:
                if requestType == "history":
                    if type(obj) == type(list()):
                        for item in obj:
                            if type(item) == type(list()):
                                for subitem in item:
                                    encryptItem = pc.decrypt(
                                        self.cipher_key, subitem)
                                    out.append(encryptItem)
                            elif type(item) == type(dict()):
                                outdict = {}
                                for k, subitem in item.iteritems():
                                    encryptItem = pc.decrypt(
                                        self.cipher_key, subitem)
                                    outdict[k] = encryptItem
                                    out.append(outdict)
                            else:
                                encryptItem = pc.decrypt(self.cipher_key, item)
                                out.append(encryptItem)
                        callback(out)
                    elif type(obj) == type(dict()):
                        for k, item in obj.iteritems():
                            encryptItem = pc.decrypt(self.cipher_key, item)
                            out.append(encryptItem)
                        callback(out)
                else:
                    callback(obj)
            else:
                callback(obj)
Пример #14
0
    def publish(self, args):
        """
        #**
        #* Publish
        #*
        #* Send a message to a channel.
        #*
        #* @param array args with channel and message.
        #* @return array success information.
        #**

        ## Publish Example
        def publish_complete(info):
            print(info)

        pubnub.publish({
            'channel' : 'hello_world',
            'message' : {
                'some_text' : 'Hello my World'
            },
            'callback' : publish_complete
        })

        """
        ## Fail if bad input.
        if not (args['channel'] and args['message']):
            print('Missing Channel or Message')
            return False

        ## Capture User Input
        channel = str(args['channel'])
        message = args['message']

        if self.cipher_key:
            pc = PubnubCrypto()
            out = []
            if type(message) == type(list()):
                for item in message:
                    encryptItem = pc.encrypt(self.cipher_key, item).rstrip()
                    out.append(encryptItem)
                message = json.dumps(out)
            elif type(message) == type(dict()):
                outdict = {}
                for k, item in message.iteritems():
                    encryptItem = pc.encrypt(self.cipher_key, item).rstrip()
                    outdict[k] = encryptItem
                    out.append(outdict)
                message = json.dumps(out[0])
            else:
                message = json.dumps(
                    pc.encrypt(self.cipher_key, message).replace('\n', ''))
        else:
            message = json.dumps(args['message'])

        ## Capture Callback
        if args.has_key('callback'):
            callback = args['callback']
        else:
            callback = lambda x: x

        ## Sign Message
        if self.secret_key:
            hashObject = sha256()
            hashObject.update(self.secret_key)
            hashedSecret = hashObject.hexdigest()
            hash = hmac.HMAC(hashedSecret,
                             '/'.join([
                                 self.publish_key, self.subscribe_key,
                                 self.secret_key, channel, message
                             ]),
                             digestmod=digestmod)
            signature = hash.hexdigest()
        else:
            signature = '0'

        ## Send Message
        return self._request([
            'publish', self.publish_key, self.subscribe_key, signature,
            channel, '0', message
        ], callback)
Пример #15
0
    def publish( self, args ) :
        """
        #**
        #* Publish
        #*
        #* Send a message to a channel.
        #*
        #* @param array args with channel and message.
        #* @return array success information.
        #**

        ## Publish Example
        def publish_complete(info):
            print(info)

        pubnub.publish({
            'channel' : 'hello_world',
            'message' : {
                'some_text' : 'Hello my World'
            },
            'callback' : publish_complete
        })

        """
        ## Fail if bad input.
        if not (args['channel'] and args['message']) :
            print('Missing Channel or Message')
            return False

        ## Capture User Input
        channel = str(args['channel'])
        message = args['message']

        if self.cipher_key :
            pc = PubnubCrypto()
            out = []
            if type( message ) == type(list()):
                for item in message:
                    encryptItem = pc.encrypt(self.cipher_key, item ).rstrip()
                    out.append(encryptItem)
                message = json.dumps(out)
            elif type( message ) == type(dict()):
                outdict = {}
                for k, item in message.iteritems():
                    encryptItem = pc.encrypt(self.cipher_key, item ).rstrip()
                    outdict[k] = encryptItem
                    out.append(outdict)
                message = json.dumps(out[0])
            else:
                message = json.dumps(pc.encrypt(self.cipher_key, message).replace('\n',''))
        else :
            message = json.dumps(args['message'])

        ## Capture Callback
        if args.has_key('callback') :
            callback = args['callback']
        else :
            callback = lambda x : x

        ## Sign Message
        if self.secret_key :
            hashObject = sha256()
            hashObject.update(self.secret_key)
            hashedSecret = hashObject.hexdigest()
            hash = hmac.HMAC(hashedSecret, '/'.join([
                    self.publish_key,
                    self.subscribe_key,
                    self.secret_key,
                    channel,
                    message
                ]), digestmod=digestmod)
            signature = hash.hexdigest()        
        else :
            signature = '0'
        
        ## Send Message
        return self._request([
            'publish',
            self.publish_key,
            self.subscribe_key,
            signature,
            channel,
            '0',
            message
        ], callback );