def get_stock_notify(self): wxbot = config.get_uni_obj("wxbot") if not wxbot: return watch_list = db.get_instance().get_notify_list() c_list = watch_list['codelist'] self._logger.info("watch:%s", str(c_list).decode('string_escape')) if c_list: m_list = watch_list['lastmodifylist'] msg_list = [] df = ts.get_realtime_quotes(c_list) sendFlag = False for index, row in df.iterrows(): modi_time = row['date'] + ' ' + row['time'] if index == 0: msg_list.append(' ' + str(index + 1) + ": ") else: msg_list.append(str(index + 1) + ": ") msg_list.append(row['code']) msg_list.append(row['name']) msg_list.append(row['price']) msg_list.append(row['time']) msg_list.append('\n') if modi_time != m_list[index]: sendFlag = True db.get_instance().update_notify(row['code'], modi_time) if sendFlag: msg = ' '.join(msg_list) wechatUser(config.profile, wxbot, '股价关注', msg)
def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): if self.wxbot is not None: wechatUser(self.profile, self.wxbot, "%s: %s" % (self.robot_name, phrase), "") self.speaker.say(phrase)
def nlpHandle(nlp, mic, profile, wxbot=None): """ Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) profile -- contains information related to the user (e.g., phone number) wxbot -- wechat bot instance """ def get_news(data): rs = [] for index, row in data.iterrows(): rs.append(str(index + 1)) for col_name in data.columns: rs.append(row[col_name]) rs.append('\n') return ' '.join(rs) sys.path.append(mic.dingdangpath.LIB_PATH) from app_utils import wechatUser text = nlp['text'] if any(word in text for word in [u"财经新闻"]): news = ts.get_latest_news(top=10, show_content=False) t = mic.asyncSay("已获取财经新闻," + ('将发送到您的微信' if wxbot != None else "篇幅较长,请登录微信获取")) if wxbot != None: wechatUser(profile, wxbot, '财经新闻', get_news(news)) t.join() elif any(word in text for word in [u"信息地雷"]): orgName = None code = None items = nlp['items'] for item in items: if item['ne'] == u'ORG': orgName = item['item'] break if orgName: code = db.get_instance().get_stock_code(orgName) else: m = pattern.search(text) if m: code = m.group(2) if code: orgName = db.get_instance().get_stock_name(code) if not orgName: mic.say("股票代码可能不存在") return notices = ts.get_notices(code) notices = notices[0:10] tit = orgName + '的信息地雷' t = mic.asyncSay("已获取" + tit + "," + ('将发送到您的微信' if wxbot != None else "篇幅较长,请登录微信获取")) if wxbot != None: wechatUser(profile, wxbot, tit, get_news(notices)) t.join() else: mic.say("没能获取股票代码")
def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): # alter phrase before speaking phrase = alteration.clean(phrase) if self.wxbot is not None: wechatUser(self.profile, self.wxbot, "%s: %s" % (self.robot_name, phrase), "") self.speaker.say(phrase)
def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): self._logger.info(u"机器人说:%s" % phrase) self.stop_passive = True if self.wxbot is not None: wechatUser(self.profile, self.wxbot, "%s: %s" % (self.robot_name, phrase), "") self.speaker.say(phrase) time.sleep(1) # 避免叮当说话时误唤醒 self.stop_passive = False
def chat(self, texts): """ 使用图灵机器人聊天 Arguments: texts -- user input, typically speech, to be parsed by a module """ msg = ''.join(texts) try: url = "http://www.tuling123.com/openapi/api" userid = str(get_mac())[:32] body = {'key': self.tuling_key, 'info': msg, 'userid': userid} r = requests.post(url, data=body) respond = json.loads(r.text) notSay = False result = '' if respond['code'] == 100000: result = respond['text'].replace('<br>', ' ') result = result.replace(u'\xa0', u' ') elif respond['code'] == 200000: result = respond['url'] notSay = True elif respond['code'] == 302000: for k in respond['list']: result = result + u"【" + k['source'] + u"】 " +\ k['article'] + "\t" + k['detailurl'] + "\n" notSay = True else: result = respond['text'].replace('<br>', ' ') result = result.replace(u'\xa0', u' ') notSay = True max_length = 200 if 'max_length' in self.profile: max_length = self.profile['max_length'] if notSay or (len(result) > max_length and \ self.profile['read_long_content'] is not None and \ not self.profile['read_long_content']): wxbot = config.get_uni_obj('wxbot') if wxbot is not None: t = self.mic.asyncSay(u'结果将发送到您的微信') wechatUser(self.profile, wxbot, "", result) t.join() else: self.mic.say(u'请扫码登录微信,结果将发送到您的微信') else: self.mic.say(result) except Exception: self._logger.critical("Tuling robot failed to responsed for %r", msg, exc_info=True) self.mic.say("抱歉, 我的大脑短路了 " + "请稍后再试试.")
def handle(text, mic, profile, wxbot=None): """ Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) profile -- contains information related to the user (e.g., phone number) wxbot -- wechat bot instance """ sys.path.append(mic.dingdangpath.LIB_PATH) from app_utils import wechatUser df = ts.realtime_boxoffice() rs = [] # rs.append("实时票房(万)") # rs.append("排名") # rs.append("影片名") # rs.append("票房占比 (%)") # rs.append("上映天数") # rs.append("累计票房(万)") # rs.append("数据获取时间") # rs.append("\n") for index, row in df.iterrows(): for col_name in df.columns: if col_name == "BoxOffice": rs.append("实时票房:" + row[col_name] + "万") elif col_name == "Irank": rs.append("排名:" + row[col_name]) elif col_name == "MovieName": rs.append("片名:" + row[col_name]) elif col_name == "boxPer": rs.append("票房占比:" + row[col_name]) elif col_name == "movieDay": rs.append("上映天数:" + row[col_name]) elif col_name == "sumBoxOffice": rs.append("累计票房:" + row[col_name]) elif col_name == "time": rs.append("获取时间:" + row[col_name]) rs.append('\n') msg = ' '.join(rs) tit = "电影票房实时排行榜" t = mic.asyncSay("已获取" + tit + "," + ('将发送到您的微信' if wxbot != None else "篇幅较长,请登录微信收取")) if wxbot != None: wechatUser(profile, wxbot, tit, msg) t.join()
def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav", cache=False): self._logger.info(u"机器人说:%s" % phrase) self.stop_passive = True if self.wxbot is not None: wechatUser(self.profile, self.wxbot, "%s: %s" % (self.robot_name, phrase), "") # incase calling say() method which # have not implement cache feature yet. # the count of args should be 3. if self.speaker.say.func_code.co_argcount > 2: self.speaker.say(phrase, cache) else: self.speaker.say(phrase) time.sleep(1) # 避免小黑说话时误唤醒 self.stop_passive = False
def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): wxbot = config.get_uni_obj('wxbot') if wxbot is not None and wxbot.is_login: wechatUser(self.profile, wxbot, "%s: %s" % (self.robot_name, phrase), "") t1 = time.time() t2 = t1 t3 = t2 with self.speaker_lock: t2 = time.time() self.speaker.say(phrase) t3 = time.time() self._logger.info("Mic say(%s) LockTime:%fs, SpeakTime:%fs", phrase, t2 - t1, t3 - t2)