Esempio n. 1
0
def change_qq_xnr(xnr_user_no, group_names_string, mark_names_string,
                  group_numbers_string):

    get_result = es_xnr.get(index=qq_xnr_index_name,
                            doc_type=qq_xnr_index_type,
                            id=xnr_user_no)['_source']

    group_info = json.loads(get_result['group_info'])

    group_names = group_names_string.encode('utf-8').split(',')
    mark_names = mark_names_string.encode('utf-8').split(',')
    group_numbers = group_numbers_string.encode('utf-8').split(',')

    if len(group_numbers) != len(group_names) and len(group_numbers) != 0:
        return 'not_equal'

    group_numbers_origin = group_info.keys()

    delete_list = list(
        set(group_numbers_origin).difference(set(group_numbers)))
    #add_list = list(set(group_numbers).difference(set(group_numbers_origin)))

    if delete_list:
        for item in delete_list:
            group_info.pop(item)

    for i in range(len(group_numbers)):
        group_qq_number = group_numbers[i]
        group_qq_name = group_names[i]

        if group_qq_number not in group_numbers_origin:  # 新添加的
            group_info[group_qq_number] = [group_qq_name, '']
            r.sadd(r_qq_group_set,
                   group_qq_number)  ## 存入redis,后面接收群消息时,用于过滤消息。

    qq_group_num = len(group_info)
    group_info = json.dumps(group_info)

    try:
        es_xnr.update(index=qq_xnr_index_name, doc_type=qq_xnr_index_type, id=xnr_user_no,  \
            body={"doc":{'group_info':group_info,'qq_group_num':qq_group_num}})
        result = True  #'Successfully changed'
    except:
        result = False  #'Changing Failed'

    return result
Esempio n. 2
0
def create_qq_xnr(xnr_info):
    # xnr_info = [qq_number,qq_groups,nickname,active_time,create_time]
    print '============1'
    qq_group_exist_list = ''  #[]
    qq_group_new_list = []
    qq_number = xnr_info['qq_number']
    #qq_groups = xnr_info['qq_groups'].encode('utf-8').split(',')
    group_names = xnr_info['group_names'].encode('utf-8').split(',')
    mark_names = xnr_info['mark_names'].encode('utf-8').split(',')
    group_numbers = xnr_info['group_numbers'].encode('utf-8').split(',')
    print 'group_numbers...', group_numbers
    print 'mark_names......', mark_names
    print 'group_names.....', group_names
    if not len(group_names) == len(mark_names) == len(group_numbers):
        #return [False,'群名称数量和群号码数量不一致']
        return [False, '群名称数量、群备注数量和群号码数量不一致']

    if len(group_numbers) == 0:
        return [False, '输入不能为空']

    # redis 群名
    r_qq_group_set = r_qq_group_set_pre + qq_number

    mark_name_exist_list = []

    nickname = xnr_info['nickname']
    access_id = xnr_info['access_id']
    remark = xnr_info['remark']
    submitter = xnr_info['submitter']

    #r_qq_group_mark_set = r_qq_group_mark_set_pre + qq_number

    query_body_qq_exist = {'query': {'term': {'qq_number': qq_number}}}

    search_result = es_xnr.search(index=qq_xnr_index_name,doc_type=qq_xnr_index_type,\
        body=query_body_qq_exist)['hits']['hits']
    print '456'
    if search_result:
        #return ['当前qq已经被添加!',qq_group_exist_list]
        group_info = json.loads(search_result[0]['_source']['group_info'])
        group_info_keys = group_info.keys()  # 群号

        for i in range(len(group_numbers)):

            group_qq_number = group_numbers[i]
            group_qq_name = group_names[i]
            group_qq_mark = mark_names[i]

            if group_qq_number in group_info_keys:  # 若群号已添加,则可修改群名
                #qq_group_exist_list.append(group_qq_number)
                #mark_name_list = group_info[group_qq_number]['mark_name']
                group_name_list = group_info[group_qq_number]['group_name']
                r.sadd(r_qq_group_set, group_qq_mark)

                if not group_qq_name in group_name_list:
                    group_info[group_qq_number]['group_name'].append(
                        group_qq_name)

            else:  # 若群号未添加,首先检查备注名是否重复,若重复,则返回,否则,正常流程。
                if not r.sadd(r_qq_group_set, group_qq_mark):  # 群号唯一 改为 备注唯一
                    mark_name_exist_list.append(group_qq_mark)
                else:
                    group_info[group_qq_number] = {
                        'mark_name': group_qq_mark,
                        'group_name': [group_qq_name]
                    }

        if mark_name_exist_list:
            return [False, '失败!以下备注名重复:' + ','.join(mark_name_exist_list)]

        qqbot_port = search_result[0]['_source']['qqbot_port']

        # 把不在的群添加进去
        qq_exist_result = search_result[0]['_source']
        xnr_user_no = qq_exist_result['xnr_user_no']
        qq_exist_result['group_info'] = json.dumps(group_info)

        qq_exist_result['qq_group_num'] = len(group_info)

        es_xnr.update(index=qq_xnr_index_name,doc_type=qq_xnr_index_type,id=xnr_user_no,\
            body={'doc':qq_exist_result})

        result = True

    else:

        # active_time = xnr_info[3]
        create_ts = xnr_info['create_ts']
        exist_port_list = get_all_ports()  #返回list形式int型端口号
        qqbot_port = find_port(exist_port_list)
        #qq_groups_num = len(qq_groups)
        # qq_groups = getgroup_v2(qq_number)
        user_no_current = get_qq_xnr_no()
        xnr_user_no = user_no2qq_id(user_no_current)  #五位数 QXNR0001

        # 群信息
        group_info = {}

        for i in range(len(group_numbers)):
            group_qq_number = group_numbers[i]
            group_qq_name = group_names[i]
            group_qq_mark = mark_names[i]
            print '1..', group_qq_number
            print '2..', group_qq_name
            print '3..', group_qq_mark

            if not r.sadd(
                    r_qq_group_set,
                    group_qq_mark):  # 群号唯一 改为 备注唯一. 存入redis,后面接收群消息时,用于过滤消息。
                mark_name_exist_list.append(group_qq_mark)
            else:
                group_info[group_qq_number] = {
                    'mark_name': group_qq_mark,
                    'group_name': [group_qq_name]
                }

        if mark_name_exist_list:
            return [False, '失败!以下备注名重复:' + ','.join(mark_name_exist_list)]

        qq_group_num = len(group_info)
        group_info = json.dumps(group_info)

        #try:
        ## 存入es
        print es_xnr.index(index=qq_xnr_index_name, doc_type=qq_xnr_index_type, id=xnr_user_no, \
        body={'qq_number':qq_number,'nickname':nickname,'group_info':group_info,'qq_group_num':qq_group_num,'create_ts':create_ts,\
                'qqbot_port':qqbot_port,'user_no':user_no_current,'xnr_user_no':xnr_user_no,\
                'access_id':access_id,'remark':remark,'submitter':submitter})

        result = True
        # except:
        #     result = False

    print 'before python recieveQQGroupMessage:', result

    #print 'output:', p2.stdout.readlines()

    return [result, qq_group_exist_list]