Esempio n. 1
0
def is_target_chatroom(cur_chatroom_id):
    target_chatrooms = itchat.search_chatrooms(TARGET_CHATROOM)
    logger.debug(dump_json('target_chatrooms', target_chatrooms))
    if target_chatrooms and target_chatrooms[0].get(
            'UserName') == cur_chatroom_id:
        return True
    return False
Esempio n. 2
0
    def friend_reply(msg):
        #itchat control
        #global ITCHAT_MODE
        itchat_mode = get_itchat_mode()
        logger.info('ITCHAT_MODE is %s' % itchat_mode)
        logger.debug(dump_json(u'friend reply dict', msg))
        #获取当前用户nickname
        friends = itchat.search_friends()
        master_name = friends.get('NickName')
        logger.debug(dump_json(u'search friend dict', friends))
        from_user = itchat.search_friends(userName=msg.get('FromUserName'))
        to_user = itchat.search_friends(userName=msg.get('ToUserName'))
        from_user_name = from_user.get('NickName', 'Somebody')
        content = msg.get('Text')

        msg_log_info = u'Receive message [%s] from [%s]' % (
            content, msg.get('FromUserName'))
        msg_send_info = u'Receive message [%s] from [%s]' % (content,
                                                             from_user_name)
        logger.info(msg_log_info)
        itchat.send_msg(msg_send_info)
        #发送者为master自己,且发送给本人时,进入控制逻辑
        if (from_user_name == master_name) and (from_user == to_user):
            if content in [u'人工', u'人工模式', u'H', u'h']:
                logger.info(u'Change mode to [%s]' % content)
                set_itchat_mode('HUMAN')
                logger.info(u'Current mode is [%s]' % get_itchat_mode())
                itchat.send_msg(u'Current mode is [%s]' % get_itchat_mode())
            elif content in [u'机器', u'机器模式', u'A', u'a']:
                logger.info(u'Change mode to [%s]' % content)
                set_itchat_mode('AI')
                logger.info(u'Current mode is [%s]' % get_itchat_mode())
                itchat.send_msg(u'Current mode is [%s]' % get_itchat_mode())
            elif content in [u'群机器', u'群机器模式', u'GA', u'ga']:
                logger.info(u'Change mode to [%s]' % content)
                set_itchat_mode('GAI')
                logger.info(u'Current mode is [%s]' % get_itchat_mode())
                itchat.send_msg(u'Current mode is [%s]' % get_itchat_mode())
            elif content in [u'?', u'stat', u'状态']:
                status_msg = 'Mode: %s\nMEM: %s' % (get_itchat_mode(), 'TODO')
                itchat.send_msg(status_msg)
        else:
            logger.info('ITCHAT_MODE here is %s' % get_itchat_mode())
            if get_itchat_mode() == 'AI':
                robot_send_info = TURING_BOT.get_turing_result(content)
                robot_send_info = json.loads(robot_send_info).get('text')
                default_send_info = u'主人不在服务区,现在是托管模式'
                msg_send_info = robot_send_info if robot_send_info else default_send_info
                logger.info('AI response is %s' % msg_send_info)
                itchat.send_msg(msg=msg_send_info + TURING_BOT.suffix,
                                toUserName=msg.get('FromUserName'))
            elif get_itchat_mode() == 'GAI':
                if content == u'任务':
                    logger.info(u'Receive regisiter [%s/%s]' %
                                (from_user_name, from_user))
                    REDIS_OBJ.client.sadd('REGISITER', from_user_name)
                    itchat.send_msg(u'Receive regisiter [%s/%s]' %
                                    (from_user_name, from_user))
Esempio n. 3
0
def text_reply(self, msg):
    logger.debug(u'msg dict : ' + json.dumps(msg, indent=True, ensure_ascii=False))
    #from_user = itchat.search_friends(userName=msg['FromUserName'])['NickName'] if \
    #    itchat.search_friends(userName=msg['FromUserName']) else 'Somebody'
    from_user = msg.get('ActualNickName', 'Somebody')
    logger.info(u'Receive message [%s] from [%s]' % (msg['Text'], from_user))
    #itchat.send_msg(u'自动回复收到-来自Brishen的机器人', toUserName=msg['FromUserName'])
    if msg.get('isAt'):
        logger.info(u'收到At你的消息,内容是[%s]' % msg.get('Text'))
        itchat.send_msg(u'自动回复收到-来自Brishen的机器人', toUserName=msg.get('FromUserName'))
