示例#1
0
class Auto:
    def __init__(self, name="Test"):
        self.name = name

        self.http = Http()
        self.db = Database()
        self.db.execute(
            ''' 
            create table if not exists auto(
                url         text,
                about        text,
                time        text,
                v1       text,
                v2       text,
                v3       text,
                v4       text,
                v5       text
            )
            ''' )

        return
    # 日志输出
    def out(self, obj):
        print(time.strftime("%Y%m%d %H:%M:%S", time.localtime()) + "." + self.__module__ + self.name + "." + str(obj))
        # tools.out(obj)
        return
    # 实时控制帮助
    def help(self):
        self.out(dir(self))
        return
    # doMethod([methodName arg1 arg2]) -> methodName(arg1,arg2)
    def doMethod(self, listArgs):
        size = len(listArgs)
        res = None
        if(size > 0):
            if(hasattr(self, listArgs[0])):
                method = getattr(self, listArgs[0])
                if(callable(method)):
                    if(size == 2):
                        res = method(listArgs[1])
                    elif(size == 3):
                        res = method(listArgs[1], listArgs[2])
                    elif(size == 4):
                        res = method(listArgs[1], listArgs[2], listArgs[3])
                    elif(size == 5):
                        res = method(listArgs[1], listArgs[2], listArgs[3], listArgs[4])
                    else:
                        res = method()
                else:
                    self.out(method)
        return res
    # 手动命令监控
    def inputHello(self):
        self.out("开启输入监控!")
        self.help()
        while(True):
            try:
                cmd=raw_input("")
                if(cmd != ""):
                    if(not self.doMethod(cmd.split(" "))):
                        self.doCmd(cmd)
                        time.sleep(1)
            except Exception as e:
                self.out(repr(e))
        return
    # 测试用
    def test(self):
        self.login()
        # 输入监控线程
        ThreadRun( "InputHello." + str(self.name),  self.inputHello ).start()

        tool.wait()
        return
    # 非函数调用 属性变量查看 其他的指令控制
    def doCmd(self, cmd):
        self.out("其他指令." + str(cmd))
        
        return

    # 认证登录
    def login(self):
        self.out("访问主页 获取 token session")
#        http://content.icbc.com.cn/site1/a5f9ff6d068a46e68d363426ca69bd07/login_unfirst.html?pageOrder=login_unfirst.html;index.html&_mp_cmp_ttime=1548294412328
# "http://content.icbc.com.cn/site1/a5f9ff6d068a46e68d363426ca69bd07/login_unfirst.html?pageOrder=login_unfirst.html;index.html&_mp_cmp_ttime=1545268253550"
#http://content.icbc.com.cn/site1/a5f9ff6d068a46e68d363426ca69bd07/login_unfirst.html?pageOrder=login_unfirst.html;index.html&_mp_cmp_ttime=1552005766720
        ttt = str(tool.getNowTime())
        kkk = 'a5f9ff6d068a46e68d363426ca69bd07'
        url = "http://content.icbc.com.cn/site1/" + kkk + "/login_unfirst.html?pageOrder=login_unfirst.html;index.html&_mp_cmp_ttime=" + ttt
        
        
#BIGipServerwifi_ContextPlat_80_Pool	2097346570.20480.0000	N/A	N/A	N/A	59				
#cmp_glb_param	98f7808c851a9f53da5f0c3f1639b678	N/A	N/A	N/A	46				
#layerNettimeType	true	N/A	N/A	N/A	23				
#layerPosterType	true	N/A	N/A	N/A	22				
#layerSafeType	true	N/A	N/A	N/A	20

#Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
#Accept-Encoding: gzip, deflate
#Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
#Connection: keep-alive

        header = {
            "Cookie"    : "BIGipServerwifi_ContextPlat_80_Pool=2097346570.20480.0000; layerSafeType=true; layerPosterType=true; layerNettimeType=true; cmp_glb_param=98f7808c851a9f53da5f0c3f1639b678"
            
        }
        self.http.addHeader(header)
        
        self.http.show()
        responce = self.http.doGet(url)
        re = responce.read()
        soup =BeautifulSoup.BeautifulSoup(re)
        nameList = soup.findAll('button',{'id':{'login_btn'}})
        if(len(nameList) > 0):
            self.out(nameList)
            self.out("抓取成功: ")
            self.http.show(responce)
            self.out("模拟登录")
# http://content.icbc.com.cn/cmp/AuthSkipController.do?method=authSkip&ajaxRequest=true
            url = "http://content.icbc.com.cn/cmp/AuthSkipController.do?method=authSkip&ajaxRequest=true"
