def load_tel_book(self, tel_book_file): logger.info('Loading tel book file %s' % tel_book_file) cnt = 0 for line in open(tel_book_file): try: tel_item = line.rstrip('\n').split('\t') tel_name, tel_no, dept = tel_item[:3] tel_sys = tel_item[3] if len(tel_item) == 4 else '' tel_name = tel_name.strip() tel_dic = { "tel_name": tel_name, "tel_no": tel_no, "dept": dept, "tel_sys": tel_sys } cnt += 1 if cnt % 100 == 0: logger.info('%s tel loaded' % cnt) #如已存在,则跳过 #if self.tel_book.find({"$and":[{"tel_name":tel_name}, {"tel_no":tel_no}]}): # logger.info('tel info already loaded [%s-%s]' % (tel_name, tel_no)) # continue self.tel_book.insert_one(tel_dic) except: logger.error('Error tel format in line %s %s' % (line, traceback.format_exc())) logger.info('All [%s] tel loaded' % cnt) for t in self.tel_book.find({'tel_name': u"刘兵"}): logger.info(t) return cnt
def txt_process(self): txt_rst_str = get_random_err_msg() try: logger.info('txt_process = %s', self.text) if self.text in [u'刘雨墨', u'刘雨墨墨娃']: logger.info('Call Menu %s', SITE_IMG_URL) txt_rst_str = u'<a href="%s">干嘛</a>' % SITE_IMG_URL else: turing_result = self.turing_bot.get_turing_result(self.text) logger.info('turing_result = %s', turing_result) turing_json = json.loads(turing_result) text_strs = [] text_strs.append(turing_json.get('text')) for each in turing_json.get('list', []): text_strs.append(u'<a href="%s">%s</a>' % \ ((each.get('detailurl', '')), each.get('article', ''))) txt_rst_str = '\n'.join(text_strs) except: logger.error('txt_process exception = %s', traceback.format_exc()) txt_rst_str = get_random_err_msg() finally: self.msg_dic.update({'ResponseMsg': txt_rst_str}) logger.info('Write txt message to DB %s', \ json.dumps(self.msg_dic, ensure_ascii=False)) self.insert_msg(self.msg_dic) return txt_rst_str
async def start(self): tasks = [ self.crawl(report[0], report[1]) for report in self.report_list ] try: [ await f for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks)) ] except Exception as e: logger.error('A fatal error occured when crawling report! Reason: {}'.format(e)) print('A fatal error occured when crawling report! Reason: {}'.format(e)) exit()
def evt_process(self): evt_rst_str = '' try: logger.info('evt_process = %s', self.event) if self.event == 'subscribe': evt_rst_str = SUB_WEL_MSG except: logger.error('evt_process exception = %s', traceback.format_exc()) evt_rst_str = get_random_err_msg() finally: self.msg_dic.update({'ResponseMsg': evt_rst_str}) logger.info('Write evt message to DB %s', self.msg_dic) self.insert_msg(self.msg_dic) return evt_rst_str
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)
def get_objects(self): object_result = self.object_detect() try: object_result_json = json.loads(object_result) objects = [] for obj in object_result_json.get('objects'): if float(obj.get('confidence')) > self.object_threshold: obj_val = obj.get('value') if obj_val == 'Person': continue objects.append('%s(%s)' % (obj_val, \ TRANS_OBJ.get_trans(obj_val))) logger.info('face++ object result=%s', ','.join(objects)) obj_rst_str = u'我看到图片里有%s哦!' % ','.join(objects) \ if len(objects) > 0 else '' return obj_rst_str except: logger.error(traceback.format_exc()) return ''
def post(self): msg = MessageProcessor() body = self.request.body xml = etree.fromstring(body) msg.load_message(xml) to_user = msg.get_to_user() from_user = msg.get_from_user() msg_type = msg.get_msg_type() create_time = int(time.time()) out_str = '' if not to_user or not from_user or not msg_type: logger.error('to_user/from_user/msg_type lost') self.write(out_str) logger.info('%s XML = \n%s\nMSG = %s\n', msg_type.upper(), body, msg.dump_message()) if msg_type in ['text', 'voice']: from txtProcessor import TxtProcessor txt_processor = TxtProcessor(msg) rst_str = txt_processor.txt_process() out_str = self.reply_text(from_user, to_user, create_time, rst_str) elif msg_type == 'image': from imgProcessor import ImgProcessor img_processor = ImgProcessor(msg) rst_str = img_processor.img_process() out_str = self.reply_text(from_user, to_user, create_time, rst_str) elif msg_type == 'event': from evtProcessor import EvtProcessor evt_processor = EvtProcessor(msg) rst_str = evt_processor.evt_process() out_str = self.reply_text(from_user, to_user, create_time, rst_str) elif msg_type == 'location': from locProcessor import LocProcessor loc_processor = LocProcessor(msg) rst_str = loc_processor.loc_process() out_str = self.reply_text(from_user, to_user, create_time, rst_str) else: out_str = self.reply_text(from_user, to_user, create_time, \ u'这种消息我还不会处理,等我长大哦!') self.write(out_str)
def get_real_time_weather(self, longitude, latitude): """ get real time weather status based on longitude and latitude """ try: get_params = {'key': self.api_key, 'location': '%s:%s' % (longitude, latitude), 'language': 'zh-Hans', 'unit': 'c'} r = requests.get(self.weather_service_url, params=get_params) logger.warn('weather request = %s\n%s', json.dumps(get_params), r.content) weather_rst_json = json.loads(r.content) city_name = weather_rst_json.get('results')[0].get('location', {}).get('name') wea_status = weather_rst_json.get('results')[0].get('now', {}).get('text') wea_temp = weather_rst_json.get('results')[0].get('now', {}).get('temperature') return u'您所在的位置是%s,天气%s,当前温度是%s℃' % (city_name, \ wea_status, wea_temp) except: logger.error(traceback.format_exc()) return WEA_NO_REC_MSG
def img_process(self): img_rst_str = PIC_NO_REC_MSG try: local_pic_path, local_thumb_path = self.save_pic( self.picurl, self.msg_id) self.face_plus_plus.local_pic_path = local_pic_path self.face_plus_plus.local_thumb_path = local_thumb_path img_rst_str = self.face_plus_plus.parse_result() logger.info('img_process = %s', img_rst_str) img_rst_str = img_rst_str.strip() except: logger.error('img_process error %s', traceback.format_exc()) img_rst_str = PIC_NO_REC_MSG finally: self.msg_dic.update({'ResponseMsg': img_rst_str, \ 'LocalPicUrl': self.face_plus_plus.local_pic_path, \ 'LocalThumbUrl': self.face_plus_plus.local_thumb_path}) logger.info('Write img message to DB %s', self.msg_dic) self.insert_msg(self.msg_dic) return img_rst_str
def load_schedule(self, schedule_file): logger.info('Loading schedule file %s' % schedule_file) cnt = 0 for line in open(schedule_file): if line and line.strip().startswith('ZK') and len( line.strip().split('\t')) == 6: task_id, task_desc, task_owner, task_checker, task_beg_time, task_end_time = line.strip( ).split('\t')[:6] try: #task_beg_timestramp = time.mktime(time.strptime(task_beg_time,'%Y-%m-%d %H:%M:%S')) #task_end_timestramp = time.mktime(time.strptime(task_end_time,'%Y-%m-%d %H:%M:%S')) task_beg_timestramp = time.mktime( time.strptime(task_beg_time, '%Y-%m-%d %H:%M')) task_end_timestramp = time.mktime( time.strptime(task_end_time, '%Y-%m-%d %H:%M')) except: logger.error('Error time format in task %s %s' % (task_id, traceback.format_exc())) continue task_dic = { "task_id": task_id, "task_desc": task_desc, "task_owner": task_owner, "task_checker": task_checker, "task_beg_time": task_beg_time, "task_end_time": task_end_time, "task_beg_timestramp": task_beg_timestramp, "task_end_timestramp": task_end_timestramp, "task_raw_str": line.strip(), "is_noticed": 0 } cnt += 1 if cnt % 100 == 0: logger.info('%s tasks loaded' % cnt) self.xhx_task.insert_one(task_dic) logger.info('All [%s] tasks loaded' % cnt) for t in self.xhx_task.find({'task_owner': u"刘兵"}): logger.info(t) return cnt
async def start(self): print("Loading district code...") self.load_districts() print("Loading district code successfully.") tasks = list() for year in self.settings['years']: for district_code in self.districts_list: tasks.append(self.crawl(district_code, year)) try: [ await f for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks)) ] except Exception as e: logger.error( "A fatal error occured when crawling report id! Reason: {}". format(e)) print('A fatal error occured when crawling report! Reason: {}'. format(e)) exit() return self.report_list
def friend_reply(msg): logger.info('Receive friend msg [%s] from [%s][%s] to [%s][%s]' % (msg.text, msg.sender, msg.sender.puid, msg.receiver, msg.receiver.puid)) msg.forward(bot.self, prefix='Receive friend msg [', suffix='] from %s' % msg.sender.name) if msg.text.strip() in [u'?', u'?']: msg.sender.send_msg( u'【WARNING】\n本功能仅适用于826工程投产演练及上线流程,非法使用带来的生理或心理伤害,本人概不负责:)\n\n【当前功能】\n1. 回复“task 姓名”, 获取名下所有责任人和复核人任务\n2. 回复“add 姓名”,经本人注册后,可接收任务提醒(开始前10分钟,指令群@时,结束前10分钟,暂无法区分高威与单高威)\n3. 回复“tel 姓名”,查询目标姓名的联系方式\n\nPS: 自建乞丐服务器,CPU low,存储low,网络low,随时存在宕机风险,且用且珍惜!欢迎提bug,反正提了也不改\n祝各位826一切顺利!' + TARGET_SUFFIX) elif msg.text.strip().lower().startswith('task'): task_owners = msg.text.strip().split()[1:] if len(task_owners) < 1: logger.error('No task owner found') for task_owner in task_owners: msg.sender.send_msg(MONGO_OBJ.get_task_by_name(task_owner)) elif msg.text.strip().lower().startswith('tel'): tel_keys = msg.text.strip().split()[1:] if len(tel_keys) < 1: logger.error('Not found') for tel_key in tel_keys: msg.sender.send_msg(MONGO_OBJ.get_tel_info_by_key(tel_key))
def get_faces(self): try: face_result = self.face_detect() face_rst_json = json.loads(face_result) #TODO get first result by default sex = face_rst_json.get('faces')[0].get('attributes').get( 'gender').get('value') age = face_rst_json.get('faces')[0].get('attributes').get( 'age').get('value') glass = face_rst_json.get('faces')[0].get('attributes').get( 'glass').get('value') is_smile = face_rst_json.get('faces')[0].get('attributes').get('smile').get('value') \ >= face_rst_json.get('faces')[0].get('attributes').get('smile').get('threshold') reply_content = u'看起来是个%s, %s岁, %s戴眼镜, %s笑' % \ (u'男生' if sex == 'Male' else u'女生', \ age, \ u'没有' if glass == 'None' else u'', \ u'正在' if is_smile else u'没有') logger.info('face++ result = %s', reply_content) return reply_content except: logger.error(traceback.format_exc()) return ''
def self_reply(msg): #文本消息 if msg.type == 'Text': logger.info('Receive self text msg [%s][%s][%s][%s][%s]' % (msg.text, msg.type, msg.sender, msg.receiver, msg.sender.puid)) content = msg.text return_msg = '' #发送者为本人,进入控制逻辑 content = content.strip().lower() if content in [u'人工', u'人工模式', u'H', u'h']: logger.info(u'Change mode to [%s]' % content) set_wxpy_mode('HUMAN') logger.info(u'Current mode is [%s]' % get_wxpy_mode()) return_msg = u'Current mode is [%s]' % get_wxpy_mode() elif content in [u'机器', u'机器模式', u'A', u'a']: logger.info(u'Change mode to [%s]' % content) set_wxpy_mode('AI') logger.info(u'Current mode is [%s]' % get_wxpy_mode()) return_msg = u'Current mode is [%s]' % get_wxpy_mode() elif content in [u'群机器', u'群机器模式', u'GA', u'ga']: logger.info(u'Change mode to [%s]' % content) set_wxpy_mode('GAI') logger.info(u'Current mode is [%s]' % get_wxpy_mode()) return_msg = u'Current mode is [%s]' % get_wxpy_mode() elif content in [u'?', u'stat', u'状态']: return_msg = 'Mode: %s\nMEM: %s' % (get_wxpy_mode(), 'TODO') elif content.startswith('add') or content.startswith('del'): #register_name register_names = content.strip().split()[1:] if len( content.strip().split()) > 1 else [] if content.startswith('add'): for rn in register_names: logger.info('Add register [%s]' % rn) if rn == u'刘兵': REDIS_OBJ.client.sadd('TARGET_FRIENDS', rn) continue friends = bot.friends().search(rn) if len(friends) != 1: logger.info('Add register failed [%s]' % ','.join(f.name for f in friends)) msg.forward( bot.self, prefix='Add register [%s] failed' % rn, suffix='0 or more than 1 friends found') continue logger.info( 'Add register [%s][nick_name:%s, puid=%s] success' % (rn, friends[0].nick_name, friends[0].puid)) REDIS_OBJ.client.sadd('TARGET_FRIENDS', rn) bot.self.send_msg( 'Add register [%s][nick_name:%s, puid=%s] success' % (rn, friends[0].nick_name, friends[0].puid)) logger.info( dump_json( 'All registers', list(REDIS_OBJ.client.smembers('TARGET_FRIENDS')))) return_msg = 'All registers : [%s]' % ','.join( list(REDIS_OBJ.client.smembers('TARGET_FRIENDS'))) elif content == 'del': #无后续参数,删除全部register logger.info('Delete all registers') bot.self.send_msg('Delete all registers [%s]' % ','.join( list(REDIS_OBJ.client.smembers('TARGET_FRIENDS')))) REDIS_OBJ.client.delete('TARGET_FRIENDS') elif content.strip().lower().startswith('task'): task_owners = content.strip().split()[1:] if len(task_owners) < 1: logger.error('No task owner found') for task_owner in task_owners: bot.self.send_msg(MONGO_OBJ.get_task_by_name(task_owner)) elif content.strip().lower().startswith('tel'): tel_keys = content.strip().split()[1:] if len(tel_keys) < 1: logger.error('Not found') for tel_key in tel_keys: bot.self.send_msg(MONGO_OBJ.get_tel_info_by_key(tel_key)) #return时return_msg将发送到自己微信 return return_msg #文件消息 elif msg.type == 'Attachment': logger.info('Receive self file msg [%s][%s][%s][%s][%s]' % (msg.file_name, msg.type, msg.sender, msg.receiver, msg.sender.puid)) if msg.file_name == os.path.split(PROJ_SCHEDULE_FILE)[1]: msg.get_file(save_path=PROJ_SCHEDULE_FILE) logger.info('Save received file %s to %s' % (msg.file_name, PROJ_SCHEDULE_FILE)) MONGO_OBJ.xhx_task.remove() cnt = MONGO_OBJ.load_schedule(PROJ_SCHEDULE_FILE) bot.self.send_msg('%s tasks loaded' % cnt) elif msg.file_name == os.path.split(TEL_BOOK_FILE)[1]: msg.get_file(save_path=TEL_BOOK_FILE) logger.info('Save received file %s to %s' % (msg.file_name, TEL_BOOK_FILE)) MONGO_OBJ.tel_book.remove() cnt = MONGO_OBJ.load_tel_book(TEL_BOOK_FILE) bot.self.send_msg('%s tel loaded' % cnt)
data['BidNumber'].iloc[0], file_uuid) if data.empty: continue # file no longer exists data = data.groupby('Product').first().reset_index() # tp20_bid_shpr.to_csv('shipping.csv') for colname in data_type_dict.keys(): #logger.debug('Searching for the colname') if colname in data.columns: data[colname] = data[colname].astype( data_type_dict[colname]) except EOFError as e: logger.error( e, "Error 3.3a: Consumer.py crashed due to file access: " + e, logger) pass except Exception as e: logger.error( e, "Error 3.3b: Consumer.py crashed due to model run: ", logger) pass else: try: # Run the prediction using loaded model start_time = datetime.datetime.now() non_cwt_data = data[~data.Product_Mode. isin(['AIR_CWT', 'GND_CWT'])]
if 'python' in str(name): script_list = i.cmdline() from sys import platform if platform == 'win32' or platform == 'win64': if script_list and len( script_list) > 1 and 'consumer.py' in script_list[1]: consumer_is_running = True else: if script_list and len( script_list ) > 1 and home + '/bin/consumer.py' in script_list[1]: consumer_is_running = True if not consumer_is_running: logger.error( "Consumer is not running at this moment, Please start the consumer then producer." ) raise AssertionError( "Consumer is not running at this moment, Please start the consumer then producer." ) logger.debug("Consumer is running") # Check for catastrophic errors if cError: print "Startup encountered the following errors:" # logger.error("Startup encountered the following errors:") print errorMsg # logger.error(errorMsg) print "Startup was able to perform the following:" print successMsg sys.exit(1)
def print_error_message(e, file_name, logger=None, message=True): if message: traceMsg = traceback.format_exc() else: traceMsg = "" if logger is None: if hasattr(e, 'errno'): # PermissionError if e.errno == EPERM or e.errno == EACCES: print "Error 1.2a: Cannot read from file / folder {1} due to PermissionError({0}) in module {2}\n{3}".format( e.errno, e.strerror, file_name, traceMsg) # FileNotFoundError elif e.errno == ENOENT: print "Error 1.2a: Cannot read from file / folder {1} due to FileNotFoundError({0}) in module {2}\n{3}".format( e.errno, e.strerror, file_name, traceMsg) elif e.__class__ == IOError: print "Error 1.2a: Cannot read from file / folder {1} due to I/O error: {0}\n{2}".format( str(e), file_name, traceMsg) elif e.__class__ == OSError: print "Error 1.2a: Cannot read from file / folder {1} due to OS error: {0}\n{2}".format( str(e), file_name, traceMsg) else: # general error if hasattr(e, "strerror"): print "Error: {0} in module {1}\n{2}".format( e.strerror, file_name, traceMsg) elif hasattr(e, "message"): print "Error: {0} in module {1}\n{2}".format( e.message, file_name, traceMsg) else: print "Error in module {0}\n{1}".format( file_name, traceMsg) else: if hasattr(e, "strerror"): print file_name + ": " + e.strerror + "\n" print traceMsg elif hasattr(e, "message"): print file_name + ": " + e.message + "\n" print traceMsg else: print traceMsg else: if hasattr(e, 'errno'): # PermissionError if e.errno == EPERM or e.errno == EACCES: logger.error( "Error 1.2a: Cannot read from file / folder {1} due to PermissionError({0}) in module {2}\n{3}" .format(e.errno, e.strerror, file_name, traceMsg)) # FileNotFoundError elif e.errno == ENOENT: logger.error( "Error 1.2a: Cannot read from file / folder {1} due to FileNotFoundError({0}) in module {2}\n{3}" .format(e.errno, e.strerror, file_name, traceMsg)) elif e.__class__ == IOError: logger.error( "Error 1.2a: Cannot read from file / folder {1} due to I/O error: {0}\n{2}" .format(str(e), file_name, traceMsg)) elif e.__class__ == OSError: logger.error( "Error 1.2a: Cannot read from file / folder {1} due to OS error: {0}\n{2}" .format(str(e), file_name, traceMsg)) else: # general error if hasattr(e, "strerror"): logger.error("Error: {0} in module {1}\n{2}".format( e.strerror, file_name, traceMsg)) elif hasattr(e, "message"): logger.error("Error: {0} in module {1}\n{2}".format( e.message, file_name, traceMsg)) else: logger.error("Error in module {0}\n{1}".format( file_name, traceMsg)) else: if hasattr(e, "strerror"): logger.error(file_name + ": " + e.strerror + "\n") logger.error(traceMsg) elif hasattr(e, "message"): logger.error(file_name + ": " + str(e.message) + "\n") logger.error(traceMsg) else: logger.error(traceMsg)