def Delete_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: Delete_DBctrl_Twitter: DB Connect test is failed: " + wRes['Reason']) wOBJ_DB.Close() return ############################# # 削除対象レコード数 wQuery = "select * from TBL_TWITTER_READER where sended = True ;" wDBRes = wOBJ_DB.RunQuery(wQuery) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result'] != True: ##失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Delete_DBctrl_Twitter: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query']) wOBJ_DB.Close() return False wDeleteNum = len(wDBRes['Responce']['Data']) ############################# # 削除 wQuery = "delete from TBL_TWITTER_READER where " + \ "sended = True " + \ ";" ############################# # クエリ実行 wDBRes = wOBJ_DB.RunQuery(wQuery) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result'] != True: ##失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Delete_DBctrl_Twitter: DB insert is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query']) wOBJ_DB.Close() return self.STR_Cope['Delete'] += wDeleteNum ############################# # DB切断 wOBJ_DB.Close() return
def __oneRunQueryMstdn(self, inQuery): ############################# # DB接続 wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['MstdnInfo_File']) wRes = wOBJ_DB.GetIniStatus() if wRes['Result'] != True: ##失敗 wStr = "CLS_DBedit: __oneRunQueryMstdn: DB Connect test is failed: " + wRes[ 'Reason'] CLS_OSIF.sPrn(wStr) wOBJ_DB.Close() return wRes ############################# # クエリ実行 wRes = wOBJ_DB.RunQuery(inQuery) ## "Result" : False ## "Reason" : None ## "Responce" : None ############################# # DB切断 wOBJ_DB.Close() wRes = wOBJ_DB.GetQueryStat() return wRes
def __recovery_TBL_TRAFFIC_DATA(self): ############################# # 読み出し先初期化 wTrafficUser = [] ############################# # ファイル読み込み wFile_path = gVal.DEF_STR_FILE['TrafficFile'] if CLS_File.sReadFile(wFile_path, outLine=wTrafficUser) != True: wStr = "CLS_Regist : __recovery_TBL_TRAFFIC_DATA: TrafficFile read is failed: " + gVal.DEF_STR_FILE[ 'TrafficFile'] CLS_OSIF.sPrn(wStr) return False ############################# # DBの接続 wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File']) wRes = wOBJ_DB.GetIniStatus() if wRes['Result'] != True: ###失敗 wStr = "CLS_Regist : __recovery_TBL_TRAFFIC_DATA: DB connect error: " + wRes[ 'Reason'] CLS_OSIF.sPrn(wStr) return False ############################# # 修復 for wLine in wTrafficUser: wDomain = wLine.split("@") if len(wDomain) != 2: wStr = "CLS_Regist : __recovery_TBL_TRAFFIC_DATA: Traffic user is invalid: user="******"insert into TBL_TRAFFIC_DATA values (" + \ "'" + wDomain + "'," + \ "0," + \ "-1," + \ "-1," + \ "-1," + \ "0" + \ ") ;" wDBRes = wOBJ_DB.RunQuery(wQuery) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result'] != True: ##失敗 wStr = "CLS_Regist : __recovery_TBL_TRAFFIC_DATA: DB insert is failed: " + wDBRes[ 'Reason'] CLS_OSIF.sPrn(wStr) wOBJ_DB.Close() return False ############################# # DBのクローズ wOBJ_DB.Close() return True
def GetOldUser(self, inARR_Follow): wARR_Users = [] ############################# # 指定日付の抽出 wLag = gVal.DEF_STR_TLNUM['AutoRemoveDays'] * 24 * 60 * 60 wLagTime = CLS_OSIF.sTimeLag(inThreshold=wLag, inTimezone=-1) if wLagTime['Result'] != True: ##失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_UserCorr: GetOldUser: sTimeLag is failed") return wARR_Users ############################# # 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_UserCorr: GetOldUser: DB Connect test is failed: " + wRes['Reason']) wOBJ_DB.Close() return wARR_Users ############################# # 古いユーザ取得 wQuery = "select id from TBL_USER_DATA 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_UserCorr: GetOldUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query']) wOBJ_DB.Close() return wARR_Users ############################# # 取得あればローカルに詰める wARR_OldUsers = [] wOBJ_DB.ChgList(wDBRes['Responce']['Data'], outList=wARR_OldUsers) ############################# # DB切断 wOBJ_DB.Close() ############################# # フォロー者のなかからリムーブ対象のユーザを出力 for wUser in inARR_Follow: if wUser in wARR_OldUsers: wARR_Users.append(wUser) return wARR_Users
def Sended_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: Sended_DBctrl_Twitter: DB Connect test is failed: " + wRes['Reason']) wOBJ_DB.Close() return ############################# # 更新 wQuery = "update TBL_TWITTER_READER set " + \ "sended = True " + \ "where " wKeylist = self.ARR_Twitter.keys() wLen = len(wKeylist) wIndex = 0 for wKey in wKeylist: if self.ARR_Twitter[wKey]['sended'] == True: wQuery = wQuery + "id = '" + str( self.ARR_Twitter[wKey]['id']) + "'" self.STR_Cope['Update'] += 1 wIndex += 1 if wLen > wIndex: wQuery = wQuery + " or " wQuery = wQuery + " ;" ############################# # クエリ実行 wDBRes = wOBJ_DB.RunQuery(wQuery) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result'] != True: ##失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Sended_DBctrl_Twitter: DB insert is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query']) wOBJ_DB.Close() return ############################# # DB切断 wOBJ_DB.Close() return
def __delTrafficUser( self, inUsername ): ############################# # 応答データ wRes = { "Result" : False, "Reason" : "", "Update" : False } ############################# # 名前の妥当性チェック wResUser = CLS_UserData.sUserCheck( inUsername ) ## "Result" : False, ## "User" : "", ## "Domain" : "", ## "Reason" : "", ## "Registed" : False, ## if wResUser['Result']!=True or wResUser['Registed']==False : if wResUser['Result']!=True : wRes['Reason'] = "CLS_Regist : __delTrafficUser: Username is not valid: " + inUsername return wRes #不正 ############################# # 読み出し先初期化 wTrafficUser = [] ############################# # ファイル読み込み wFile_path = gVal.DEF_STR_FILE['TrafficFile'] if CLS_File.sReadFile( wFile_path, outLine=wTrafficUser )!=True : wRes['Reason'] = "CLS_Regist : __delTrafficUser: TrafficFile read is failed: " + gVal.DEF_STR_FILE['TrafficFile'] return wRes #失敗 ############################# # トラヒック対象ではなら削除しない if inUsername not in wTrafficUser : wRes['Result'] = True return wRes #削除しないで終了 ###対象なので削除する wTrafficUser.remove( inUsername ) ############################# # ユーザ一覧取得 wUserList = CLS_UserData.sGetUserList() ############################# # 削除ユーザと同一の既登録ドメインがあるか wUsername = None for wLine in wUserList : wDomain = wLine.split("@") if wDomain[1]==wResUser['Domain'] : ## 既登録で既にドメインがあった wUsername = wLine break if wUsername!=None : ##同一ドメインの別ユーザに切り替える wTrafficUser.append( wUsername ) else : ##同一ドメインの別ユーザがなければDBから削除する ############################# # DB接続 wOBJ_DB = CLS_PostgreSQL_Use( gVal.DEF_STR_FILE['DBinfo_File'] ) wDBRes = wOBJ_DB.GetIniStatus() if wDBRes['Result']!=True : ##失敗 wRes['Reason'] = "CLS_Regist : __delTrafficUser: DB Connect test is failed: " + wDBRes['Reason'] ## wOBJ_DB.Close() return wRes ############################# # ドメイン存在チェック wQuery = "domain = '" + wResUser['Domain'] + "'" wDBRes = wOBJ_DB.RunExist( inObjTable="TBL_TRAFFIC_DATA", inWhere=wQuery ) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result']!=True : ##失敗 ## wRes['Reason'] = "CLS_Regist : __delTrafficUser: Run Query is failed: " + wDBRes['Reason'] wRes['Reason'] = "CLS_Regist : __delTrafficUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query'] wOBJ_DB.Close() return wRes ############################# # 存在していれば削除 if wDBRes['Responce']==True : wQuery = "delete from TBL_TRAFFIC_DATA where domain = '" + wResUser['Domain'] + \ "' ;" wDBRes = wOBJ_DB.RunQuery( wQuery ) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result']!=True : ##失敗 wRes['Reason'] = "CLS_Regist : __delTrafficUser: DB insert is failed: " + wDBRes['Reason'] wOBJ_DB.Close() return wRes ############################# # DB切断 wRes['Update'] = True #DBからは削除 wOBJ_DB.Close() ############################# # トラヒックを更新 if self.__setTrafficUser( wTrafficUser )!=True : wRes['Reason'] = "CLS_Regist : __delTrafficUser: TrafficFile write is failed: " + gVal.DEF_STR_FILE['TrafficFile'] return wRes ############################# # 正常 wRes['Result'] = True return wRes
def __regTrafficUser( self, inUsername ): ############################# # 応答データ wRes = { "Result" : False, "Reason" : "", "Update" : False } ############################# # 名前の妥当性チェック wResUser = CLS_UserData.sUserCheck( inUsername ) ## "Result" : False, ## "User" : "", ## "Domain" : "", ## "Reason" : "", ## "Registed" : False, if wResUser['Result']!=True or wResUser['Registed']==False : wRes['Reason'] = "CLS_Regist : __regTrafficUser: Username is not valid: " + inUsername return wRes #不正 ############################# # 読み出し先初期化 wTrafficUser = [] ############################# # ファイル読み込み wFile_path = gVal.DEF_STR_FILE['TrafficFile'] if CLS_File.sReadFile( wFile_path, outLine=wTrafficUser )!=True : wRes['Reason'] = "CLS_Regist : __regTrafficUser: TrafficFile read is failed: " + gVal.DEF_STR_FILE['TrafficFile'] return wRes #失敗 ############################# # 既登録のドメインがあるか wFlg = False for wLine in wTrafficUser : wDomain = wLine.split("@") if wDomain[1]==wResUser['Domain'] : ## 既登録で既にドメインがあった if wLine==gVal.STR_MasterConfig['MasterUser'] : ##MasterUserの場合差し替え wTrafficUser.remove( gVal.STR_MasterConfig['MasterUser'] ) else : ##登録あり wFlg = True break if wFlg==True : wRes['Result'] = True return wRes #更新しないで終了 ############################# # DBに登録 ############################# # DB接続 wOBJ_DB = CLS_PostgreSQL_Use( gVal.DEF_STR_FILE['DBinfo_File'] ) wDBRes = wOBJ_DB.GetIniStatus() if wDBRes['Result']!=True : ##失敗 wRes['Reason'] = "CLS_Regist : __regTrafficUser: DB Connect test is failed: " + wDBRes['Reason'] ## wOBJ_DB.Close() return wRes ############################# # ドメイン存在チェック wQuery = "domain = '" + wResUser['Domain'] + "'" wDBRes = wOBJ_DB.RunExist( inObjTable="TBL_TRAFFIC_DATA", inWhere=wQuery ) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result']!=True : ##失敗 ## wRes['Reason'] = "CLS_Regist : __regTrafficUser: Run Query is failed: " + wDBRes['Reason'] wRes['Reason'] = "CLS_Regist : __regTrafficUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query'] wOBJ_DB.Close() return wRes ############################# # 存在しなければ追加 if wDBRes['Responce']==False : wQuery = "insert into TBL_TRAFFIC_DATA values (" + \ "'" + wResUser['Domain'] + "'," + \ "0," + \ "-1," + \ "-1" + \ ") ;" wDBRes = wOBJ_DB.RunQuery( wQuery ) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result']!=True : ##失敗 wRes['Reason'] = "CLS_Regist : __regTrafficUser: DB insert is failed: " + wDBRes['Reason'] wOBJ_DB.Close() return wRes ############################# # DB切断 wOBJ_DB.Close() ############################# # トラヒックに登録 wTrafficUser.append( inUsername ) if self.__setTrafficUser( wTrafficUser )!=True : wRes['Reason'] = "CLS_Regist : __regTrafficUser: TrafficFile write is failed: " + gVal.DEF_STR_FILE['TrafficFile'] return wRes ############################# # 正常 wRes['Update'] = True wRes['Result'] = True return wRes
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だしええかと) ############################# # 単語の登録 wWord = str(wGetWords[wKey]['word']).replace("'", "''") wClaz = str(wGetWords[wKey]['claz']).replace("'", "''") wYomi = str(wGetWords[wKey]['yomi']).replace("'", "''") wQuery = "insert into TBL_WORD_CORRECT values (" + \ "'" + wWord + "'," + \ "'" + wClaz + "'," + \ "'" + wYomi + "'," + \ "'" + 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
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 #成功
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
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
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 #成功
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 #トラヒックは送信済
def AddUser(self, inROW, inUser): ## wAccount = inROW['account'] ## ############################# ## # ユーザ名の変換 ## wFulluser = CLS_UserData.sGetFulluser( wAccount['username'], wAccount['url'] ) ## if wFulluser['Result']!=True : ## ###今のところ通らないルート ## self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_UserCorr: AddUser: sGetFulluser failed: " + wFulluser['Reason'] ) ## return False #スルー ############################# # 除外トゥート ## ###自分 ## if wFulluser['Fulluser'] == self.Obj_Parent.CHR_Account : ## return False #スルーする ## ## ###外人 (日本人限定=ON時) ## if inROW['language']!="ja" and gVal.STR_MasterConfig["JPonly"]=="on" : ## self.Obj_Parent.OBJ_Mylog.Log( 'c', "CLS_UserCorr: AddUser: 日本人以外を検出(登録除外): " + wFulluser['Fulluser'] ) ## return False #スルーする ## ## ###除外ドメイン ## if wFulluser['Domain'] in gVal.STR_DomainREM : ## self.Obj_Parent.OBJ_Mylog.Log( 'c', "CLS_UserCorr: AddUser: 除外ドメインを検出(登録除外): " + wFulluser['Fulluser'] ) ## return False #スルーする ## ###今回関わったユーザ if inUser['Fulluser'] in self.ARR_UpdateUser: return False #スルーする self.ARR_UpdateUser.append(inUser['Fulluser']) # ここまでで登録処理確定 ############################# self.STR_Stat['Cope'] += 1 ############################# # postgreSQL対策で ' 文字を消す wResReplace = CLS_OSIF.sRe_Replace( "'", str(inROW['account']['display_name']), "''") ### "Result" : False, ### "Match" : False, ### "After" : None if wResReplace['Result'] != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_UserCorr: AddUser: sRe_Replace is failed: display_name=" + str(inROW['account']['display_name'])) return False if wResReplace['Match'] == True: wDisplay_Name = wResReplace['After'] else: wDisplay_Name = str(inROW['account']['display_name']) ############################# # セット用領域の作成 wUserData = {} wUserData.update({"id": inUser['Fulluser']}) ### wUserData.update({ "username" : str( inROW['account']['display_name'] ) }) wUserData.update({"username": wDisplay_Name}) wUserData.update({"domain": inUser['Domain']}) wUserData.update({"status": "-"}) wUserData.update({"followed": False}) wUserData.update({"locked": False}) wUserData.update({"lupdate": ""}) ## status ## @ フォローチェック予約(まだ未フォロー) ## - フォローチェック済 ## D ドメインブロックユーザ ## R リムーブ予約 ## X チェックもしくはフォロー失敗 ## * リストから消す ############################# # ステータスの設定 ###鍵の有無 ## if wAccount['locked']==True : if inROW['account']['locked'] == True: wUserData['locked'] = True ###更新時間 (mastodon時間) wTime = CLS_OSIF.sGetTimeformat(inROW['created_at']) if wTime['Result'] == True: wUserData['lupdate'] = wTime['TimeDate'] ############################# # 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_UserCorr: AddUser: DB Connect test is failed: " + wRes['Reason']) wOBJ_DB.Close() return False ## ############################# ## # ユーザ存在チェック ## wQuery = "id = '" + wUserData['id'] + "'" ## wDBRes = wOBJ_DB.RunExist( inObjTable="TBL_USER_DATA", inWhere=wQuery ) ## wDBRes = wOBJ_DB.GetQueryStat() ## if wDBRes['Result']!=True : ## ##失敗 ## self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_UserCorr: AddUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query'] ) ## wOBJ_DB.Close() ## return False ## ############################# # ユーザ取得 wQuery = "select * from TBL_USER_DATA where id = '" + wUserData[ 'id'] + "' ;" wDBRes = wOBJ_DB.RunQuery(wQuery) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result'] != True: ##失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_UserCorr: AddUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query']) wOBJ_DB.Close() return False wFLG_AddUser = False ############################# # クエリの作成 ###なければ追加 ## if wDBRes['Responce']==False : if len(wDBRes['Responce']['Data']) == 0: ###ステータスがなければ、チェック候補にする if wUserData['status'] == "-": wUserData['status'] = "@" wQuery = "insert into TBL_USER_DATA values (" + \ "'" + wUserData['id'] + "'," + \ "'" + wUserData['username'] + "'," + \ "'" + wUserData['domain'] + "'," + \ "'" + wUserData['status'] + "'," + \ str(wUserData['followed']) + "," + \ str(wUserData['locked']) + "," + \ "'" + str(wUserData['lupdate']) + "' " + \ ") ;" wFLG_AddUser = True ###あれば更新 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行しかないし切る ###領域に詰め直す ## wUserData['id'] = wChgList[0] ## wUserData['username'] = wChgList[1] ## wUserData['domain'] = wChgList[2] ### wUserData['status'] = wChgList[3] ### wUserData['followed'] = wChgList[4] wUserData['status'] = wChgList[0][3] wUserData['followed'] = wChgList[0][4] ## wUserData['locked'] = wChgList[5] ## wUserData['lupdate'] = wChgList[6] wQuery = "update TBL_USER_DATA set " + \ "status = '" + str(wUserData['status']) + "', " + \ "locked = '" + str(wUserData['locked']) + "', " + \ "lupdate = '" + str(wUserData['lupdate']) + "' " + \ "where id = '" + wUserData['id'] + "' ;" ############################# # クエリ実行 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 ############################# # DB切断 wOBJ_DB.Close() ############################# # 結果を記録 if wFLG_AddUser == True: ## self.Obj_Parent.OBJ_Mylog.Log( 'c', "CLS_UserCorr: AddUser: ユーザ追加: " + inUser['Fulluser'] ) self.STR_Stat['UserAdd'] += 1 else: ## self.Obj_Parent.OBJ_Mylog.Log( 'c', "CLS_UserCorr: AddUser: ユーザ更新: " + inUser['Fulluser'] ) self.STR_Stat['UserUpdate'] += 1 return True
def Send_Trend(self): ############################# # トレンド取得対象ドメインがなければ未処理 if len(self.ARR_SendDomain) == 0: self.Obj_Parent.OBJ_Mylog.Log('c', "トレンド対象ドメインなし") 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: Send_Trend: DB Connect test is failed: " + wRes['Reason']) wOBJ_DB.Close() return ############################# # トレンド情報のロード wQuery = "select * from TBL_TREND ;" wRes = wOBJ_DB.RunQuery(wQuery) wRes = wOBJ_DB.GetQueryStat() if wRes['Result'] != True: ##失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_Trend: Send_Trend: 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_Trend: Send_Trend: TBL_TREND record is 0") wOBJ_DB.Close() return ############################# # 一回ローカルに読み出す wARR_Trend = {} wIndex = 0 for wLineTap in wRes['Responce']['Data']: ##領域の準備 wARR_Trend.update({wIndex: ""}) wARR_Trend[wIndex] = {} wARR_Trend[wIndex].update({"rank": 0}) wARR_Trend[wIndex].update({"name": ""}) wARR_Trend[wIndex].update({"domain": ""}) wARR_Trend[wIndex].update({"uses": 0}) wARR_Trend[wIndex].update({"accs": 0}) wARR_Trend[wIndex].update({"lupdate": "None"}) ##トレンド情報の取り出し wGetTap = [] for wCel in wLineTap: wGetTap.append(wCel) ## [0] ..name ## [1] ..domain ## [2] ..uses ## [3] ..accs ##領域へロード wARR_Trend[wIndex].update({"rank": int(wGetTap[0])}) wARR_Trend[wIndex].update({"name": wGetTap[1].strip()}) wARR_Trend[wIndex].update({"domain": wGetTap[2].strip()}) wARR_Trend[wIndex].update({"uses": int(wGetTap[3])}) wARR_Trend[wIndex].update({"accs": int(wGetTap[4])}) wARR_Trend[wIndex].update({"lupdate": wGetTap[5]}) wIndex += 1 ############################# # DB切断 wOBJ_DB.Close() ############################# # トレンド取得対象ドメインごとに処理する for wDomain in self.ARR_SendDomain: wARR_MatomeKeys = [] ############################# # 現ドメインのインデックスをまとめる(まとめキー) wKeyList = wARR_Trend.keys() for wKey in wKeyList: if wARR_Trend[wKey]['domain'] == wDomain: wARR_MatomeKeys.append(wKey) if len(wARR_MatomeKeys) == 0: self.Obj_Parent.OBJ_Mylog.Log('c', "トレンド情報なし: " + wDomain) continue #トレンドがないドメイン ############################# # ランクの昇順にソートする wTrendLen = len(wARR_MatomeKeys) #ドメインのトレンド数 wSetKeylist = [] wKey = 0 wB_Index = -1 wRank = 1 while True: wKey = -1 for wChooseKey in wARR_MatomeKeys: if wChooseKey in wSetKeylist: continue #既に決定してる要素 wKey = wChooseKey break if wKey == -1: break #全て決定済 wB_Index = wKey if wARR_Trend[wKey]['rank'] != wRank: ###ランクがずれてる場合、トレンド値で並べる for wKey2 in wARR_MatomeKeys: if wKey == wKey2: continue #自分は除外 if wKey2 in wSetKeylist: continue #既に決定してる要素 if wARR_Trend[wKey]['uses'] < wARR_Trend[wKey2]['uses']: wB_Index = wKey2 #今より大きい要素が見つかった wRank += 1 wSetKeylist.append(wB_Index) #ランク順か、トレンド値の一番大きい要素を記録 ############################# # トゥートの組み立て wCHR_TimeDate = gVal.STR_TimeInfo['TimeDate'].split(" ") wCHR_Title = "Mastodon " + wDomain + " TrendTag: " + wCHR_TimeDate[ 0] + " " + str(gVal.STR_TimeInfo['Hour']) + "時" wCHR_Body = "" wVAL_Send = 0 for wKey in wSetKeylist: ## if wARR_Trend[wKey]['domain']!=wDomain : ## continue #違うドメインのトレンド ## ############################# # ハッシュタグTLとして取得できるか wResHash = self.Obj_Parent.OBJ_MyDon.GetHashtagTL( hashtag=wARR_Trend[wKey]['name'], limit=1) if wResHash['Result'] != True: ### APIの失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_Trend: __getTrends: GetHashtagTL is failed: " + wARR_Trend[wKey]['name'] + " reason=" + wResHash['Reason']) continue if len(wResHash['Responce']) == 0: ### TLがない self.Obj_Parent.OBJ_Mylog.Log( 'c', "Hashtag TLなし: " + wARR_Trend[wKey]['name']) self.STR_Cope['No_HashtagTL'] += 1 continue ###とりあえずソートなしで wCHR_Body = wCHR_Body + "#" + wARR_Trend[wKey]['name'] + '\n' wCHR_Body = wCHR_Body + "(" + str( wARR_Trend[wKey]['uses']) + ") [" + str( wARR_Trend[wKey]['lupdate']) + "]" + '\n' + '\n' wVAL_Send += 1 ### wCHR_Toot = wCHR_Body + "***上から最新順" ### ハッシュタグTLとして検索できるものがなかった if wVAL_Send == 0: self.Obj_Parent.OBJ_Mylog.Log('c', "トレンド送信なし: 有効なハッシュタグTLがない") return wCHR_Toot = wCHR_Body + "**上から最新順" + '\n' + "**() 2日間のタグ使用数" + '\n' + "**[] 最終使用日時" ############################# # トゥートの送信 wRes = self.Obj_Parent.OBJ_MyDon.Toot( status=wCHR_Toot, spoiler_text=wCHR_Title, visibility=self.DEF_SENDRANGE) if wRes['Result'] != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_Trend: Send_Trend: Mastodon error: " + wRes['Reason']) return ############################# # カウント ## self.STR_Cope['Now_Send'] += len(wSetKeylist) self.STR_Cope['Now_Send'] += wVAL_Send return
def IsActiveUser(self, inFulluser): wARR_Users = [] ############################# # 指定日付の抽出 wLag = gVal.DEF_STR_TLNUM['AutoRemoveDays'] * 24 * 60 * 60 wLagTime = CLS_OSIF.sTimeLag(inThreshold=wLag, inTimezone=-1) if wLagTime['Result'] != True: ##失敗 self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_UserCorr: IsActiveUser: sTimeLag is failed") 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_UserCorr: IsActiveUser: DB Connect test is failed: " + wRes['Reason']) wOBJ_DB.Close() return False ############################# # 一定期間活動があるユーザか ## wQuery = "id = '" + inFulluser + "' and " + \ ## "lupdate >= timestamp '" + str(wLagTime['RateTime']) + "'" ## ## wDBRes = wOBJ_DB.RunExist( inObjTable="TBL_USER_DATA", inWhere=wQuery ) ## wDBRes = wOBJ_DB.GetQueryStat() ## if wDBRes['Result']!=True : ## ##失敗 ## wRes['Reason'] = "CLS_UserCorr: IsActiveUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query'] ## wOBJ_DB.Close() ## return False ## ## ### 収集していないか、期間活動外 ## if wDBRes['Responce']==False : ## wOBJ_DB.Close() ## return False ## wQuery = "select locked from TBL_USER_DATA where " + \ "id = '" + inFulluser + "' and " + \ "lupdate >= timestamp '" + str(wLagTime['RateTime']) + "' " + \ ";" wDBRes = wOBJ_DB.RunQuery(wQuery) wDBRes = wOBJ_DB.GetQueryStat() if wDBRes['Result'] != True: ##失敗 wRes[ 'Reason'] = "CLS_UserCorr: IsActiveUser: Run Query is failed: " + wDBRes[ 'Reason'] + " query=" + wDBRes['Query'] wOBJ_DB.Close() return False ### 収集していないか、期間活動外 if len(wDBRes['Responce']['Data']) == 0: wOBJ_DB.Close() return False ### 鍵垢か wChgList = [] if wOBJ_DB.ChgList(wDBRes['Responce']['Data'], outList=wChgList) != True: ##ないケースかも self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_UserCorr: IsActiveUser: Select data is Zero") wOBJ_DB.Close() return False if wChgList[0][0] == True: wOBJ_DB.Close() return False #鍵つき ############################# # DB切断 wOBJ_DB.Close() ###有効なユーザ return True
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