#{"msg":"GlbInfo检查不通过","retCode":1,"success":false,"data":{"needJump":"http%3A%2F%2Fwww.baidu.com%2F"}}

            json=self.http.doJson(url)
            self.out((json))
            self.out(json["msg"])
            self.out(json["data"]["needJump"])
        else:
            self.out("error! 没能抓取到token")
        return False
示例#2
0
class Auto:
    def __init__(self, name="Test"):
        self.name = name

        self.http = Http()
        self.db = Database()
        self.db.execute(''' 
            create table if not exists auto(
                url         text,
                about        text,
                time        text,
                v1       text,
                v2       text,
                v3       text,
                v4       text,
                v5       text
            )
            ''')

        return

    # 日志输出
    def out(self, obj):
        print(
            time.strftime("%Y%m%d %H:%M:%S", time.localtime()) + "." +
            self.__module__ + self.name + "." + str(obj))
        # tools.out(obj)
        return

    # 实时控制帮助
    def help(self):
        self.out(dir(self))
        return

    # doMethod([methodName arg1 arg2]) -> methodName(arg1,arg2)
    def doMethod(self, listArgs):
        size = len(listArgs)
        res = None
        if (size > 0):
            if (hasattr(self, listArgs[0])):
                method = getattr(self, listArgs[0])
                if (callable(method)):
                    if (size == 2):
                        res = method(listArgs[1])
                    elif (size == 3):
                        res = method(listArgs[1], listArgs[2])
                    elif (size == 4):
                        res = method(listArgs[1], listArgs[2], listArgs[3])
                    elif (size == 5):
                        res = method(listArgs[1], listArgs[2], listArgs[3],
                                     listArgs[4])
                    else:
                        res = method()
                else:
                    self.out(method)
        return res

    # 手动命令监控
    def inputHello(self):
        self.out("开启输入监控!")
        self.help()
        while (True):
            try:
                cmd = raw_input("")
                if (cmd != ""):
                    if (not self.doMethod(cmd.split(" "))):
                        self.doCmd(cmd)
                        time.sleep(1)
            except Exception as e:
                self.out(repr(e))
        return

    # 测试用
    def test(self):
        self.login()
        # 输入监控线程
        ThreadRun("InputHello." + str(self.name), self.inputHello).start()

        tool.wait()
        return

    # 非函数调用 属性变量查看 其他的指令控制
    def doCmd(self, cmd):
        self.out("其他指令." + str(cmd))

        return

    # 认证登录
    def login(self):
        self.out("访问主页 获取 token session")
        responce = self.http.doGet('http://drrr.com/')
        re = responce.read()
        soup = BeautifulSoup.BeautifulSoup(re)
        nameList = soup.findAll('input', {'name': {'token'}})
        if (len(nameList) > 0):
            token = nameList[0]['data-value']
            token = tool.encode(token)
            self.out("抓取成功: ")
            self.out("token\t " + token)
            self.out("cookie\t " + tool.toString(self.http.getCookie()))
            self.out("模拟登录")
            responce = self.http.doPost(
                'http://drrr.com/', {
                    "name": name,
                    "login": "******",
                    "token": token,
                    "direct-join": "",
                    "language": "zh-CN",
                    "icon": icon,
                })
            if (responce != "error"):
                return True
            else:
                return False
        else:
            self.out("error! 没能抓取到token")
        return False
示例#3
0
class AutoCochat:
    def __init__(self, name="Test"):
        self.name = name

        self.http = Http()
        self.db = Database()
        self.db.execute(''' 
            create table if not exists music(
                url         text primary key,
                name        text,
                fromName    text,
                count       text
            )
            ''')

        return

    # 日志输出
    def out(self, obj):
        print(self.__module__ + "." + self.name + "." + str(obj))
        return

    # 实时控制帮助
    def help(self):
        self.out(dir(self))
        return

    # doMethod([methodName arg1 arg2]) -> methodName(arg1,arg2)
    def doMethod(self, listArgs):
        size = len(listArgs)
        res = None
        if (size > 0):
            if (hasattr(self, listArgs[0])):
                method = getattr(self, listArgs[0])
                if (callable(method)):
                    if (size == 2):
                        res = method(listArgs[1])
                    elif (size == 3):
                        res = method(listArgs[1], listArgs[2])
                    elif (size == 4):
                        res = method(listArgs[1], listArgs[2], listArgs[3])
                    elif (size == 5):
                        res = method(listArgs[1], listArgs[2], listArgs[3],
                                     listArgs[4])
                    else:
                        res = method()
                else:
                    self.out(method)
        return res

    # 手动命令监控
    def inputHello(self):
        self.out("开启输入监控!")
        self.help()
        while (True):
            try:
                cmd = raw_input("")
                if (cmd != ""):
                    if (not self.doMethod(cmd.split(" "))):
                        self.doCmd(cmd)
                        time.sleep(1)
            except Exception as e:
                self.out(repr(e))
        return

    # 测试用
    def test(self):
        self.login()
        ThreadRun("InputHello." + str(self.name), self.inputHello).start()
        tool.wait()
        return

    # 执行非 函数 属性 输入处理
    def doCmd(self, cmd):

        return

    # 认证登录
    def login(self):
        pass
        return
