Exemplo n.º 1
0
    def Update(self):
        #############################
        # カウント対象か
        if self.FLG_Valid != True:
            ##更新なし
            return True

        #############################
        # ドメインの抽出
        wDomain = self.Obj_Parent.CHR_Account.split("@")
        if len(wDomain) != 2:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: Update: Illigal admin_id: " +
                self.Obj_Parent.CHR_Account)
            return False
        wDomain = wDomain[1]

        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: Update: DB Connect test is failed: " +
                wRes['Reason'] + " domain=" + wDomain)
            wOBJ_DB.Close()
            return False

        #############################
        # ドメイン存在チェック
        wQuery = "select * from TBL_TRAFFIC_DATA where " + \
           " domain = '" + wDomain + "' " + \
           ";"
        wRes = wOBJ_DB.RunQuery(wQuery)
        wRes = wOBJ_DB.GetQueryStat()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: Update: Run Query is failed: " +
                wRes['Reason'] + " query=" + wRes['Query'])
            wOBJ_DB.Close()
            return False

        if len(wRes['Responce']['Data']) != 1:
            ##存在しない
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_Traffic: Update: select Domain is failed: " + " domain=" +
                wDomain + " nums=" + str(len(wRes['Responce']['Data'])))
            wOBJ_DB.Close()
            return False

        #############################
        # カウント値の取り出し
        for wLineTap in wRes['Responce']['Data']:
            wGetTap = []
            for wCel in wLineTap:
                wGetTap.append(wCel)
                ## [0] ..domain
                ## [1] ..count
                ## [2] ..rat_count
                ## [3] ..now_count
                ## [4] ..rat_days
                ## [5] ..now_days

        wCount = int(wGetTap[1])

        #############################
        # カウントアップ
        wCount += self.VAL_NowCount

        #############################
        # テーブルの更新
        wQuery = "update TBL_TRAFFIC_DATA set " + \
           "count = " + str(wCount) + " " + \
           "where domain = '" + wDomain + "' ;"

        wRes = wOBJ_DB.RunQuery(wQuery)
        wRes = wOBJ_DB.GetQueryStat()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: Update: Update Count is failed: " +
                wRes['Reason'] + " domain=" + wDomain)
            wOBJ_DB.Close()
            return False

        #############################
        # DB切断
        wOBJ_DB.Close()
        return True  #成功
Exemplo n.º 2
0
    def Get_Trend(self):
        #############################
        # ドメインの抽出
        wDomain = self.Obj_Parent.CHR_Account.split("@")
        if len(wDomain) != 2:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Trend: Get_Trend: Illigal admin_id: " +
                self.Obj_Parent.CHR_Account)
            return
        wDomain = wDomain[1]

        #############################
        # トレンド取得対象のドメインか
        if wDomain not in self.ARR_SendDomain:
            self.Obj_Parent.OBJ_Mylog.Log('c', "トレンド取得対象外のドメイン")
            return

        #############################
        # mastodonからトレンド取得
        wRes = self.__getTrends()
        if wRes['Result'] != True:
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_Trend: Get_Trend: Trend read failed: " + wRes['Reason'])
            return

        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Trend: Get_Trend: DB Connect test is failed: " +
                wRes['Reason'])
            wOBJ_DB.Close()
            return

        #############################
        # 自ドメインのトレンド情報を一旦削除
        wQuery = "delete from TBL_TREND where domain = '" + wDomain + "'" + \
          ";"
        wDBRes = wOBJ_DB.RunQuery(wQuery)
        wDBRes = wOBJ_DB.GetQueryStat()
        if wDBRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Trend: Get_Trend: Run Query is failed: " +
                wDBRes['Reason'] + " query=" + wDBRes['Query'])
            wOBJ_DB.Close()
            return

        #############################
        # 最新のトレンド情報を追加
        wKeyList = self.ARR_Trend.keys()
        wRank = 1
        for wKey in self.ARR_Trend:
            wName = str(self.ARR_Trend[wKey]['name']).replace("'", "''")
            wQuery = "insert into TBL_TREND values (" + \
               str(wRank) + "," + \
               "'" + wName + "'," + \
               "'" + wDomain + "'," + \
               str(self.ARR_Trend[wKey]['uses']) + "," + \
               str(self.ARR_Trend[wKey]['accs']) + "," + \
               "'" + str(self.ARR_Trend[wKey]['lupdate']) + "' " + \
               ") ;"

            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_Trend: Get_Trend: Run Query is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return
            wRank += 1

        #############################
        # 正常終了
        wOBJ_DB.Close()

        #############################
        # カウント
        self.STR_Cope['Now_Cope'] += len(self.ARR_Trend)
        return