Esempio n. 4
0
 def prepare(self):
     """
     overwrite setup
     """
     logger.info('===== ITCHAT MESSAGE START =====')
     itchat.auto_login(hotReload=True, enableCmdQR=True)
     itchat.run()
     #获取friends和chatroom信息
     logger.debug('Friend list :' + json.dumps(
         itchat.get_friends(update=True), indent=True, ensure_ascii=False))
     logger.debug('Chatroom list :' + json.dumps(
         itchat.get_chatrooms(
             update=True), indent=True, ensure_ascii=False))
Esempio n. 5
0
 def group_reply(msg):
     #logger.info('msg : [%s] [%s] [%s]', type(msg), dump_json('debug', dir(msg)), dump_json('raw', msg.raw))
     #logger.info('Receive group msg [%s] from [%s][%s] to [%s][%s] in group[%s][%s]' % (msg.text, msg.member, msg.member.puid, msg.receiver,
     #    msg.receiver.puid, msg.chat.nick_name, msg.chat.puid))
     if get_wxpy_mode() == 'GAI' and is_target_group(
             msg.chat.nick_name) and is_target_keyword(msg.text):
         logger.info(
             'Receive target group msg [%s] from [%s][%s] to [%s][%s] in group[%s][%s]'
             % (msg.text, msg.member, msg.member.puid, msg.receiver,
                msg.receiver.puid, msg.chat.nick_name, msg.chat.puid))
         #check @TARGET_FRIENDS or not
         target_group = bot.groups().search(
             msg.chat.nick_name)[0] if bot.groups().search(
                 msg.chat.nick_name) else None
         if not target_group:
             logger.error('Target group not found : [%s]' %
                          msg.chat.nick_name)
             return
         #更新目标群member信息
         #target_group.update_group(members_details=True)
         for friend_name in REDIS_OBJ.client.smembers('TARGET_FRIENDS'):
             friends = bot.friends().search(friend_name)
             if friend_name not in msg.text or len(friends) != 1:
                 logger.debug(
                     'Found zero or more than one friend [%s] [%s]' %
                     (friend_name, ','.join([f.name for f in friends])))
                 continue
             logger.info('Display name [%s] -> [%s]' %
                         (friend_name,
                          msg.chat.search(friend_name)[0].display_name))
             #if ('@' + friend[0].display_name) in msg.text:
             if friend_name in msg.text:
                 log_info = 'Send notice [%s] to register [%s]' % (
                     msg.text, friends[0].name)
                 logger.info(log_info)
                 msg.forward(friends[0], suffix=TARGET_SUFFIX)
                 msg.forward(bot.self,
                             prefix='Send notice [',
                             suffix='] to register %s' % friends[0].name)
         if msg.is_at:
             time.sleep(10)
             logger.info('Send auto reply [%s] in group [%s]' %
                         (TARGET_REPLY, msg.chat.nick_name))
             msg.reply_msg(TARGET_REPLY)
             msg.forward(bot.self,
                         prefix='Send auto reply [',
                         suffix='in group [%s]' % msg.chat.nick_name)
             msg.forward(bot.friends().search(u'李春春')[0],
                         suffix=TARGET_SUFFIX)