示例#4
0
class AutoCochat:
    def __init__(self, name="Test", id="18408249138", pwd="1234qwer"):
        reload(sys)
        sys.setdefaultencoding('utf8') #针对socket发送中文异常
        self.detaTime = 3600 * 1000 #推送延时 60min
        self.detaTimeMin = 1200 * 1000 #推送延时 20min

        self.id = id
        self.pwd = pwd
        self.name = name
        self.robot = Robot()
        self.http = Http()
        self.db = Database('sqlite_' + self.id + '.db')
        self.socket = Socket()
        self.onConnect = False
        self.ifOk = False
        # sendTime添加时间, type消息类型, msg发送消息体, deta间隔发送时间, preTime预期发送时间节点
        self.db.execute(
            ''' 
            create table if not exists cochat(
                id  text,
                sendTime    text,
                sendTimeT text,
                deta    text,
                preTime text,
                preTimeT text,
                type        text,
                msg    text,
                flag    text
            )
            ''' )
        self.sendList = [] #发送队列 (time, deta, type, data)
        return
    # 日志输出
    def out(self, obj):
        print(time.strftime("%Y%m%d %H:%M:%S", time.localtime()) + "." + self.name + "." + str(obj))
        return
    # 实时控制帮助
    def help(self):
        self.out(dir(self))
        return
    # doMethod([methodName arg1 arg2]) -> methodName(arg1,arg2)
    def doMethod(self, listArgs):
        size = len(listArgs)
        res = None
        if(size > 0):
            if(hasattr(self, listArgs[0])):
                method = getattr(self, listArgs[0])
                if(callable(method)):
                    if(size == 2):
                        res = method(listArgs[1])
                    elif(size == 3):
                        res = method(listArgs[1], listArgs[2])
                    elif(size == 4):
                        res = method(listArgs[1], listArgs[2], listArgs[3])
                    elif(size == 5):
                        res = method(listArgs[1], listArgs[2], listArgs[3], listArgs[4])
                    else:
                        res = method()
                else:
                    self.out(method)
        return res
    # 手动命令监控
    def inputHello(self):
        self.out("开启输入监控!")
        self.help()
        while(True):
            try:
                cmd=raw_input("")
                if(cmd != ""):
                    if(not self.doMethod(cmd.split(" "))):
                        # self.out("exm???")
                        # self.out(cmd)
                        pass
                    time.sleep(1)
            except Exception as e:
                self.out(traceback.format_exc())
        return
    def send(self, ttt, data, deta=-1):
        if(deta == -1):
            deta = tools.getRandom(self.detaTime, self.detaTimeMin)
        sendTime = tool.getNowTime()
        preTime = sendTime + deta
        sendTimeT = tool.parseTime(sendTime/1000, "%D %H:%M:%S")
        preTimeT = tool.parseTime(preTime/1000, "%D %H:%M:%S")

        # self.sendList1.append((sendTime, deta, preTime, type, data))
        # sendTime添加时间, type消息类型, msg发送消息体, deta间隔发送时间, preTime预期发送时间节点
        self.db.execute("insert into cochat values(?,?,?,?,?,?,?,?,?)", str(self.id), str(sendTime),str(sendTimeT), str(deta), str(preTime),str(preTimeT), str(ttt), str(data),'0')

        return
    # 定时任务
    def timeSend(self):
        self.out("开启定时任务 发送队列!")
        self.nowNight = 0
        while(True):
            time.sleep(5) #每分钟扫描 只对非凌晨时间处理 延时一小时发送
            timeNow = tool.getNowTime()
            hour = tool.parseTime(timeNow/1000, "%H:%M")
            if(hour >= "23:30" or hour <= "07:20" or self.ifOk == False):
                if(self.nowNight != 1):
                    self.out("不是白天 或者 没有登录 now:" + str(hour) )
                self.nowNight = 1
                continue
            self.nowNight = 0
            try:
                # self.sendList1.append((sendTime, deta, preTime, type, data))
                self.sendList = self.db.executeQuery("select * from cochat where preTime<=? and flag=0 and id=?", timeNow, str(self.id) )
                i = len(self.sendList) - 1
                while(i >= 0):
                    obj = self.sendList[i]
                    sendTime = int(obj.get("sendTime", 0))
                    deta = int(obj.get("deta", 0))
                    preTime = int(obj.get("preTime", 0))
                    type = str(obj.get("type", ""))
                    data = ast.literal_eval((str(obj.get("msg", ""))))
                    info = ""
                    hour = tool.parseTime(preTime/1000, "%D %H:%M:%S")
                    info = str(timeNow) + "->" + str(preTime) + " 推送时刻:" + str(hour)
                    if(timeNow >= preTime):
                        info += " 触发"
                        self.sendTrue(type, data)
                        # self.sendList.pop(i)
                    else:
                        info += " "
                    self.out(tool.parseTime(timeNow/1000, "%D %H:%M:%S") + "." + str(type) + "." + str(data) + "." + info)
                    i = i - 1
                self.db.execute("update cochat set flag='1' where preTime<=? and flag=0 and id=? ", timeNow, str(self.id))
            except Exception as e:
                self.out(traceback.format_exc())
        return

    # 定时任务
    def timeHello(self):
        self.out("开启定时任务!")
        self.help()
        while(True):
            time.sleep(3600 * 3)
            try:
                self.out("定时重新连接")
                self.login()
            except Exception as e:
                self.out(repr(e))
        return
    # 测试用
    def test(self):
        self.login()
        ThreadRun( "TimeHello." + str(self.name),  self.timeHello ).start()
        # ThreadRun( "TimeCtrl." + str(self.name),  self.inputHello ).start()
        ThreadRun( "TimeSend." + str(self.name),  self.timeSend ).start()
        self.socket.waitRead(self.onException)    #异常回调
        tool.wait()
        return
    # 监控执行
    def doCmd(self, cmd):
        self.send({"data":cmd})
        return
    def login(self):
        i = 0
        while(self.ifOk == False or i <= 0):
            try:
                self.whileLogin()
                i = i + 1
            except Exception as e:
                self.out(traceback.format_exc())
                self.out("登录异常,5s后重试 try:" + str(i))
            time.sleep(5)
        return

    # 认证登录
    def whileLogin(self):
        self.ifOk = False
        if(self.onConnect):
            self.out("已经在尝试登录")
            return
        self.socket.close()
        self.onConnect = True
        self.out("尝试登录:")