Exemplo n.º 3
0
    def SendTraffic(self):
        #############################
        # トラヒックが有効か
        if gVal.STR_MasterConfig['Traffic'] != "on":
            return False

        #############################
        # MasterUserか
        if gVal.STR_MasterConfig['MasterUser'] != self.Obj_Parent.CHR_Account:
            return False  #SubUserは抜ける

        #############################
        # 1時間経ってる周回か
        if gVal.STR_TimeInfo['OneHour'] == False:
            return False  #Traffic送信周回ではない

        #############################
        # トゥート送信対象のトラヒックドメイン読み込み
        ###読み出し先初期化
        if self.__getTrafficPatt() != True:
            return False  #失敗
        if len(self.ARR_SendDomain) == 0:
            ###デフォルトだと指定あるのでいちお入れる
            self.Obj_Parent.OBJ_Mylog.Log('c', "CLS_Traffic: トラヒック送信の指定先=なし")
            return False  ###対象なし

        #############################
        # ドメインの抽出
        wDomain = self.Obj_Parent.CHR_Account.split("@")
        if len(wDomain) != 2:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: Update: Illigal admin_id: " +
                self.Obj_Parent.CHR_Account)
            return False
        wDomain = wDomain[1]

        #############################
        # 送信処理開始

        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: SendTraffic: DB Connect test is failed: " +
                wRes['Reason'] + " domain=" + wDomain)
            wOBJ_DB.Close()
            return False

        #############################
        # 全トラヒックのロード
        wQuery = "select * from TBL_TRAFFIC_DATA ;"

        wRes = wOBJ_DB.RunQuery(wQuery)
        wRes = wOBJ_DB.GetQueryStat()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: SendTraffic: Run Query is failed: " +
                wRes['Reason'] + " query=" + wRes['Query'])
            wOBJ_DB.Close()
            return False

        if len(wRes['Responce']['Data']) == 0:
            ##トラヒック情報なし
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_Traffic: SendTraffic: TBL_TRAFFIC_DATA record is 0")
            wOBJ_DB.Close()
            return False

        #############################
        # カウント値の取り出しと、リセット
        wTraffic = {}
        wIndex = 0
        for wLineTap in wRes['Responce']['Data']:
            ##領域の準備
            wTraffic.update({wIndex: ""})
            wTraffic[wIndex] = {}
            wTraffic[wIndex].update({"domain": ""})
            wTraffic[wIndex].update({"count": 0})
            wTraffic[wIndex].update({"rat_count": 0})
            wTraffic[wIndex].update({"now_count": 0})
            wTraffic[wIndex].update({"rat_days": 0})
            wTraffic[wIndex].update({"now_days": 0})
            wTraffic[wIndex].update({"rat_wday": 0})
            wTraffic[wIndex].update({"now_wday": 0})

            ##カウンタの取り出し
            wGetTap = []
            for wCel in wLineTap:
                wGetTap.append(wCel)
                ## [0] ..domain
                ## [1] ..count
                ## [2] ..rat_count
                ## [3] ..now_count
                ## [4] ..rat_days
                ## [5] ..now_days

            ##領域へロード
            wTraffic[wIndex].update({"domain": wGetTap[0].strip()})
            wTraffic[wIndex].update({"count": int(wGetTap[1])})
            wTraffic[wIndex].update({"rat_count": int(wGetTap[2])})
            wTraffic[wIndex].update({"now_count": int(wGetTap[3])})
            wTraffic[wIndex].update({"rat_days": int(wGetTap[4])})
            wTraffic[wIndex].update({"now_days": int(wGetTap[5])})

            ##カウンタのリセットと、過去カウンタの記録(入れ替え)
            wTraffic[wIndex]['rat_count'] = wTraffic[wIndex][
                'now_count']  # 1時間前
            wTraffic[wIndex]['now_count'] = wTraffic[wIndex][
                'count']  # 現在(後ろで送信)
            wTraffic[wIndex]['now_days'] += wTraffic[wIndex]['count']  # 1日に加算
            wTraffic[wIndex]['count'] = 0

            ### 1日経った =twitterに送信する
            if gVal.STR_TimeInfo['OneDay'] == True:
                wTraffic[wIndex]['rat_wday'] = wTraffic[wIndex]['rat_days']
                wTraffic[wIndex]['now_wday'] = wTraffic[wIndex]['now_days']
                wTraffic[wIndex]['rat_days'] = wTraffic[wIndex]['now_days']
                wTraffic[wIndex]['now_days'] = 0

            ##DBを更新
            wQuery = "update TBL_TRAFFIC_DATA set " + \
              "count = " + str(wTraffic[wIndex]['count']) + ", " + \
              "rat_count = " + str(wTraffic[wIndex]['rat_count']) + ", " + \
              "now_count = " + str(wTraffic[wIndex]['now_count']) + ", " + \
              "rat_days = " + str(wTraffic[wIndex]['rat_days']) + ", " + \
              "now_days = " + str(wTraffic[wIndex]['now_days']) + " " + \
              "where domain = '" + wTraffic[wIndex]['domain'] + "' ;"

            wRes = wOBJ_DB.RunQuery(wQuery)
            wRes = wOBJ_DB.GetQueryStat()
            if wRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_Traffic: SendTraffic: Standby reset is failed: " +
                    wRes['Reason'] + " domain=" + wTraffic[wIndex]['domain'])
                wOBJ_DB.Close()
                return False

            ##インデックス更新
            wIndex += 1

        #############################
        # DB切断
        wOBJ_DB.Close()

        #############################
        # 送信するトラヒックの作成

        #############################
        # トラヒック送信対象の順番に入れ替える
        wARR_SendTraffic = {}
        wIndex = 0
        wKeyList = wTraffic.keys()
        for wSendDomain in self.ARR_SendDomain:
            ##対象のトラヒックを探す
            for wKey in wKeyList:
                if wTraffic[wKey]['domain'] != wSendDomain:
                    continue
                ##ヒット
                wARR_SendTraffic.update({wIndex: wTraffic[wKey]})
                wIndex += 1

        #############################
        # トゥートの組み立て
        wCHR_TimeDate = gVal.STR_TimeInfo['TimeDate'].split(" ")
        ##		wCHR_Title = "Mastodon Traffic: " + wCHR_TimeDate[0] + " " + str(gVal.STR_TimeInfo['Hour']) + "時台"
        wCHR_Title = "Mastodon Traffic: " + wCHR_TimeDate[0] + " " + str(
            gVal.STR_TimeInfo['Hour']) + "時"

        wCHR_Body = ""
        wKeyList = wARR_SendTraffic.keys()
        for wIndex in wKeyList:
            wCHR_Body = wCHR_Body + wARR_SendTraffic[wIndex][
                'domain'] + ": " + str(wARR_SendTraffic[wIndex]['now_count'])

            if wARR_SendTraffic[wIndex]['rat_count'] >= 0:
                ##bot起動の最初の1時間は差を出さない
                wRateCount = wARR_SendTraffic[wIndex][
                    'now_count'] - wARR_SendTraffic[wIndex]['rat_count']
                wCHR_Body = wCHR_Body + "("
                if wRateCount > 0:
                    wCHR_Body = wCHR_Body + "+"
                wCHR_Body = wCHR_Body + str(wRateCount) + ")" + '\n'
            else:
                wCHR_Body = wCHR_Body + '\n'

        wCHR_Toot = wCHR_Body + "#" + gVal.STR_MasterConfig['TrafficTag']

        #############################
        # トゥートの送信
        wRes = self.Obj_Parent.OBJ_MyDon.Toot(status=wCHR_Toot,
                                              spoiler_text=wCHR_Title,
                                              visibility=self.CHR_SendRange)
        if wRes['Result'] != True:
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_Traffic: SendTraffic: Mastodon error: " + wRes['Reason'])
            return False

        #############################
        # Twitterが有効か
        if gVal.STR_MasterConfig['Twitter'] != "on":
            return True  #トラヒックは送信済

        #############################
        # 1日経ってる周回か
        if gVal.STR_TimeInfo['OneDay'] == False:
            return True  #トラヒックは送信済

        #############################
        # ツイートの組み立て
        wCHR_TimeDate = gVal.STR_TimeInfo['TimeDate'].split(" ")
        ##		wCHR_Title = "Mastodon Traffic: " + wCHR_TimeDate[0] + " " + str(gVal.STR_TimeInfo['Hour']) + "時"
        wCHR_Title = "Mastodon Traffic: " + wCHR_TimeDate[0]

        wCHR_Body = ""
        wKeyList = wARR_SendTraffic.keys()
        for wIndex in wKeyList:
            wCHR_Body = wCHR_Body + wARR_SendTraffic[wIndex][
                'domain'] + ": " + str(wARR_SendTraffic[wIndex]['now_wday'])

            if wARR_SendTraffic[wIndex]['rat_wday'] >= 0:
                ##bot起動の最初の1日は差を出さない
                wRateCount = wARR_SendTraffic[wIndex][
                    'now_wday'] - wARR_SendTraffic[wIndex]['rat_wday']
                wCHR_Body = wCHR_Body + "("
                if wRateCount > 0:
                    wCHR_Body = wCHR_Body + "+"
                wCHR_Body = wCHR_Body + str(wRateCount) + ")" + '\n'
            else:
                wCHR_Body = wCHR_Body + '\n'

