Exemplo n.º 1
0
def new_init():
    #protobuf组包
    new_init_request = mm_pb2.NewInitRequest(
        login = mm_pb2.LoginInfo(
            aesKey =  Util.sessionKey,
            uin = 0,
            guid = define.__GUID__ + '\0',          #guid以\0结尾
            clientVer = define.__CLIENT_VERSION__,
            androidVer = define.__ANDROID_VER__,
            unknown = 1,
        ),
        wxid = Util.wxid,
        tag3 = mm_pb2.mmStr(),
        tag4 = mm_pb2.mmStr(),
        language = define.__LANGUAGE__,
    )
    #组包
    send_data = Util.pack(new_init_request.SerializeToString(),139)

    #发包
    ret_bytes = Util.mmPost('/cgi-bin/micromsg-bin/newinit',send_data)
    logger.debug('返回数据:' + str(ret_bytes))

    #解包
    res = mm_pb2.NewInitResponse()
    res.ParseFromString(Util.UnPack(ret_bytes))

    #newinit后保存sync key
    Util.sync_key = res.synckeybytes.key
    logger.debug('sync key len:{}\ndata:{}'.format(res.synckeybytes.len, Util.b2hex(res.synckeybytes.key)))

    #初始化数据
    logger.debug('tag7数量:{}'.format(res.cntList))

    #未读消息
    for i in range(res.cntList):
        if 5 == res.tag7[i].type:                           #未读消息
            msg = mm_pb2.Msg()
            msg.ParseFromString(res.tag7[i].data.data)
            if 10002 == msg.type or 9999 == msg.type:       #过滤系统垃圾消息
                continue
            else:
                logger.info('收到新消息:\ncreate utc time:{}\ntype:{}\nfrom:{}\nto:{}\nraw data:{}\nxml data:{}'.format(msg.createTime, msg.type, msg.from_id.id, msg.to_id.id, msg.raw.content, msg.xmlContent))
    
    return
Exemplo n.º 2
0
def new_init_buf2resp(buf):
    #解包
    res = mm_pb2.NewInitResponse()
    res.ParseFromString(UnPack(buf))

    #newinit后保存sync key
    Util.set_sync_key(res.sync_key_cur)                    #newinit结束前不要异步调用newsync
    logger.debug('newinit sync_key_cur len:{}\ndata:{}'.format(len(res.sync_key_cur), Util.b2hex(res.sync_key_cur)))
    logger.debug('newinit sync_key_max len:{}\ndata:{}'.format(len(res.sync_key_max), Util.b2hex(res.sync_key_max)))

    #初始化数据
    logger.info('newinit cmd数量:{},是否需要继续初始化:{}'.format(res.cntList,res.continue_flag))

    #初始化
    for i in range(res.cntList):
        if 5 == res.tag7[i].type:                           #未读消息
            msg = mm_pb2.Msg()
            msg.ParseFromString(res.tag7[i].data.data)
            if 10002 == msg.type or 9999 == msg.type:       #过滤系统垃圾消息
                continue
            else:
                #将消息存入数据库
                Util.insert_msg_to_db(msg.serverid,msg.createTime,msg.from_id.id,msg.to_id.id,msg.type,msg.raw.content)
                logger.info('收到新消息:\ncreate utc time:{}\ntype:{}\nfrom:{}\nto:{}\nraw data:{}\nxml data:{}'.format(Util.utc_to_local_time(msg.createTime), msg.type, msg.from_id.id, msg.to_id.id, msg.raw.content, msg.xmlContent))
        elif 2 == res.tag7[i].type:                         #好友列表
            friend = mm_pb2.contact_info()
            friend.ParseFromString(res.tag7[i].data.data)
            #过滤系统wxid
            if friend.wxid.id in define.MM_DEFAULT_WXID:
                logger.info('更新好友信息:跳过默认wxid[{}]'.format(friend.wxid.id))
                continue
            #好友分类
            if friend.wxid.id.endswith('@chatroom'):        #群聊
                logger.info('更新好友信息:群聊名:{} 群聊wxid:{} chatroom_serverVer:{} chatroom_max_member:{} 群主:{} 群成员数量:{}'.format(friend.nickname.name,friend.wxid.id,friend.chatroom_serverVer,friend.chatroom_max_member,friend.chatroomOwnerWxid,friend.group_member_list.cnt))
            elif friend.wxid.id.startswith('gh_'):          #公众号
                logger.info('更新好友信息:公众号:{} 公众号wxid:{} alias:{} 注册主体:{}'.format(friend.nickname.name,friend.wxid.id,friend.alias,friend.register_body if friend.register_body_type == 24 else '个人'))
            else:                                           #好友
                logger.info('更新好友信息:昵称:{} 备注名:{} wxid:{} alias:{} 性别:{} 好友来源:{} 个性签名:{}'.format(friend.nickname.name,friend.remark_name.name,friend.wxid.id,friend.alias,friend.sex,Util.get_way(friend.src),friend.qianming))
            #将好友信息存入数据库
            Util.insert_contact_info_to_db(friend.wxid.id,friend.nickname.name,friend.remark_name.name,friend.alias,friend.avatar_big,friend.v1_name,friend.type,friend.sex,friend.country,friend.sheng,friend.shi,friend.qianming,friend.register_body,friend.src,friend.chatroomOwnerWxid,friend.chatroom_serverVer,friend.chatroom_max_member,friend.group_member_list.cnt)
    return (res.continue_flag,res.sync_key_cur,res.sync_key_max)
Exemplo n.º 3
0
def new_sync_buf2resp(buf):
    #解包
    res = mm_pb2.new_sync_resp()
    res.ParseFromString(Util.UnPack(buf))

    #刷新sync key
    Util.set_sync_key(res.sync_key)
    logger.debug('sync key len:{}\ndata:{}'.format(len(res.sync_key), Util.b2hex(res.sync_key)))

    #未读消息
    for i in range(res.msg.cntList):
        if 5 == res.msg.tag2[i].type:                           #未读消息
            msg = mm_pb2.Msg()
            msg.ParseFromString(res.msg.tag2[i].data.data)
            if 10002 == msg.type or 9999 == msg.type:           #过滤系统垃圾消息
                continue
            else:
                #将消息存入数据库
                Util.insert_msg_to_db(msg.serverid,msg.createTime,msg.from_id.id,msg.to_id.id,msg.type,msg.raw.content)
                logger.info('收到新消息:\ncreate utc time:{}\ntype:{}\nfrom:{}\nto:{}\nraw data:{}\nxml data:{}'.format(Util.utc_to_local_time(msg.createTime), msg.type, msg.from_id.id, msg.to_id.id, msg.raw.content, msg.xmlContent))
    return