# {CONF_V   ARS: "*", ORG_VARS: true, logintype: "mobile", id: "18408249138", password: "******"}
# Request URL:http://picc.cochat.cn/SY_ORG_LOGIN.login.do?DESKTOP_OS=Win10&USER_LAST_BROWSER=Win32&USER_LAST_CLIENT=2.5.1&USER_LAST_OS=DESKTOP&USER_LAST_PCNAME=%7B%7D
# request Cookie:JSESSIONID=abcb0skaQYCGs6lvy9orw
        obj = self.http.doJson("http://picc.cochat.cn/SY_ORG_LOGIN.login.do?DESKTOP_OS=Win10&USER_LAST_BROWSER=Win32&USER_LAST_CLIENT=2.5.1&USER_LAST_OS=DESKTOP&USER_LAST_PCNAME=%7B%7D",{
            "CONF_VARS":"*",
            "ORG_VARS":"true",
            "logintype":"mobile",
            "id":self.id,
            "password":self.pwd
        })
        if(obj.get("error","") != ""):
            self.onConnect = False
            return
        self.loginUser = obj
        token = obj.get("USER_TOKEN", "")
        self.out("登录结果 token:" + token)
        urlWithPort = obj.get("CONF_VARS", {}).get("@C_SY_COMM_SOCKET_SERV_V1.0@", "http://cochat.cn:9091")
        uus = urlWithPort.split(':')
        port = int(uus[2])
        url = uus[1][2:999] #cochat.cn 不需要ws http 只需要ip 域名

        self.socketServerUrl = urlWithPort
        self.socketUrl = url
        self.socketPort = port
        self.showUser()

        self.config = {
            "transports":['websocket', 'polling'],  # websocket优先
            "timeout":5 * 1000, # 超时时间
            "forceNew": True,
            "reconnection" : False
        };
        self.out("socket开始")
        self.socket.connect(url,port) # , params=self.config)
        self.out("socket连接完成,开始初始化事件")

        socketMsgTypes = ("connect", "disconnect","error","connect_error","connect_timeout","connecting","reconnecting","message", "event")
        # socketMsgTypes = ("connect", "disconnect","message", "event")
        for item in socketMsgTypes:
            if(hasattr(self, item)):
                method = getattr(self, item)
                if(callable(method)):
                    self.socket.on(item, method)
                else:
                    self.out("变量而非方法" + item + "回调?")
            else:
                self.out("属性" + item + "不存在,是否写错了名字?")
        # self.socket.on("message", self.message)

        self.out("socket初始化事件完成,开始发送认证")
        self.data = {
            "userName":obj.get("USER_CODE", ""),
            "displayName": tool.encode(obj.get("ORG_VARS", {}).get("@USER_NAME@", "") ), #"ccc",#
            "odept":obj.get("ORG_VARS", {}).get("@ODEPT_CODE@", ""),
            "token":obj.get("USER_TOKEN", ""),
            "uuid":"" + str(uuid.uuid1()),
            "version":obj.get("USER_CODE", "") + "_LAST_MSG"
        }
        self.out(self.data)
        self.socket.emit('loginv17', self.data, self.onSocketLogin)
        self.out("已发送认证信息")
        self.ifOk = True
        self.onConnect = False
        return
    def connect(self, *args):
        print("connect")
        print(args)
        # self.login()
        return
    def disconnect(self, *args):
        print("disconnect")
        print(args)
        self.login()
        return
    def error(self, *args):
        print("error")
        print(args)
        self.login()
        return
    def connect_error(self, *args):
        print("connect_error")
        print(args)
        return
    def connect_timeout(self, *args):
        print("connect_timeout")
        print(args)
        return
    def connecting(self, *args):
        print("connecting")
        print(args)
        return
    def reconnect(self, *args):
        print("reconnect")
        print(args)
        return
    def reconnecting(self, *args):
        print("reconnecting")
        print(args)
        return
    def onException(self, *args):
        tool.line()
        self.out("onexception")
        print(args)
    def turnArray(self, args):
        if(len(args) == 1):
            args = args[0]
            reg = re.match(r'^\d+', args)         #25[xxxx] -> [xxxxx]
            if(reg is not None):
                start = reg.group()
                args = args[len(start):999999]
            # reg = re.match(r'^\["\w+",', args)         #["message",  {"to":{"id":" ->  {"to":xxxx
            # if(reg is not None):
            #     start = reg.group()
            #     args = args[len(start):999999]
            #     mtype = start[2:-2]
            res = tool.toJson(args)
        else:
            res = list(args)
            if(len(res) <= 1):
                res = ( 'null', res[0])
            elif(callable(res[1])):
                res = ( 'fun', res[0])

        return res
    def message(self, *args): # 普通消息
        try:
            # print("收到message ")
            # print(args)
            # tool.line()
            # print("转换")
            args = self.turnArray(args)
            # print(args[0])
            # print(args[1])
            if(len(args) <= 1):
                return

            data = args[1] #{}
            mtype = args[0] #message null fun
            if(mtype == 'message'):
                data = tool.toJson(data)
                fro = data.get("from", {})
                to = data.get("to", {})
                contact = data.get("contact", {})
                msg = data.get("body")
                fullId = contact.get("fullId")
                sessionName = contact.get("nickName", "")

                uid = self.data.get("uuid")
                tTag = data.get("timeMillis", tool.getNowTime())
                tool.line()
                self.out("Msg:" + fro.get("nickName","from") + ">>" + msg + ">>" + to.get("nickName","to") + " time:" + data.get("time"))
                # self.out(msg);

                # 自发消息不需要处理
                if(fro.get("nickName","from").find(self.loginUser.get("ORG_VARS", {}).get("@USER_NAME@", "")) >= 0):
                    return

                ttt = self.detaTime
                reg = re.match(r'^\d+$', str(msg))
                if(reg is not None):
                    ttt = int(msg) * 1000


                self.send("updateConversationStatus", {
                    'contactFullId': fullId,
                    'clientId': uid,
                    'timeTag': tTag
                }, ttt)
                self.send("updateMsgStatus", {
                    "messages":data.get("id","")
                }, ttt)


                # 自发言 且 只有自己auto回复
                if(self.id != "18408249138"):
                    return
                # 过滤
                ff = sessionName.find("陈鹏辉")
                hh = fro.get("nickName","from").find("许欢")
                zz = fro.get("nickName","from").find("赵振国")
                cdf = fro.get("nickName","from").find("迪")
                d1 = sessionName.find("迪")
                d2 = sessionName.find("祝")
                d3 = sessionName.find("纯")
                if(ff < 0 or d1 >= 0 or d2 >= 0 or d3 >= 0):
                    self.out("未命中自己title 命中特殊 不回复")
                    return
                point = 76
                if(hh >= 0 or zz >= 0):
                    point = 33
                    self.sendTrue("updateMsgStatus", {
                        "messages":data.get("id","")
                    })
                if(tool.getRandom(0,100) < point):
                    self.out("概率不自动回复" + str(point))
                    return

                # data["body"] = str(data["body"]) + "."
                # self.send("message", data)

                obj = {}
                # if(contact.get("type") == "GROUP"):
                # unicode(self.robot.do(msg, fro.get("nickName")))
                msg = self.robot.do(msg, fro.get("nickName"))
                msg = msg.get("text", '');
                if(msg == ''):
                    return
                obj["body"] = msg #"666" + str(tool.getNowTime())
                obj["bodyType"] = "text"
                obj["clientId"] = str(uuid.uuid1())
                obj["retry"] = 1
                obj["from"] = {}
                obj["from"]["fullId"] = "u__" + self.data.get("userName")
                obj["from"]["id"] = self.data.get("userName")
                obj["from"]["nickName"] = "fromnickname"
                obj["to"] = {}

                if(contact.get("type") == "GROUP"):
                    obj["to"]["fullId"] = contact.get("fullId")
                else:
                    obj["to"]["fullId"] = fro.get("fullId")
                obj["to"]["nickName"] = "tonickname"

                obj["from"]["nickName"] = self.loginUser["ORG_VARS"]["@USER_NAME@"]   # "from-nickName"
                obj["to"]["nickName"] = "to-nickName"

                self.sendTrue("message", obj)
            elif(str(mtype) == 'fun'):
                pass
            elif(str(mtype) == 'event'):
                pass
            else:
                self.out("其他:" + str(mtype))
                print(data)
                # print(args)
                # tool.line()
        except Exception as e:
            self.out(traceback.format_exc())

        return

    def event(self, *args): # 事件消息 群创建?
        print("event")
        print(args)
        return
    def onSocketLogin(self, *data):
        self.out("socket登录回调:")
        print(data)
        return


    def sendTrue(self, type, data):
        self.socket.send(type, data)
        return


    def showUser(self):
        tool.line()
        obj = self.loginUser
        user = obj.get("ORG_VARS", {})
        self.out(user.get("@USER_NAME@", ""))
        self.out(user.get("@USER_POST@", ""))
        self.out(user.get("@LOGIN_NAME@", ""))
        self.out("USER_CODE:" + obj.get("USER_CODE", ""))
        self.out("USER_TOKEN:" + user.get("UESR_TOKEN", ""))
        self.out("socketServerUrl:" + self.socketServerUrl)
        self.out("to url:" + self.socketUrl)
        self.out("to port:" + str(self.socketPort))

        tool.line()
        return
