Пример #1
0
def create_table(db_name):
    conn = sqlite3.connect(db_name)
    try:
        create_tb_msgdata = ''' 
		CREATE TABLE IF NOT EXISTS msgdata 
		(id INT PRIMARY KEY,
		msgtype TEXT,
		msgtext TEXT, 
		whosend TEXT, 
		sendtowhere TEXT,
		senddate TEXT
		); 
		'''
        create_tb_splitword = ''' 
		CREATE TABLE IF NOT EXISTS splitword 
		(id INT PRIMARY KEY,
		wordpos TEXT,
		theword TEXT, 
		fromwho TEXT, 
		sendtowhere TEXT,
		senddate TEXT
		); 
		'''
        #主要就是上面的语句
        conn.execute(create_tb_msgdata)
        conn.execute(create_tb_splitword)
    except Exception as e:
        myException("STEP OF func:create_table",
                    "Create table failed db_name=[%s]" % db_name, e)
        return False
    else:
        print 'success'
Пример #2
0
    def toDate(self, fmt, dayStr, fromFmt='%Y%m%d%H%M%S"'):
        try:
            timeStruct = time.strptime(dayStr, "%Y%m%d")
        except:
            try:
                timeStruct = time.strptime(dayStr, "%Y-%m-%d")

            except:
                try:
                    timeStruct = time.strptime(dayStr, "%Y-%m-%d %H:%M:%S")
                except:
                    try:
                        timeStruct = time.strptime(dayStr, "%Y/%m/%d %H:%M:%S")
                    except:
                        try:
                            timeStruct = time.strptime(dayStr, fromFmt)
                        except Exception as e:
                            raise myException(e, sys.argv[0],
                                              sys._getframe().f_back.f_lineno)
        try:
            resultDate = time.strftime(fmt, timeStruct)

            return resultDate
        except Exception as e:
            raise myException(e, sys.argv[0], sys._getframe().f_back.f_lineno)
Пример #3
0
    def text_reply(msg):
        global dict_chatroomName_Value, list_randomReplyMsg
        global sendToWhere, theMsg, whoSend
        global pauseNum
        print 'pauseNum is', pauseNum
        replyText = ""
        if 0 == pauseNum:
            #member NickName
            whoSend = msg['ActualNickName']
            #msg text
            theMsg = msg['Content']
            #group Value
            sendToWhere = msg['FromUserName']
            #print '#text_reply'
            print sendToWhere  #群聊的地址
            #if sendToWhere in dict_chatroomName_Value.values():
            msgtext = ""
            if 'Text' == msg['Type']:

                if 1 == isIstructions(msg['Text'], isgroupchat=2):
                    replyText = "-Instruction_Confirmed"
                    itchat.send(u'@%s\u2005 \n%s' % (whoSend, replyText),
                                sendToWhere)
                    print replyText
                    return 1
                else:
                    #这条群聊消息不是 指令;继续完成【获取相应消息】【储存当前消息】操作
                    msgtext = theMsg
                    #储存
                    strtime = time.strftime('%Y_%m_%d_%H_%M_%S',
                                            time.localtime(time.time()))
                    try:

                        saveMsg("Text", whoSend, sendToWhere, msgtext, strtime,
                                1)
                    except Exception as e:
                        myException("STEP OF func:saveMsg",
                                    "saveMsg failed msgtext[%s]" % msgtext, e)
                    else:
                        pass
                    finally:
                        pass

                    replyText = talks_robot(theMsg)

            else:
                pass
                #replyText = list_randomReplyMsg[random.randint(0,len(list_randomReplyMsg)-1)]

            #itchat.send(u'@%s\u2005I received: %s'%(whoSend, theMsg), sendToWhere)
            print sendToWhere
            #replyText = talks_robot(msgtext)
            itchat.send('%s' % (replyText), sendToWhere)
            #itchat.send(u'@%s\u2005I received: %s'%(whoSend, theMsg), sendToWhere)
            #else:
            #	print '-PingBi'
        else:
            pauseNum -= 1