##		wCHR_Tweet = wCHR_Title + '\n' + wCHR_Body + "#" + gVal.STR_MasterConfig['TwitterTag']
##		if gVal.STR_MasterConfig['TwitterTag']!="" :
##			wCHR_Tweet = wCHR_Tweet + "#" + gVal.STR_MasterConfig['TwitterTag']

        wCHR_Tweet = wCHR_Title + '\n' + wCHR_Body
        wARR_Tweet = wCHR_Tweet.split('\n')  #チェック用の行にバラす

        #############################
        # タグを抜いた文字数を計算する
        wTwitterTag = ""
        if gVal.STR_MasterConfig['TwitterTag'] == "":
            wMAX_Tweet = 140
        else:
            wTwitterTag = '\n' + "#" + gVal.STR_MasterConfig['TwitterTag']
            wMAX_Tweet = 140 - len(wTwitterTag)

        #############################
        # 文字数を140字に収める
        #   最後尾のトラヒック情報(ドメイン別)を1つずつ削って収める
        for wI in list(range(len(wARR_Tweet))):
            ##			if len(wCHR_Tweet)<=140 :
            if len(wCHR_Tweet) <= wMAX_Tweet:
                break  #140字に収まっている

            ##最後尾を削って文字列に直す
            del wARR_Tweet[-1]
            wCHR_Tweet = '\n'.join(wARR_Tweet)

        #############################
        # 1番目のドメインを最後尾に移動(Twitter上で目立たせる)
        wARR_Tweet = wCHR_Tweet.split('\n')
        if len(wARR_Tweet) >= 2:
            wARR_Tweet.append(wARR_Tweet[1])
            del wARR_Tweet[1]
        wCHR_Tweet = '\n'.join(wARR_Tweet)
        wCHR_Tweet = wCHR_Tweet + wTwitterTag  #タグを付ける

        #############################
        # ツイートの送信
        if gVal.FLG_Test_Mode == False:
            wRes = self.Obj_Parent.OBJ_Twitter.Tweet(wCHR_Tweet)
            if wRes['Result'] != True:
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_Traffic: SendTraffic: Twitter API Error: " +
                    wRes['Reason'])
                ##失敗しても処理は継続

        else:
            wStr = "Twitter(TestMode): ツイート内容= " + '\n' + wCHR_Tweet
            self.Obj_Parent.OBJ_Mylog.Log('b', wStr, inView=True)

        return True  #トラヒックは送信済