示例#5
0
class Auto:
    def __init__(self, name="Test"):
        self.name = name

        self.http = Http()
        self.db = Database()
        self.db.execute(
            ''' 
            create table if not exists auto(
                url         text,
                about        text,
                time        text,
                v1       text,
                v2       text,
                v3       text,
                v4       text,
                v5       text
            )
            ''' )

        return
    # 日志输出
    def out(self, obj):
        print(time.strftime("%Y%m%d %H:%M:%S", time.localtime()) + "." + self.__module__ + self.name + "." + str(obj))
        # tools.out(obj)
        return
    # 实时控制帮助
    def help(self):
        self.out(dir(self))
        return
    # doMethod([methodName arg1 arg2]) -> methodName(arg1,arg2)
    def doMethod(self, listArgs):
        size = len(listArgs)
        res = None
        if(size > 0):
            if(hasattr(self, listArgs[0])):
                method = getattr(self, listArgs[0])
                if(callable(method)):
                    if(size == 2):
                        res = method(listArgs[1])
                    elif(size == 3):
                        res = method(listArgs[1], listArgs[2])
                    elif(size == 4):
                        res = method(listArgs[1], listArgs[2], listArgs[3])
                    elif(size == 5):
                        res = method(listArgs[1], listArgs[2], listArgs[3], listArgs[4])
                    else:
                        res = method()
                else:
                    self.out(method)
        return res
    # 手动命令监控
    def inputHello(self):
        self.out("开启输入监控!")
        self.help()
        while(True):
            try:
                cmd=raw_input("")
                if(cmd != ""):
                    if(not self.doMethod(cmd.split(" "))):
                        self.doCmd(cmd)
                        time.sleep(1)
            except Exception as e:
                self.out(repr(e))
        return
    # 测试用
    def test(self):
        self.login()
        # 输入监控线程
        ThreadRun( "InputHello." + str(self.name),  self.inputHello ).start()

        tool.wait()
        return
    # 非函数调用 属性变量查看 其他的指令控制
    def doCmd(self, cmd):
        self.out("其他指令." + str(cmd))
        
        return

    # 认证登录
    def login(self):
        self.out("访问主页 获取 token session")