Пример #4
0
def init():
    global myValue, myName, db_name, dict_especialFriendName_Value, dict_chatroomName_Value
    global dict_chatroomName_Value, dict_Instructions_dict
    global SPLITWORD
    global sendToWhere, whoSend, SPLITWORD
    dict_myinfo = itchat.search_friends()
    # print dict_myinfo
    # print dict_myinfo["NickName"]
    # print dict_myinfo["UserNamef"]
    myName = dict_myinfo["NickName"]
    myValue = dict_myinfo["UserName"]
    #连接数据库建表
    #create_table(db_name)
    #获取配置信息
    cp = ConfigParser.SafeConfigParser()
    with codecs.open('config.conf', 'r', encoding='utf-8') as f:
        cp.readfp(f)

    flg = 1  #有效
    for x in xrange(1, 0xFFFFFFF):
        Nametemp = ""
        flg = 1  #有效
        if cp.has_option('chatroomName', str(x)):
            try:
                #特别 群
                Nametemp = cp.get('chatroomName', str(x))
                list_dict_tmpvalue = (itchat.search_chatrooms(
                    name=Nametemp))[0]
                if 1 < len(list_dict_tmpvalue):
                    valuetemp = list_dict_tmpvalue["UserName"]
                    (dict_Instructions_dict['*dictCR'])[Nametemp] = valuetemp
            except Exception as e:
                myException(
                    "STEP OF func:init",
                    "chatroomName get value error; Nametemp=[%s]" % Nametemp,
                    e)
        else:
            flg -= 1
        if cp.has_option('especialFriend', str(x)):
            try:
                #特别 人
                Nametemp = cp.get('especialFriend', str(x))
                list_dict_tmpvalue = itchat.search_friends(name=Nametemp)[0]
                if 1 < len(list_dict_tmpvalue):
                    valuetemp = list_dict_tmpvalue["UserName"]
                    (dict_Instructions_dict['*dictEF'])[Nametemp] = valuetemp
            except Exception as e:
                myException(
                    "STEP OF func:init",
                    "chatroomName get value error; Nametemp=[%s]" % Nametemp,
                    e)
        else:
            flg -= 1
        if -1 >= flg:
            #循环结束
            break
        print x, Nametemp
Пример #5
0
def executeSqlSttmnt(conn, sqlsttmnt):
    try:
        cursor = conn.execute(sqlsttmnt)
        conn.commit()
        return cursor
    except Exception as e:
        myException("STEP OF func:executeSqlSttmnt",
                    "sqlsttmnt=[%s]" % sqlsttmnt, e)
        return -1
    else:
        pass
    finally:
        pass
Пример #6
0
def hadoopToFileQ(cmdQueue,filePath):
    while True:
        try:
            cmd=cmdQueue.get(block=False)
            try:
                command = '%s >> %s' % (cmd, filePath)
                print command
                os.system(command)
            except Exception as e:
                raise myException(e, sys.argv[0], sys._getframe().f_back.f_lineno)
        except Exception as e:

            raise myException(e, sys.argv[0], sys._getframe().f_back.f_lineno)
Пример #7
0
def monthAdd(dayStr, count):
    try:
        timeStruct = datetime.datetime.strptime(dayStr, "%Y%m%d")
    except:
        try:
            timeStruct = datetime.datetime.strptime(dayStr, "%Y-%m-%d")
        except Exception as e:
            raise myException(e, sys.argv[0], sys._getframe().f_back.f_lineno)
    try:
        resultDate = (timeStruct +
                      relativedelta(months=int(count))).strftime('%Y-%m-%d')

        return resultDate
    except Exception as e:
        raise myException(e, sys.argv[0], sys._getframe().f_back.f_lineno)
Пример #8
0
def hadoopToFileL(cmdList,filePath):

    for cmd in cmdList:
        try:
            command = '%s >> %s' % (cmd, filePath)
            os.system(command)

        except Exception as e:
            raise myException(e, sys.argv[0], sys._getframe().f_back.f_lineno)