Exemplo n.º 4
0
    def WordStudy(self, inCont):
        #############################
        # 単語なし
        if inCont == "":
            return False

        #############################
        # ' 文字が含まれていたら除外する
##		wRes = CLS_OSIF.sRe_Search( "'", inCont )
##		if wRes :
        if inCont.find("'") >= 0:
            return False

        #############################
        # ワード収集が有効か
        if gVal.STR_MasterConfig['WordStudy'] != "on":
            return False  #無効

        #############################
        # 今回の学習数が上限か
        if self.STR_Stat['StudyNum'] >= gVal.DEF_STR_TLNUM['studyNum']:
            return False  #今回は学習しない

        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_WordCorr: WordStudy: DB Connect test is failed: " +
                wRes['Reason'])
            wOBJ_DB.Close()
            return False

        wVAL_Rate_WordNum = -1
        #############################
        # 1日1回、古いレコードを削除する
        if gVal.STR_TimeInfo['OneDay'] == True:
            ###現在のレコード数
            wDBRes = wOBJ_DB.RunCount("TBL_WORD_CORRECT")
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: Run Query is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False
            wVAL_Rate_WordNum = wDBRes['Responce']

            ###指定日付の抽出
            wLag = gVal.DEF_STR_TLNUM['studyDay'] * 24 * 60 * 60
            ##			wLagTime = CLS_OSIF.sTimeLag( gVal.STR_TimeInfo['TimeDate'], inThreshold=wLag, inTimezone=-1 )
            wLagTime = CLS_OSIF.sTimeLag(inThreshold=wLag, inTimezone=-1)
            if wLagTime['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: sTimeLag is failed")
                wOBJ_DB.Close()
                return False

            ###単語テーブル
            wQuery = "delete from TBL_WORD_CORRECT where lupdate < " + \
              "timestamp '" + str(wLagTime['RateTime']) + "' " + \
              ";"
            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Old TBL_WORD_CORRECT delete): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            ###品詞パターン テーブル
            wQuery = "delete from TBL_CLAZ_LIST where lupdate < " + \
              "timestamp '" + str(wLagTime['RateTime']) + "' " + \
              ";"
            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Old TBL_CLAZ_LIST delete): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            ###削除後レコード数
            wDBRes = wOBJ_DB.RunCount("TBL_WORD_CORRECT")
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: Run Query is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False
            wVAL_WordNum = wDBRes['Responce']

            ###削除数
            wVAL_Delete_WordNum = wVAL_Rate_WordNum - wVAL_WordNum
            self.STR_Stat["Delete"] += wVAL_Delete_WordNum
            self.Obj_Parent.OBJ_Mylog.Log(
                'b', "古い単語・品詞パターン削除: 対象=" + str(wLagTime['RateTime']) + " 以前")

        #############################
        # レコード数の抽出
        if wVAL_Rate_WordNum == -1:
            ###削除で結果出してない場合に処理する
            wDBRes = wOBJ_DB.RunCount("TBL_WORD_CORRECT")
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: Run Query is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            wVAL_WordNum = wDBRes['Responce']

        #############################
        # 学習数が上限か
        if gVal.DEF_STR_TLNUM['studyMax'] <= wVAL_WordNum:
            if self.STR_Stat['WordLimit'] == False:
                self.Obj_Parent.OBJ_Mylog.Log(
                    'b', "学習不能(単語登録数上限: " +
                    str(gVal.DEF_STR_TLNUM['studyMax']) + "件)")
                self.STR_Stat['WordLimit'] = True
            wOBJ_DB.Close()
            return False  #上限

        #############################
        # デコーダで解読 (出力は辞書型)
        wGetWords = self.__analizeMeCab(inCont)
        if len(wGetWords) == 0:
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_WordCorr: WordStudy: MeCab analize result is zero, or failed"
            )
            wOBJ_DB.Close()
            return False  #失敗

        # ここまでで登録処理確定
        #############################
        self.STR_Stat['Cope'] += len(wGetWords)  #単語数を記録

        ##		#############################
        ##		# トゥートから時間を取得
        ##		wTime = CLS_OSIF.sGetTimeformat( inCreateAt )
        ##		if wTime['Result']==True :
        ##			wTime = wTime['TimeDate']
        ##		else:
        ##			wTime = None
        ##
        #############################
        # 解読した結果(単語)を判定しながら詰めていく
        wKeylist = wGetWords.keys()  #キーはIndex整数
        wClazList = ""  #文書パターン
        for wKey in wKeylist:
            #############################
            # 登録済みの単語か
            wQuery = "word = '" + wGetWords[wKey]['word'] + "'"
            wDBRes = wOBJ_DB.RunExist("TBL_WORD_CORRECT", wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Word check): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            if wDBRes['Responce'] == True:
                ###登録済み
                ###  触れたので時刻を更新
                wQuery = "update TBL_WORD_CORRECT set " + \
                  "lupdate = '"   + str(gVal.STR_TimeInfo['TimeDate']) + "' " + \
                  "where word = '" + wGetWords[wKey]['word'] + "' ;"

                wDBRes = wOBJ_DB.RunQuery(wQuery)
                wDBRes = wOBJ_DB.GetQueryStat()
                if wDBRes['Result'] != True:
                    ##失敗
                    self.Obj_Parent.OBJ_Mylog.Log(
                        'a',
                        "CLS_WordCorr: WordStudy: Run Query is failed (Word check, time update): "
                        + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                    wOBJ_DB.Close()
                    return False

                ###品詞パターンは記録する
                wClazList = wClazList + wGetWords[wKey]['claz'] + ","
                continue  #登録済なのでスキップ

            #############################
            # 除外する品詞か
            #### BOS/EOS
            if wGetWords[wKey]['claz'] == "BOS/EOS":
                continue

            #### 意識不明な単語、パターンとして使えない単語
            if (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['yomi'] == "*" and wGetWords[wKey]['cla1'] == "サ変接続" ) or \
               (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['cla1'] == "数" ) or \
               wGetWords[wKey]['claz'] == "記号" :
                continue

            #### 名詞かつ 3文字以内の半角英字
            if (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['cla1'] == "一般" ) or \
               (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['cla1'] == "固有名詞" and wGetWords[wKey]['cla2'] == "組織" ) :
                wRes = CLS_OSIF.sRe_Search(r'^[a-zA-Z]+$',
                                           wGetWords[wKey]['word'])
                if wRes:
                    if len(wGetWords[wKey]['word']) <= 3:
                        continue

            #### 禁止ワードを含むか
##			if self.CheckWordREM( wGetWords[wKey]['word'] )==False :
            if wGetWords[wKey]['word'] in gVal.STR_WordREM:
                continue  #禁止あり

            ###**ループ中に辞書の登録上限を超えても何もしない仕様(DBだしええかと)

            #############################
            # 単語の登録
            wQuery = "insert into TBL_WORD_CORRECT values (" + \
               "'" + wGetWords[wKey]['word'] + "'," + \
               "'" + wGetWords[wKey]['claz'] + "'," + \
               "'" + wGetWords[wKey]['yomi'] + "'," + \
               "'" + wGetWords[wKey]['cla1'] + "'," + \
               "'" + wGetWords[wKey]['cla2'] + "'," + \
               "'" + wGetWords[wKey]['cla3'] + "'," + \
               "'" + wGetWords[wKey]['ktyp'] + "'," + \
               "'" + wGetWords[wKey]['kkat'] + "'," + \
               "'" + str(gVal.STR_TimeInfo['TimeDate']) + "'," + \
               "False " + \
               ") ;"

            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Word regist): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            self.STR_Stat['Regist'] += 1

            ###品詞パターンは記録する
            wClazList = wClazList + wGetWords[wKey]['claz'] + ","

        #############################
        # 今回の学習回数更新
        self.STR_Stat['StudyNum'] += 1

        #############################
        # 品詞パターン学習
        if wClazList != "":
            wClazList = wClazList[0:len(wClazList) - 1]  #末尾の','を抜く

            ###同じ品詞パターンがあるか
            wQuery = "claz = '" + wClazList + "'"
            wDBRes = wOBJ_DB.RunExist("TBL_CLAZ_LIST", wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Claz check): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            if wDBRes['Responce'] == True:
                ###登録済みならここで終わる(正常)
                wOBJ_DB.Close()
                return True

            ###登録なしなら、登録する
            ###  **ここで品詞パターンの登録上限を超えても何もしない仕様(DBだしええかと)
            wQuery = "insert into TBL_CLAZ_LIST values (" + \
               "'" + wClazList + "'," + \
               "'" + str(gVal.STR_TimeInfo['TimeDate']) + "'," + \
               "False " + \
               ") ;"

            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Claz regist): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            self.STR_Stat['ClazList'] += 1

        #############################
        # 正常終了
        wOBJ_DB.Close()
        return True