# "http://content.icbc.com.cn/site1/a5f9ff6d068a46e68d363426ca69bd07/login_unfirst.html?pageOrder=login_unfirst.html;index.html&_mp_cmp_ttime=1545268253550"
        ttt = '1545268253550' #str(tool.getNowTime())
        kkk = 'a5f9ff6d068a46e68d363426ca69bd07'
        url = "http://content.icbc.com.cn/site1/" + kkk + "/login_unfirst.html?pageOrder=login_unfirst.html;index.html&_mp_cmp_ttime=" + ttt
        
        responce = self.http.doGet(url)
        re = responce.read()
        soup =BeautifulSoup.BeautifulSoup(re)
        nameList = soup.findAll('button',{'id':{'login_btn'}})
        if(len(nameList) > 0):
            self.out(nameList)
            self.out("抓取成功: ")
            self.out("cookie\t " + tool.toString(self.http.getCookie()))
            self.out("模拟登录")
# http://content.icbc.com.cn/cmp/AuthSkipController.do?method=authSkip&ajaxRequest=true
            url = "http://content.icbc.com.cn/cmp/AuthSkipController.do?method=authSkip&ajaxRequest=true"
            json=self.http.doJson(url)
            self.out((json))
        else:
            self.out("error! 没能抓取到token")
        return False