def parseString(string):
	"""parse"""
	list_word = []
	seg_list = []
	try:
		seg_list = jieba.posseg.cut(string)
	except Exception as e:
		myException("STEP OF func:parseString","string:%s"%(string),e)
		return 
	else:
		"""第一次分词成功"""
		for a_seg in seg_list:
			if 1 >= len(a_seg.word):
				continue
			else:
				list_word.append(a_seg.word)
		print list_word
		return list_word

			
Пример #10
0
def addOrdelDictNV(v_p):
    #ADDORDELDICTNV_[%s dictname]_ADD|OR_[%sname]
    global dict_chatroomName_Value, dict_Instructions_dict
    global SPLITWORD
    global sendToWhere, whoSend, SPLITWORD
    if whoSend != myValue:
        print 'No permission'
        return -1
    try:
        listtmp = v_p.split(SPLITWORD)
        dictname = listtmp[0]
        addOrdel = listtmp[1]
        Nametemp = listtmp[2]
        #删除数据项指令
        print dict_Instructions_dict.keys()
        if "DEL" == addOrdel and dictname in dict_Instructions_dict.keys():
            del (dict_Instructions_dict[dictname])[Nametemp]
            return 1
        #增加数据项指令
        list_dict_tmpvalue = []
        if "*dictCR" == dictname and "ADD" == addOrdel:
            list_dict_tmpvalue = (itchat.search_chatrooms(name=Nametemp))[0]

        elif "*dictEF" == dictname and "ADD" == addOrdel:
            list_dict_tmpvalue = itchat.search_friends(name=Nametemp)[0]
        if 1 < len(list_dict_tmpvalue):
            valuetemp = list_dict_tmpvalue["UserName"]
            (dict_Instructions_dict[dictname])[Nametemp] = valuetemp
            return 1
        else:
            myException(
                "STEP OF addOrdel",
                "addOrdel [%s] is unknown,please check your instruction" %
                (addOrdel), "-")

    except Exception as e:
        myException("STEP OF func:addOrdelChatRoomNV()", "v_p=%s" % (v_p), e)
    else:
        pass
    finally:
        pass
Пример #11
0
def ourCloud(v_p):
    global dict_chatroomName_Value, dict_Instructions_dict
    global SPLITWORD
    global sendToWhere, whoSend, SPLITWORD
    print 'ourCloud'
    #def genWordCloud(dict_data,rootPath = path.dirname(__file__),imgName="ico1.jpg",saveFileName="wordcloud_init.png"):
    #构造 词-词频字典
    try:
        dict_ourcloud = {}
        conn = sqlite3.connect(db_name)
        sqlsttmnt_p = "SELECT theword,COUNT(theword) FROM %s WHERE sendtowhere LIKE \'%s\'  GROUP BY theword ;"\
         %(splitwordtable,sendToWhere)
        cursor = executeSqlSttmnt(conn, sqlsttmnt_p)
        for row in cursor:
            print row[0], '-', row[1],
            dict_ourcloud[row[0]] = row[1]

        #调用genWordCloud
        filename = "ourcloud_%s.png" % (time.strftime(
            '%Y_%m_%d_%H_%M_%S', time.localtime(time.time())))
        genWordCloud(dict_ourcloud,
                     path.dirname(__file__),
                     imgName="ico1.png",
                     saveFileName=filename)
        #2.'@fil@文件地址'将会被识别为传送文件,'@img@图片地址'将会被识别为传送图片,'@vid@视频地址'将会被识别为小视频
        itchat.send(u'@%s\u2005 our wordcloud as follow' % whoSend,
                    sendToWhere)
        itchat.send('@img@%s' % filename, sendToWhere)
        print 'genWordCloud-ourcloud success'
        return 1
    except Exception as e:
        myException(
            "STEP OF func:ourCloud-genWordCloud and send img",
            "genWordCloud or send img error; dict_ourcloud=[%s]" %
            dict_ourcloud, e)
    else:
        cnn.close()
    finally:
        pass
