Exemplo n.º 1
0
 def __init__(self):
     self.operate = Operate()
     self._api = OpenApi()
     self._http = HttpClient.getInstance()
     self._pool = ThreadPool(5)    # 初始化5个线程
     print("Task Class 初始化完毕")
Exemplo n.º 2
0
class Tasks(Codes):

    def __init__(self):
        self.operate = Operate()
        self._api = OpenApi()
        self._http = HttpClient.getInstance()
        self._pool = ThreadPool(5)    # 初始化5个线程
        print("Task Class 初始化完毕")

    def getAllAdmin(self):
        print("所有管理员: %s", variable.Admins)
        return variable.Admins

    def getAllGroup(self):
        print("所有关注群: %s", variable.Groups)
        return variable.Groups

    def addAdmin(self, qq):
        return self.operate.addAdmin(qq)

    def delAdmin(self, qq):
        return self.operate.delAdmin(qq)

    def isAdmin(self, qq):
        return self.operate.isAdmin(qq)

    def addGroup(self, qq):
        return self.operate.addGroup(qq)

    def delGroup(self, qq):
        return self.operate.delGroup(qq)

    def inGroup(self, qq):
        # print("inGroup: %s", qq)
        return self.operate.inGroup(qq)

    def addAsk(self, question, answer):
        return self.operate.addAsk(question, answer)

    def delAsk(self, Id):
        return self.operate.delAsk(Id)

    def getAsk(self, content):
        return self.operate.getAsk(content)

    def end(self):
        self._pool.destroy()

    def uin_to_qq(self, uin):
        if uin in variable.UsersQQ:
            return variable.UsersQQ.get(uin)
        print("获取qq %s %s %s", uin, variable.Vfwebqq, variable.Referer)
        html = self._http.get(variable.Get_friend_uin2.format(uin, self.bytesToStr(variable.Vfwebqq)), referer = variable.Referer)
        print("uin_to_qq: %s", html)
        try:
            result = json.loads(self.bytesToStr(html))
            if result.get("retcode") != 0:
                return ""
            qq = result.get("result").get("account")
            if qq:
                variable.UsersQQ[uin] = str(qq)
                return str(qq)
        except Exception as e:
            print(e)
            return ""

    def sendMsg(self, *args, **kwargs):
        print("回复消息")
        url = kwargs.get("url")
        data = kwargs.get("data")
        # print(data)
        referer = kwargs.get("referer")
        result = self._http.post(url = url, data = data, referer = referer)
        print("回复结果: %s", result)

    def otherMsg(self, content, to, url, uin):
        if content:
            html = self._http.get(url = variable.RobotUrl.format(quote(content), uin))
            html = html.replace("\\n", "").replace("\n", "")
            html = self._api.parse(html)
            html = self._api.getResult()
            if html:
                print("智能回复: ", html)
                data = {'r' : variable.Msg_Data.format(to, uin, html, variable.Clientid, variable.Msgid, variable.Psessionid)}
                print(data)
                self._pool.add_task(callback = self.sendMsg, url = url, data = data, referer = variable.Referer)

    def analyze(self, qq, uin, content, iseq = None):
        print("开始解析消息")
        if iseq:
            print("消息来自群")
            to = "group_uin"
            url = variable.Send_qun_msg2
        else:
            print("消息来自好友")
            to = "to"
            url = variable.Send_buddy_msg2
        # 是管理员
        if self.isAdmin(qq) and content in ("开启机器人", "关闭机器人", "退出"):
            # 解析管理员命令
            _msg = ""
            print("是管理员消息")
            if content == "开启机器人":
                variable.State = True
                print("机器人已开启")
                _msg = "机器人已开启"
            elif content == "关闭机器人":
                variable.State = False
                print("机器人已关闭")
                _msg = "机器人已关闭"
            elif content == "退出":
                variable.State = False
                variable.Exit = True
                print("机器人已退出")
                _msg = "机器人已退出"
            if _msg:
                data = {'r' : variable.Msg_Data.format(to, uin, _msg, variable.Clientid, variable.Msgid, variable.Psessionid)}
                self._pool.add_task(callback = self.sendMsg, url = url, data = data, referer = variable.Referer)
            return
        # 给我发送的私人消息(是否是命令)
        result = variable.Command.findall(content)
        if result and to == "to":
            ver, msg = result[0]
            _msg = ""
            if ver == variable.AddAdmin:
                # 添加管理员
                print("添加管理员")
                if self.addAdmin(msg):
                    _msg = "添加管理员: " + msg + " 成功"
                else:
                    _msg = "添加管理员: " + msg + " 失败"
            elif ver == variable.DelAdmin:
                # 删除管理员
                print("删除管理员")
                if self.delAdmin(msg):
                    _msg = "删除管理员: " + msg + " 成功"
                else:
                    _msg = "删除管理员: " + msg + " 失败"
            elif ver == variable.AddAttention:
                # 添加关注群号
                print("添加关注群")
                if self.addGroup(msg):
                    _msg = "添加关注群: " + msg + " 成功"
                else:
                    _msg = "添加关注群: " + msg + " 失败"
            elif ver == variable.DelAttention:
                # 删除关注群号
                print("删除关注群号")
                if self.delGroup(msg):
                    _msg = "删除关注群: " + msg + " 成功"
                else:
                    _msg = "删除关注群: " + msg + " 失败"
            if _msg:
                data = {'r' : variable.Msg_Data.format(to, uin, _msg, variable.Clientid, variable.Msgid, variable.Psessionid)}
                self._pool.add_task(callback = self.sendMsg, url = url, data = data, referer = variable.Referer)
            return
        if content.startswith("#") and len(content) > 2 and self.inGroup(iseq):
            # 开头以#开头并且长度大于2并且是关注群发送的消息
            i = 0
            content = content[1:].strip()
            for w in content:
                if w in variable.Filter:
                    print("发现过滤词: ", w)
                    i += 1
            if i == 0:
                self._pool.add_task(callback = self.otherMsg, content = content, to = to, url = url, uin = uin)
                return
            else:
                data = {'r' : variable.Msg_Data.format(to, uin, "你想干什么!f**k", variable.Clientid, variable.Msgid, variable.Psessionid)}
                self._pool.add_task(callback = self.sendMsg, url = url, data = data, referer = variable.Referer)
                return
        if to == "to" and len(content) > 2:
            # 私人消息
            self._pool.add_task(callback = self.otherMsg, content = content.strip(), to = to, url = url, uin = uin)
            return

    def delwith(self, fuin, suin, iseq, content):
        '''
        #fuin 消息发送者
        #suin 群消息发送人qq
        #iseq 群号码
        #content 消息内容
        '''
        print("%s %s %s %s", fuin, suin, iseq, content)
        # 如果是群文件消息
        if content.startswith("<?xml"):
            print("发现共享文件")
        # 发送者qq
        if iseq and suin:
            qq = self.uin_to_qq(suin)
        else:
            qq = self.uin_to_qq(fuin)
        print("qq: %s", qq)
        self.analyze(qq, fuin, content, iseq)