def __init__(self, host, port, topic_s, topic_p, message, mic): self._logger = logging.getLogger(__name__) self.host = host self.port = port self.topic_s = topic_s self.topic_p = topic_p self.message = message self.mic = mic self.mqttc = mqtt.Client() self.mqttc.on_message = self.on_message self.mqttc.on_connect = self.on_connect #mqttc.on_publish = on_publish #mqttc.on_subscribe = on_subscribe #mqttc.on_log = on_log if self.host and self.topic_p: publish.single(self.topic_p, payload=self.message, hostname=self.host, port=self.port) if self.port and self.topic_s and self.host: self.mqttc.connect(self.host, self.port, 5) self.mqttc.subscribe(topic_s, 0) #while True: # self.mqttc.loop(timeout=5) self.mqttc.loop_start()
def fetchUnreadEmails(since=None, markRead=False, limit=None): """ Fetches a list of unread email objects from a user's email inbox. Arguments: since -- if provided, no emails before this date will be returned markRead -- if True, marks all returned emails as read in target inbox Returns: A list of unread email objects. """ logger = logging.getLogger(__name__) profile = config.get() conn = imaplib.IMAP4(profile[SLUG]['imap_server'], profile[SLUG]['imap_port']) conn.debug = 0 msgs = [] try: conn.login(profile[SLUG]['address'], profile[SLUG]['password']) conn.select(readonly=(not markRead)) (retcode, messages) = conn.search(None, '(UNSEEN)') except Exception: logger.warning("抱歉,您的邮箱账户验证失败了,请检查下配置") return None if retcode == 'OK' and messages != ['']: numUnread = len(str(messages[0]).split(' ')) if limit and numUnread > limit: return numUnread for num in messages[0].split(' '): # parse email RFC822 format ret, data = conn.fetch(num, '(RFC822)') if data is None: continue msg = email.message_from_string(data[0][1]) if not since or getDate(msg) > since: msgs.append(msg) if isEchoEmail(msg): conn.store(num, '+FLAGS', '\Seen') conn.close() conn.logout() return msgs
def fetchUnreadEmails(self, since=None, markRead=False, limit=None): """ Fetches a list of unread email objects from a user's email inbox. Arguments: since -- if provided, no emails before this date will be returned markRead -- if True, marks all returned emails as read in target inbox Returns: A list of unread email objects. """ logger = logging.getLogger(__name__) profile = config.get() conn = imaplib.IMAP4(profile[self.SLUG]["imap_server"], profile[self.SLUG]["imap_port"]) conn.debug = 0 msgs = [] try: conn.login(profile[self.SLUG]["address"], profile[self.SLUG]["password"]) conn.select(readonly=(not markRead)) (retcode, messages) = conn.search(None, "(UNSEEN)") except Exception: logger.warning("抱歉,您的邮箱账户验证失败了,请检查下配置") return None if retcode == "OK" and messages != [b""]: numUnread = len(messages[0].split(b" ")) if limit and numUnread > limit: return numUnread for num in messages[0].split(b" "): # parse email RFC822 format ret, data = conn.fetch(num, "(RFC822)") if data is None: continue msg = email.message_from_string(data[0][1].decode("utf-8")) if not since or self.getDate(msg) > since: msgs.append(msg) conn.close() conn.logout() return msgs
def translate(self, appId, appSecret, sentence): logger = logging.getLogger(__name__) url = 'https://openapi.youdao.com/api' salt = random.randint(1, 65536) sign = appId + sentence + str(salt) + appSecret m1 = hashlib.md5(sign.encode('utf-8')) sign = m1.hexdigest() params = { 'q': sentence, 'from': 'auto', 'to': 'auto', 'appKey': appId, 'salt': salt, 'sign': sign } result = requests.get(url, params=params) res = json.loads(result.text, encoding='utf-8') s = res['translation'][0] return s
def handle(self, text, parsed): logger = logging.getLogger(__name__) profile = config.get() if SLUG not in profile or \ 'appId' not in profile[SLUG] or\ 'appSecret' not in profile[SLUG]: self.say('有道翻译插件配置有误,插件使用失败', cache=True) return appId = profile[SLUG]['appId'] appSecret = profile[SLUG]['appSecret'] sentence = self.getSentence(text) logger.info('sentence: ' + sentence) if sentence: try: s = self.translate(appId, appSecret, sentence) if s: self.say(sentence + "的翻译是" + s, cache=False) else: self.say("翻译" + sentence + "失败,请稍后再试", cache=False) except Exception as e: logger.error(e) self.say('抱歉, 我不知道怎么翻译' + sentence, cache=False) else: self.say(u"没有听清楚 请重试", cache=True)
from server import server from tools import make_json, solr_tools from watchdog.observers import Observer import sys import os import fire import base64 import signal import hashlib import urllib3 import requests import multiprocessing import _thread as thread urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) logger = logging.getLogger(__name__) class Wukong(object): _profiling = False _dev = False def init(self): global conversation self.detector = None self._interrupted = False print(''' ******************************************************** * wukong-robot - 中文语音对话机器人 * * (c) 2019 潘伟洲 <*****@*****.**> * * https://github.com/wzpan/wukong-robot.git *
#!/usr/bin/env python import collections import pyaudio from . import snowboydetect from robot import utils, logging import time import wave import os from ctypes import CFUNCTYPE, c_char_p, c_int, cdll from contextlib import contextmanager from robot import constants logger = logging.getLogger("小一") TOP_DIR = os.path.dirname(os.path.abspath(__file__)) RESOURCE_FILE = os.path.join(TOP_DIR, "resources/common.res") DETECT_DING = os.path.join(TOP_DIR, "resources/ding.wav") DETECT_DONG = os.path.join(TOP_DIR, "resources/dong.wav") def py_error_handler(filename, line, function, err, fmt): pass ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p) c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)
#!/usr/bin/env python import collections import pyaudio from . import snowboydetect from robot import utils, logging import time import wave import os from ctypes import CFUNCTYPE, c_char_p, c_int, cdll from contextlib import contextmanager from robot import constants logger = logging.getLogger("snowboy") TOP_DIR = os.path.dirname(os.path.abspath(__file__)) RESOURCE_FILE = os.path.join(TOP_DIR, "resources/common.res") DETECT_DING = os.path.join(TOP_DIR, "resources/ding.wav") DETECT_DONG = os.path.join(TOP_DIR, "resources/dong.wav") def py_error_handler(filename, line, function, err, fmt): pass ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p) c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)