Exemplo n.º 5
0
    def GetRandToot(self):
        wToot = ""
        #############################
        # ワード収集が有効か
        if gVal.STR_MasterConfig['WordStudy'] != "on":
            return wToot  #無効

        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_WordCorr: GetRandToot: DB Connect test is failed: " +
                wRes['Reason'])
            wOBJ_DB.Close()
            return wToot

        #############################
        # 品詞パターンを取得
        wQuery = "select claz from TBL_CLAZ_LIST order by random() limit 1" + \
           ";"
        wDBRes = wOBJ_DB.RunQuery(wQuery)
        wDBRes = wOBJ_DB.GetQueryStat()
        if wDBRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_WordCorr: GetRandToot: Run Query is failed (get TBL_CLAZ_LIST): "
                + wDBRes['Reason'] + " query=" + wDBRes['Query'])
            wOBJ_DB.Close()
            return wToot
        if len(wDBRes['Responce']['Data']) != 1:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_WordCorr: GetRandToot: Run Query is failed (get TBL_CLAZ_LIST is not 1): "
                + wDBRes['Reason'] + " query=" + wDBRes['Query'])
            wOBJ_DB.Close()
            return wToot
        wSelClazList = wDBRes['Responce']['Data'][0][0]
        wClazList = wSelClazList.split(",")

        #############################
        # 最適化用にパターンにある品詞をまとめこむ。
        # ついで収集用にまとめ領域を作る。
        wRandDic = {}
        wClazDic = {}
        wIndex = 0
        for wClaz in wClazList:
            wKeyList = list(wClazDic.keys())
            if wClaz not in wKeyList:
                wClazDic.update({wClaz: 1})
            else:
                wClazDic[wClaz] += 1

            wRandDic.update({wIndex: ""})
            wRandDic[wIndex] = {}
            wRandDic[wIndex].update({"claz": wClaz})
            wRandDic[wIndex].update({"word": ""})
            wIndex += 1

        #############################
        # 品詞ごとにワードを収集する
        wKeyList = list(wClazDic.keys())
        for wKey in wKeyList:
            wQuery = "select word from TBL_WORD_CORRECT where " + \
               "claz = '" + wKey + "' order by random() limit " + str(wClazDic[wKey]) + \
               ";"
            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: GetRandToot: Run Query is failed (get TBL_WORD_CORRECT is random select): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return wToot

            for wWord in wDBRes['Responce']['Data']:
                wRandList = wRandDic.keys()
                wFLG_Hit = False
                for wRandKey in wRandList:
                    if wRandDic[wRandKey]['claz'] == wKey:
                        wFLG_Hit = True
                        break

                if wFLG_Hit == True:
                    wRandDic[wRandKey]['word'] = wWord[0]

        #############################
        # 使った品詞パターンを削除する
        wQuery = "delete from TBL_CLAZ_LIST where " + \
           "claz = '" + wSelClazList + "'" + \
           ";"
        wDBRes = wOBJ_DB.RunQuery(wQuery)
        wDBRes = wOBJ_DB.GetQueryStat()
        if wDBRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_WordCorr: GetRandToot: Run Query is failed (delete TBL_CLAZ_LIST): "
                + wDBRes['Reason'] + " query=" + wDBRes['Query'])
            wOBJ_DB.Close()
            return wToot

        #############################
        # DB切断
        wOBJ_DB.Close()

        #############################
        # 文字列に組み立てる
        wToot = ""
        wRandList = wRandDic.keys()
        for wRandKey in wRandList:
            wToot = wToot + wRandDic[wRandKey]['word']

        return wToot  #成功
