Ejemplo n.º 1
0
    def parse_data(self, data):
        """
        解析微信服务器发送过来的数据并保存类中
        :param data: HTTP Request 的 Body 数据
        :raises ParseError: 解析微信服务器数据错误, 数据不合法
        """
        #        result = {}
        if isinstance(data, str):
            data = data.encode('utf-8')
        elif isinstance(data, bytes):
            pass
        else:
            raise ParseError()

        try:
            xml = XMLStore(xmlstring=data)
        except Exception:
            raise ParseError()

        result = xml.xml2dict
        result['raw'] = data
        result['type'] = result.pop('MsgType').lower()

        message_type = MESSAGE_TYPES.get(result['type'], UnknownMessage)
        self.__message = message_type(result)
        self.__is_parse = True
Ejemplo n.º 2
0
def xml_to_dict(data):
    result = {}

    if type(data) == unicode:
        data = data.encode('utf-8')
    elif type(data) == str:
        pass
    else:
        raise ParseError

    try:
        doc = minidom.parseString(data)
    except Exception:
        raise ParseError()

    params = [
        ele for ele in doc.childNodes[0].childNodes
        if isinstance(ele, minidom.Element)
    ]
    for param in params:
        if param.childNodes:
            text = param.childNodes[0]
            result[param.tagName] = text.data

    return result
Ejemplo n.º 3
0
 def __init__(self, message):
     try:
         self.media_id = message.pop('MediaId')
         self.thumb_media_id = message.pop('ThumbMediaId')
     except KeyError:
         raise ParseError()
     super(ShortVideoMessage, self).__init__(message)
Ejemplo n.º 4
0
 def __init__(self, message):
     try:
         self.picurl = message.pop('PicUrl')
         self.media_id = message.pop('MediaId')
     except KeyError:
         raise ParseError()
     super(ImageMessage, self).__init__(message)
Ejemplo n.º 5
0
 def __init__(self, message):
     try:
         self.title = message.pop('Title')
         self.description = message.pop('Description')
         self.url = message.pop('Url')
     except KeyError:
         raise ParseError()
     super(LinkMessage, self).__init__(message)
Ejemplo n.º 6
0
 def __init__(self, message):
     try:
         self.media_id = message.pop('MediaId')
         self.format = message.pop('Format')
         self.recognition = message.pop('Recognition', None)
     except KeyError:
         raise ParseError()
     super(VoiceMessage, self).__init__(message)
Ejemplo n.º 7
0
 def __init__(self, message):
     try:
         location_x = message.pop('Location_X')
         location_y = message.pop('Location_Y')
         self.location = (float(location_x), float(location_y))
         self.scale = int(message.pop('Scale'))
         self.label = message.pop('Label')
     except KeyError:
         raise ParseError()
     super(LocationMessage, self).__init__(message)
Ejemplo n.º 8
0
    def _decrypt_message(self, msg, msg_signature, timestamp, nonce):
        """检验消息的真实性,并且获取解密后的明文

        :param msg: 密文,对应POST请求的数据
        :param msg_signature: 签名串,对应URL参数的msg_signature
        :param timestamp: 时间戳,对应URL参数的timestamp
        :param nonce: 随机串,对应URL参数的nonce
        :return: 解密后的原文
        """
        timestamp = to_binary(timestamp)
        nonce = to_binary(nonce)
        try:
            msg = xmltodict.parse(to_text(msg))['xml']
        except Exception as e:
            raise ParseError(e)

        encrypt = msg['Encrypt']
        signature = get_sha1_signature(self.__token, timestamp, nonce, encrypt)
        if signature != msg_signature:
            raise ValidateSignatureError()
        return self.__pc.decrypt(encrypt, self.__id)
Ejemplo n.º 9
0
 def __init__(self, message):
     message.pop('type')
     try:
         self.type = message.pop('Event').lower()
         if self.type == 'subscribe' or self.type == 'scan':
             self.key = message.pop('EventKey', None)
             self.ticket = message.pop('Ticket', None)
         elif self.type in [
                 'click', 'view', 'scancode_push', 'scancode_waitmsg',
                 'pic_sysphoto', 'pic_photo_or_album', 'pic_weixin',
                 'location_select'
         ]:
             self.key = message.pop('EventKey')
         elif self.type == 'location':
             self.latitude = float(message.pop('Latitude'))
             self.longitude = float(message.pop('Longitude'))
             self.precision = float(message.pop('Precision'))
         elif self.type == 'templatesendjobfinish':
             self.status = message.pop('Status')
     except KeyError:
         raise ParseError()
     super(EventMessage, self).__init__(message)