Esempio n. 6
0
 def group_reply(msg):
     #global ITCHAT_MODE
     logger.debug(dump_json(u'group dict', msg))
     chatroom_id = msg.get('FromUserName')
     #chatroom_name = msg.get('User').get('NickName') if msg.get('User') else 'SomeGroup'
     chatroom_name = itchat.search_chatrooms(chatroom_id).get(
         'NickName') if itchat.search_chatrooms(
             chatroom_id) else 'SomeGroup'
     from_user = msg.get('ActualNickName', 'Somebody')
     content = msg.get('Text')
     if msg.get('isAt'):
         msg_log_info = u'Receive message [%s] at you from [%s] in group [%s]' % (
             content, from_user, chatroom_id)
         msg_send_info = u'Receive message [%s] at you from [%s] in group [%s]' % (
             content, from_user, chatroom_name)
         logger.info(msg_log_info)
         itchat.send_msg(msg_send_info)
     else:
         msg_log_info = u'Receive message [%s] from [%s] in group [%s]' % (
             content, from_user, chatroom_id)
         msg_send_info = u'Receive message [%s] from [%s] in group [%s]' % (
             content, from_user, chatroom_name)
         logger.info(msg_log_info)
         itchat.send_msg(msg_send_info)
     itchat_mode = get_itchat_mode()
     if itchat_mode == 'GAI' and is_target_chatroom(
             chatroom_id) and is_target_keyword(content):
         logger.info('Get target chatroom %s' % chatroom_name)
         #for friend in REDIS_OBJ.client.smembers('TARGET_FRIENDS'):
         for friend in TARGET_FRIENDS:
             #未@到的,直接跳过 TODO 根据displayName精细判断
             if friend not in content:
                 continue
             friend_info = itchat.search_friends(name=friend)
             if friend_info:
                 itchat.send_msg(content + TURING_BOT.suffix,
                                 friend_info[0].get('UserName'))
                 itchat.send_msg('Send notice [%s] to [%s] in group [%s]' %
                                 (content, friend, chatroom_name))
         #有@自己的消息,sleep后回复
         if msg.get('isAt'):
             time.sleep(10)
             itchat.send_msg(TARGET_REPLY,
                             toUserName=msg.get('FromUserName'))
             itchat.send_msg('Send auto reply [%s] in group [%s]' %
                             (TARGET_REPLY, chatroom_name))
Esempio n. 7
0
def send_request(data, tp20_bid_shpr, folder_path, logger):
    """
    Write requests to unique file in folder_path
    """
    file_name = str(uuid.uuid4())

    # print "sending request: " + folder_path + file_name

    try:
        with open(folder_path + file_name, 'wb') as pickle_file:
            pickle.dump((data, tp20_bid_shpr, file_name), pickle_file,
                        pickle.HIGHEST_PROTOCOL)
    except Exception as e:
        print_error_message(
            e, "Error 2.2b: Data exchange issues writing " + folder_path +
            file_name, logger)
        raise
    logger.debug('******************Exiting function send_request')
    return file_name
Esempio n. 8
0
def send_results(output_data, tncvcel, bidNumber, log_path, result_file,
                 p2c_file):
    """
    Combine the output and input and
    write to a pickle file
    """
    file_name = bidNumber + '-' + p2c_file

    acy = output_data[output_data['Product_Mode'] == 'ACY']
    reg = output_data[output_data['Product_Mode'] != 'ACY']
    #reg = reg.drop('SVC_GRP_NR', axis=1)

    # merge tncvcel to capture TP 1.0 values
    tncvcel = tncvcel.rename(
        columns={
            'NVP_BID_NR': 'BidNumber',
            'SVC_TYP_CD': 'SVM_TYP_CD',
            'RCM_NCV_QY': 'TP1_Target',
            'NCV_MIN_QY': 'TP1_Low',
            'NCV_MAX_QY': 'TP1_High'
        })

    output_data = reg.merge(tncvcel, how="inner")

    # re-add accessorials
    output_data = output_data.append(acy)

    # check is RESI and DAS are columns
    if 'DAS' not in output_data.columns:
        output_data['DAS'] = ''

    if 'RESI' not in output_data.columns:
        output_data['RESI'] = ''

    # add timestamp
    output_data['Timestamp'] = datetime.fromtimestamp(
        time.time()).strftime('%Y-%m-%d %H:%M')
    output_data.to_csv(log_path + file_name + '.csv')
    logger.debug('******************Exiting function send_results')
    return output_data, result_file, p2c_file
                model_objs[modelName] = pickle.load(model_pickle)
        except Exception, e:
            print_error_message(e,
                                "Error 3.2a: Model cannot be loaded: " + model,
                                logger)

    logger.info("All models loaded")

    # Load config variables
    settings_dict = {}
    for key, value in config.iteritems():
        settings_dict.update(value)
    settings = settings_from_init_file(settings_dict)

    # Start run() which will check for requests
    logger.debug('******************Exiting function main_Consumer')
    run(home, paths_dict['c2p_path'], paths_dict['p2c_path'], model_objs,
        settings, paths_dict['init_path'], ceilinglookup_filename,
        svc_to_prod_filename, strategic_overlay_filename,
        sic_to_industry_filename, datatypes_filename, cwt_filename,
        accessorial_ceiling_filename, eligible_accessorials_filename, test,
        logger)