Exemplo n.º 6
0
    def Get_DBctrl_Twitter(self):
        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_TwitterReader: Get_DBctrl_Twitter: DB Connect test is failed: "
                + wRes['Reason'])
            wOBJ_DB.Close()
            return

        #############################
        # 取り出し
        wQuery = "select * from TBL_TWITTER_READER ;"

        wRes = wOBJ_DB.RunQuery(wQuery)
        wRes = wOBJ_DB.GetQueryStat()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_TwitterReader: Get_DBctrl_Twitter: Run Query is failed: "
                + wRes['Reason'] + " query=" + wRes['Query'])
            wOBJ_DB.Close()
            return

        if len(wRes['Responce']['Data']) == 0:
            ##トレンド情報なし
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_TwitterReader: Get_DBctrl_Twitter: TBL_TWITTER_READER record is 0"
            )
            wOBJ_DB.Close()
            return

        #############################
        # 一回ローカルに読み出す
        wARR_Twitter = {}
        wIndex = 0
        for wLineTap in wRes['Responce']['Data']:
            ##領域の準備
            wARR_Twitter.update({wIndex: ""})
            wARR_Twitter[wIndex] = {}
            wARR_Twitter[wIndex].update({"id": 0})
            wARR_Twitter[wIndex].update({"text": ""})
            wARR_Twitter[wIndex].update({"screen_name": ""})
            wARR_Twitter[wIndex].update({"send_user": ""})
            wARR_Twitter[wIndex].update({"tags": ""})
            wARR_Twitter[wIndex].update({"lupdate": ""})
            wARR_Twitter[wIndex].update({"sended": False})

            ##取り出し
            wGetTap = []
            for wCel in wLineTap:
                wGetTap.append(wCel)
                ## [0] ..id
                ## [1] ..text
                ## [2] ..screen_name
                ## [3] ..send_user
                ## [4] ..tags
                ## [5] ..lupdate
                ## [6] ..sended

            ##領域へロード
            wARR_Twitter[wIndex]['id'] = int(wGetTap[0])
            wARR_Twitter[wIndex]['text'] = wGetTap[1].strip()
            wARR_Twitter[wIndex]['screen_name'] = wGetTap[2].strip()
            wARR_Twitter[wIndex]['send_user'] = wGetTap[3].strip()
            wARR_Twitter[wIndex]['lupdate'] = wGetTap[5]
            wARR_Twitter[wIndex]['sended'] = wGetTap[6]

            wGetTap[4] = wGetTap[4].strip()
            if wGetTap[4] == "(none)":
                wGetTap[4] = ""  # null設定
            wARR_Twitter[wIndex]['tags'] = wGetTap[4]

            wIndex += 1

        #############################
        # DB切断
        wOBJ_DB.Close()

        #############################
        # 未送信のものを読みだす
        self.ARR_Twitter = {}

        wKeylist = wARR_Twitter.keys()
        wIndex = 0
        for wKey in wKeylist:
            if wARR_Twitter[wKey]['sended'] == True:
                continue  #送信済
            if wARR_Twitter[wKey]['send_user'] != self.Obj_Parent.CHR_Account:
                continue  #送信者ではない

            #############################
            # 格納
            self.ARR_Twitter.update({wIndex: ""})
            self.ARR_Twitter[wIndex] = {}
            self.ARR_Twitter[wIndex].update({"id": wARR_Twitter[wKey]['id']})
            self.ARR_Twitter[wIndex].update(
                {"text": wARR_Twitter[wKey]['text']})
            self.ARR_Twitter[wIndex].update(
                {"screen_name": wARR_Twitter[wKey]['screen_name']})
            self.ARR_Twitter[wIndex].update(
                {"send_user": wARR_Twitter[wKey]['send_user']})
            self.ARR_Twitter[wIndex].update(
                {"tags": wARR_Twitter[wKey]['tags']})
            self.ARR_Twitter[wIndex].update(
                {"lupdate": wARR_Twitter[wKey]['lupdate']})
            self.ARR_Twitter[wIndex].update({"sended": False})
            wIndex += 1

        return