Пример #12
0
def hadoopToFile(cmd,filePath):
    cmd = cmd.strip()
    command = '%s >> %s' % (cmd, filePath)
    try:
        print command
        a=os.system(command)

        if a == 0:
            return a
        else:
            return 1
    except Exception as e:

        raise myException(e, sys.argv[0], sys._getframe().f_back.f_lineno)
Пример #13
0
def isIstructions(stri, isgroupchat):
    global SPLITWORD
    try:
        list_str = stri.split(SPLITWORD, 1)
        print list_str
        stri = list_str[0]
        if dict_Instructions_func.has_key(stri):
            #没有带参数时 默认为 isgroupchat的值
            value_p = list_str[1] if 1 < len(list_str) else isgroupchat
            if 1 == (dict_Instructions_func.get(stri)(value_p)):
                return 1
        return -1
    except Exception as e:
        myException(
            "STEP OF func:isIstructions()",
            "instruction [%s] parse error,please check your instruction" %
            stri, e)
        # print 'warning -zhilingshibie'
        # print e
        return -1
    else:
        pass
    finally:
        pass
Пример #14
0
    def talks_robot(info='你叫什么名字'):
        global robotNmbr, dict_group_model
        global sendToWhere, whoSend, SPLITWORD
        n = 404
        try:
            #通过群聊名称值 得到模式值
            n = dict_group_model[sendToWhere]
        except Exception as e:
            dict_group_model[sendToWhere] = n
            myException("STEP OF func:talks_robot", "get robotNmbr error", e)

        if 0 == n:
            #0模式 随机选择api
            #suiji
            n = random.randint(1, 3)

        if n == 1:
            #图灵机器人
            api_url = 'http://www.tuling123.com/openapi/api'
            apikey = 'APIKey'
            data = {'key': '3869ad1b82cc41ffa4757e7799c88609', 'info': info}
            req = requests.post(api_url, data=data).text
            s = requests.session()
            s.keep_alive = False
            replys = json.loads(req)['text']
            return replys
        elif n == 2:
            #小黄鸡机器人
            #QQ:	b026dea2-f0a8-47a3-90f2-aba9be93c219
            #163key 	b4461336-b04e-4e9a-9d3a-33328a81e0ed
            api_url = 'http://sandbox.api.simsimi.com/request.p?'
            key = "b026dea2-f0a8-47a3-90f2-aba9be93c219"
            lc = "zh"
            ft = "0.0"
            text = info
            api_url = 'http://sandbox.api.simsimi.com/request.p?key=%s&lc=%s&ft=%s&text=%s'%( \
               key,lc,ft,text)
            req = requests.get(api_url).text
            s = requests.session()
            s.keep_alive = False
            replys = json.loads(req)['response']
            return replys
        elif n == 3:
            #微软小冰
            api_url = 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0'
            apikey = 'APIKey'
            headers = {'Host': 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0','Ocp-Apim-Subscription-Key':'<Your Subscription key>',\
             'Content-Type': 'application/json','Cache-Control': 'no-cache'}
            data = {'question': info}
            req = requests.post(api_url, data=data).text
            print req
            s = requests.session()
            s.keep_alive = False
            replys = json.loads(req)['text']
            return replys
        elif n == 404:
            #跳过回答
            raise Exception
            api_url = 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0'
            apikey = 'APIKey'
            headers = {'Host': 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0','Ocp-Apim-Subscription-Key':'<Your Subscription key>',\
             'Content-Type': 'application/json','Cache-Control': 'no-cache'}
            data = {'question': info}
            req = requests.post(api_url, data=data, timeout=1).text
            print req
            s = requests.session()
            s.keep_alive = False
            replys = json.loads(req)['text']
            return replys
Пример #15
0
def saveMsg(msgtype, whosend, sendtowhere, msgtext, sendtime, isgroupchat=2):
    #存入sqlite
    # msgtype TEXT,
    # msgtext TEXT,
    # whosend TEXT,
    # sendtowhere TEXT,
    # senddate TEXT

    # id INT PRIMARY KEY,
    # wordpos TEXT,
    # theword TEXT,
    # fromwho TEXT,
    # sendtowhere TEXT,
    # senddate TEXT
    global db_name
    sqlsttmnt = ""
    returnvalue = ""
    t_str = sendtime
    try:

        #t_str= time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime(time.time()))

        conn = sqlite3.connect(db_name)
        print "###"
        #存入 msgtext 表
        sqlsttmnt = "INSERT INTO %s VALUES (%s,\'%s\',\'%s\',\'%s\',\'%s\',\'%s\'); " % (
            msgdatatable, 'NULL', msgtype, msgtext, whosend, sendtowhere,
            t_str)
        returnvalue = executeSqlSttmnt(conn, sqlsttmnt)

        #存入 splitword 表
        list_word = parseString(msgtext)
        for aword in list_word:
            sqlsttmnt = "INSERT INTO %s VALUES (%s,\'%s\',\'%s\',\'%s\',\'%s\',\'%s\'); " % (
                splitwordtable, 'NULL', "pos", aword, whosend, sendtowhere,
                t_str)
            returnvalue = executeSqlSttmnt(conn, sqlsttmnt)
    except Exception as e:
        myException("STEP OF func:saveMsg", "sqlsttmnt=[%s]" % sqlsttmnt, e)
    else:
        conn.close()
    finally:
        pass

    #sendtowhere -群 	whosend-发消息的人ActualNickName
    #储存 每个群中每个人发送的消息
    global dict_GroupAndMem
    cmsgtext = classMsgText(msgtype, whosend, sendtowhere, msgtext, time)
    #群是否存在
    if sendtowhere not in dict_GroupAndMem.keys():
        #群不存在 新增群、用户
        print 'no group no user'
        list_classMsgText = []
        dict_MemAndListMsg = {}

        list_classMsgText.append(cmsgtext)
        dict_MemAndListMsg[whosend] = list_classMsgText
        dict_GroupAndMem[sendtowhere] = dict_MemAndListMsg
    elif whosend not in dict_GroupAndMem[sendtowhere].keys():
        #群存在 成员不存在
        print 'have group no user'
        list_classMsgText = []

        list_classMsgText.append(cmsgtext)
        dict_GroupAndMem[sendtowhere][whosend] = list_classMsgText
    else:
        #群、成员都存在
        print 'have group have user'
        dict_GroupAndMem[sendtowhere][whosend].append(cmsgtext)

    print 'Group amount:',len(dict_GroupAndMem),'Member amount:',len(dict_GroupAndMem[sendtowhere]),\
       'Msg amount:',len(dict_GroupAndMem[sendtowhere][whosend])

    #dict2json()
    print "--saveMsg success"
    return 1
Пример #16
0
            req = requests.post(api_url, data=data).text
            print req
            s = requests.session()
            s.keep_alive = False
            replys = json.loads(req)['text']
            return replys
        elif n == 404:
            #跳过回答
            raise Exception
            api_url = 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0'
            apikey = 'APIKey'
            headers = {'Host': 'https://westus.api.cognitive.microsoft.com/qnamaker/v1.0','Ocp-Apim-Subscription-Key':'<Your Subscription key>',\
             'Content-Type': 'application/json','Cache-Control': 'no-cache'}
            data = {'question': info}
            req = requests.post(api_url, data=data, timeout=1).text
            print req
            s = requests.session()
            s.keep_alive = False
            replys = json.loads(req)['text']
            return replys

    itchat.run()
except Exception as e:
    myException("STEP OF mainprocess", "main", e)
    # print 'warning -mainprocess'
    # print e
else:
    pass
finally:
    a = input("PAUSE")