@log_function
def run(home, c2p_path, p2c_path, model_objs, settings, init_path,
        ceilinglookup_filename, svc_to_prod_filename,
        strategic_overlay_filename, sic_to_industry_filename,
        datatypes_filename, cwt_filename, accessorial_ceiling_filename,
        eligible_accessorials_filename, test, logger):
    """
Esempio n. 10
0
def put_data(home, bid_number, config, test, response, tp20_ceiling_svc,
             tp_accessorial, logger):
    """
    Places data into Oracle DB
    """

    try:
        acy_table = response[response['Product_Mode'] == 'ACY']
        if not acy_table.empty:
            acy_table = acy_table.drop(
                'SVC_GRP_NR', axis=1)  # remove due to blank causing issues
            acy_table = acy_table.merge(
                tp_accessorial,
                how='inner',
                on=['MVM_DRC_CD', 'SVM_TYP_CD', 'ASY_SVC_TYP_CD'])
            acy_table = acy_table.merge(tp20_ceiling_svc,
                                        how='inner',
                                        left_on=['BidNumber', 'SVC_GRP_NR'],
                                        right_on=['NVP_BID_NR', 'SVC_GRP_NR'])

        prod_table = response[response['Product_Mode'] != 'ACY']
        prod_table = prod_table.merge(tp20_ceiling_svc,
                                      how='inner',
                                      left_on=['BidNumber', 'SVC_GRP_NR'],
                                      right_on=['NVP_BID_NR', 'SVC_GRP_NR'])

        response = prod_table.append(acy_table)

        if test:
            acy_table = response[response['Product_Mode'] == 'ACY']
            prod_table = response[response['Product_Mode'] != 'ACY']

            prod_table = prod_table[[
                "BidNumber", "Product", "Incentive_Freight", "Target_Low",
                "Target_High"
            ]].drop_duplicates()

            for index, row in prod_table.iterrows():
                logger.info("{0}: Inc - {1}, Low - {2}, High - {3}".format(
                    row["Product"], row["Incentive_Freight"],
                    row["Target_Low"], row["Target_High"]))

                # try:
                #     acy_table = acy_table[["BidNumber", "MKG_SVP_DSC_TE", "ASY_SVC_TYP_CD",
                #                                           "RESI", "DAS"]].drop_duplicates()
                #
                #     for index, row in acy_table.iterrows():
                #         if row["ASY_SVC_TYP_CD"] == 'RES':
                #             logger.info("{0}: Inc - {1}".format(row["MKG_SVP_DSC_TE"],
                #  row["RESI"]))
                #         elif row["ASY_SVC_TYP_CD"] == 'GDL':
                #             logger.info("{0}: Inc - {1}".format(row["MKG_SVP_DSC_TE"],
                # row["DAS"]))
                # except Exception, e:
                #     pass
        logger.debug('******************Exiting function put_data')
        return pushData(home, bid_number, config["DB"]["db_host"], config,
                        response, tp_accessorial, logger, test)
    except (IOError, OSError) as e:
        print_error_message(e, home + "/" + bid_number + "_results.csv",
                            logger)
        raise
    except RuntimeError, e:
        print_error_message(e, "", logger)
        raise
Esempio n. 11
0
                sql = sql + sql_result
                # True
        except Exception, e:
            print_error_message(e, "", logger, False)
            logger.warning("Bid " + bid_number + " scoring failed.")
        else:
            cleanup(paths_dict['c2p_path'] + result_file)
            logger.info("Bid " + bid_number + " successfully scored.")
            # Once done with everything write success flag
            set_error_flag(bid_number, paths_dict['error_log_path'], str(0),
                           logger)

    # Output master dataset immediately
    #file_name = paths_dict['log_path'] + bid_number + '-' + p2c_file + '-master.csv'
    #master.to_csv(file_name)
    logger.debug('******************Exiting function main_Producer')
    if test:
        master_result.to_csv("results.csv")
        sql_file = open("sql_results.txt", "w")
        sql_file.write(sql)
        sql_file.close()


def cleanup(file):
    """Performs cleanup of working files"""
    # Deletes IPC files, ignore if can't be deleted
    try:
        os.remove(file)
    except:
        pass