示例#6
0
class Robot:
    """机器人智能语义应答"""

    id = ""
    name = ""

    def __init__(self):
        self.id = "test id"
        self.name = "test name"
        self.http = Http()
        self.auto163 = Auto163("Music")
        self.apiKey = "bfbf6432b655493b9e861b470bca9921"
        self.userId = "WalkerDust"

        self.db = Database()
        self.db.execute(''' 
        create table if not exists music(
            url         text primary key,
            name        text,
            duration    text,
            fromName    text,
            count       text
        )
        ''')
        self.db.execute(''' 
            create table if not exists user(
                name        text primary key,
                id          text,
                icon        text,
                flag        text
            )
            ''')
        self.db.execute(''' 
            create table if not exists msg(
                id            text primary key, 
                userName      text,
                data          text,
                time          text
            )
            ''')
        self.initMusic()
        self.palyHistoryMusic = []
        return

    def out(self, obj):
        print(self.__module__ + "." + str(obj))
        return
# 音乐模块

    def initMusic(self):
        li = ""
        count = self.db.getCount("select * from music ")
        if (count <= 0):  #毫无数据 则 加入默认数据
            li = []
            with open('music.txt', 'r') as f:
                data = f.readlines()
                for item in data:
                    name = item.strip()
                    url = "http://39.107.26.100:8088/file/" + name
                    fromName = ""
                    self.db.execute('insert into music values(?,?,?,?,?)', url,
                                    name, "0", fromName, "5")
        return

    def nextMusic(self, name=""):
        self.nextNames.append(name)

    # 内部点播 若有名字则按照名字本地搜索和云搜索 否则 按照type切歌
    def turnMusic(self, playType):
        music = {}
        if (playType == -1 and len(self.palyHistoryMusic) > 1):  #上一曲
            music = self.palyHistoryMusic.pop()  #弹出顶 取末
            music = self.palyHistoryMusic[-1]
        else:
            cc = 0
            while (music.get("url", "") == ""):
                size = self.db.getCount("select * from music ")
                if (size <= 0):
                    break
                num = 5
                page = int(1.0 * size / num)
                page = tool.getRandom(0, page)
                (size,
                 listRes) = self.db.executeQueryPage("select * from music",
                                                     page, num)

                getSize = len(listRes)
                count = tool.getRandom(0, getSize)
                music = listRes[count]
                tool.line()
                self.out("随机找到歌曲页 " + "size:" + str(size) + "  page:" +
                         str(page) + " num:" + str(num) + " listResSize:" +
                         str(getSize))
                for item in listRes:
                    self.out("url:" + item.get("url") + " name:" +
                             item.get("name") + " fromName:" +
                             item.get("fromName") + " time:" +
                             str(tool.calcTime(item.get("duration", 0))))
                self.out("选中了" + str(count))
                tool.line()
                url = music.get("url", "")
                if (self.http.existAudio(url)):
                    break
                else:
                    self.out("访问音乐文件失败" + url)
                    self.removeMusic(url)
                    music = {}
                if (cc > 20):
                    break
                cc = cc + 1
        return music

    def getMusic(self, musicName="", fromName=""):
        music = {}

        if (musicName != ""):
            # res = self.db.executeQueryOne("select * from music where name=? ", musicName)
            # if(res.get("url", "") != ""):
            #     music = res
            # else:
            res = self.auto163.getMusic(musicName, fromName)  # [music,music]
            rres = []
            for item in res:
                if (self.addMusic(item)):  #有效歌曲才参与接下来的选择
                    rres.append(item)
            res = rres
            if (len(res) > 0):
                #按照 前面权重依次递减 1 2 3 4 5 -> 16 8 4 2 1
                ii = tool.getRandomWeight(0, len(res))
                self.out("0, " + str(len(res)) + " -> " + str(ii))
                music = res[ii]
        if (music.get("url", "") != ""):
            self.addHistory(music)

        return music

    def addHistory(self, music):
        self.palyHistoryMusic.append(music)
        if (len(self.palyHistoryMusic) > 20):
            self.palyHistoryMusic.pop(0)

    # 外部点播音乐记录
    def addMusic(self, music):
        url = music.get("url", "")
        name = music.get("name", "")
        fromName = music.get("fromName", "")
        duration = music.get("duration", 0)
        oldMusic = self.db.executeQueryOne(
            "select * from music where url = ? ", url)
        if (oldMusic.get("url", "") == ""):
            # self.out("添加音乐")
            # self.out(music)
            self.db.execute('insert into music values(?,?,?,?,?)', url, name,
                            duration, fromName, "1")
        else:  #更新该音乐数据
            # self.out("更新音乐")
            count = int(oldMusic.get("count", 0))
            count = str(count + 1)
            music["count"] = count
            # self.out(music)
            self.db.execute(
                'update music set name=?, fromName=?, count=?,duration=? where url=?',
                name, fromName, count, duration, url)
        if (not self.http.existAudio(url)):
            self.out("访问音乐文件失败" + url)
            self.removeMusic(url)
            return False

        return True

    def removeMusic(self, url=""):
        index = 0
        self.out("移除音乐" + url)
        self.db.execute('delete from music where url = ? ', url)
        return