Exemplo n.º 7
0
    def Set_DBctrl_Twitter(self):
        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_TwitterReader: Set_DBctrl_Twitter: DB Connect test is failed: "
                + wRes['Reason'])
            wOBJ_DB.Close()
            return

        wKeyList = self.ARR_Twitter.keys()
        #############################
        # ダブりがないかチェック
        # ・なければ追加
        # ・あれば更新
        for wKey in wKeyList:
            wQuery = "select * from TBL_TWITTER_READER where id = '" + self.ARR_Twitter[
                wKey]['id'] + "' ;"
            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_TwitterReader: Set_DBctrl_Twitter: Run Query is failed: "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            wFLG_Update = False
            wText = str(self.ARR_Twitter[wKey]['text']).replace("'", "''")
            #############################
            # クエリの作成
            if self.ARR_Twitter[wKey]['tags'] == "":
                self.ARR_Twitter[wKey]['tags'] = "(none)"  # SQL用補完

            ###なければ追加
            if len(wDBRes['Responce']['Data']) == 0:
                wQuery = "insert into TBL_TWITTER_READER values (" + \
                   str( self.ARR_Twitter[wKey]['id'] ) + "," + \
                   "'" + wText + "'," + \
                   "'" + self.ARR_Twitter[wKey]['screen_name'] + "'," + \
                   "'" + self.ARR_Twitter[wKey]['send_user'] + "'," + \
                   "'" + self.ARR_Twitter[wKey]['tags'] + "'," + \
                   "'" + self.ARR_Twitter[wKey]['lupdate'] + "'," + \
                   str( self.ARR_Twitter[wKey]['sended'] ) + " " + \
                   ") ;"

            ###あれば更新
            else:
                wChgList = []
                if wOBJ_DB.ChgList(wDBRes['Responce']['Data'],
                                   outList=wChgList) != True:
                    ##ないケースかも
                    self.Obj_Parent.OBJ_Mylog.Log(
                        'a', "CLS_UserCorr: AddUser: Select data is Zero")
                    wOBJ_DB.Close()
                    return False
                wChgList = wChgList[0]  #1行しかないし切る

                wQuery = "update TBL_TWITTER_READER set " + \
                  "text = '" + wText + "', " + \
                  "screen_name = '" + str(self.ARR_Twitter[wKey]['screen_name']) + "', " + \
                  "send_user = '******'send_user']) + "', " + \
                  "tags = '"    + str(self.ARR_Twitter[wKey]['tags']) + "', " + \
                  "lupdate = '" + str(self.ARR_Twitter[wKey]['lupdate']) + "', " + \
                  "sended = "   + str(self.ARR_Twitter[wKey]['sended']) + " " + \
                  "where id = '" + wChgList[0] + "' ;"

                wFLG_Update = True

            #############################
            # クエリ実行
            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_UserCorr: AddUser: DB insert is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            #############################
            # カウント
            if wFLG_Update == True:
                self.STR_Cope['Update'] += 1
            else:
                self.STR_Cope['Insert'] += 1

        #############################
        # 正常終了
        wOBJ_DB.Close()
        return