# 人员信息管理 权限 状态
#     name        text primary key,
#     id        text,
#     icon    text,
#     flag    text

    def addUser(self, user={}):
        name = user.get("name", "")
        id = user.get("id", "")
        icon = user.get("icon", "")
        flag = user.get("flag", "0")
        if (self.db.getCount("select * from user where name=?", name) > 0):
            self.db.execute("update user set id=?,icon=?,flag=? where name=?",
                            id, icon, flag, name)
        else:
            self.db.execute(
                "insert into user(name,id,icon,flag) values(?,?,?,?)", name,
                id, icon, flag)

    def turnUser(self, name, flag):
        if (self.db.getCount("select * from user where name=?", name) > 0):
            self.db.execute("update user set flag=? where name=?", flag, name)
        else:
            self.db.execute("insert into user(name,flag) values(?,?)", name,
                            flag)

    def getUser(self, name=""):
        res = self.db.executeQueryOne("select * from user where name=?", name)
        if (res == None):
            res = {}
        return res

# 消息监控

    def addMsg(self, id, userName, data, msgTime):
        if (self.db.getCount("select * from msg where id=?", id) <= 0):
            self.db.execute('insert into msg values(?,?,?,?)', id, userName,
                            data, msgTime)
        return


# 智能应答

    def do(self, msg, userId="CC"):
        res = ""
        if (msg == ""):
            return "消息不能为空"
        response = self.http.doPost(
            'http://www.tuling123.com/openapi/api',
            {
                "key": self.apiKey,
                "info": msg,
                "userid": userId,
                # "userInfo":{
                #     "apiKey":self.apiKey,
                #     "userId":self.userId,
                # },
                # "reqType":0,
                # "perception":{
                #     "inputText":{
                #         "text":msg,
                #     }
                # },
            })
        jsonStr = response.read()

        if (jsonStr != ""):
            res = tool.makeObj(json.loads(jsonStr))
            code = res.get("code", "")
            self.out("Robot. " + str(msg) + " -> " + jsonStr)
        else:
            self.out("Robot. " + str(msg) + " -> error !!!!!!!!! ")
        return res

    def doParse(self, obj):
        res = ""
        text = obj.get("text", "")
        res = res + text
        url = obj.get("url", "")
        list = obj.get("list", "")
        if (url != ""):
            res = res + " \n" + url
        i = 0
        if (list != "" and len(list) > 0):
            for item in list:
                # res = res + " \n"
                # for key,value in item.items():
                #     res = res + " \n" + str(value)
                ttt = item.get("url", "")
                if (ttt != ""):
                    res = res + " \n" + str(ttt)

                ttt = item.get("detailurl", "")
                if (ttt != ""):
                    res = res + " \n" + str(ttt)

                if (len(res) > 200):
                    break
            # {
            #     "name": "鱼香肉丝",
            #     "icon": "",
            #     "info": "瘦肉、黑木耳、胡萝卜、靑椒、豆瓣酱,葱姜蒜、白糖,香醋,料酒",
            #     "detailurl": "http://m.xiachufang.com/recipe/100352761/?ref=tuling"
            # }
        return res

    def test(self):
        while (True):
            cmd = raw_input("")
            if (cmd != ""):
                res = self.do(cmd)
                self.out(res)
                time.sleep(1)

        return