def __run(self): ############################# # 開始ログ self.Obj_Parent.OBJ_Mylog.Log('b', self.CHR_LogName + " 開始") ############################# # Twitterリーダ パターン読み込み if self.__getTwitterPatt() != True: return ############################# # トレンドの送信 if gVal.FLG_Test_Mode == False: # 1時間経ってる周回か if gVal.STR_TimeInfo['OneHour']==True and \ self.CHR_TrendSender==self.Obj_Parent.CHR_Account : self.Send_Trend() else: ###テストモード時 if self.CHR_TrendSender == self.Obj_Parent.CHR_Account: self.Send_Trend() ############################# # Twitterから取得 # ハード監視ユーザ 1つのみで実行 if CLS_UserData.sCheckHardUser(self.Obj_Parent.CHR_Account) == True: ############################# # 送信済レコードを削除 # ・1時間経ってる周回か if gVal.STR_TimeInfo['OneHour'] == True: self.Delete_DBctrl_Twitter() ############################# # Twitterタイムライン取得 if self.Get_TwitterTL() != True: return ############################# # 過去TLの読み込み wRes = self.Get_RateTwitterTL() if wRes != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: __run: Get_RateTwitterTL failed") return if len(self.ARR_RateTL) == 0: self.Init_RateTwitterTL() if gVal.FLG_Test_Mode == False: self.Obj_Parent.OBJ_Mylog.Log( 'b', self.CHR_LogName + " Twitter過去TL初期化") else: self.Obj_Parent.OBJ_Mylog.Log('b', self.CHR_LogName + " Twitter過去TL初期化", inView=True) return ############################# # 新規ツイートを辞書に収める self.ARR_Twitter = {} wIndex = 0 self.ARR_UpdateTL = [] for wROW in self.ARR_NewTL: ############################# # チェックするので新過去TLに保管 self.ARR_UpdateTL.append(wROW['id']) ############################# # 過去チェックしたトゥートはスキップ wFlg_Rate = False for wRow_Rate in self.ARR_RateTL: if str(wRow_Rate) == str(wROW['id']): wFlg_Rate = True break if wFlg_Rate == True: continue ############################# # パターンチェック wRes_check = self.__checkTwitterPatt(wROW) if wRes_check['result'] != True: continue ############################# # 格納 self.ARR_Twitter.update({wIndex: ""}) self.ARR_Twitter[wIndex] = {} self.ARR_Twitter[wIndex].update({"id": str(wROW['id'])}) self.ARR_Twitter[wIndex].update({"text": str(wROW['text'])}) self.ARR_Twitter[wIndex].update( {"screen_name": str(wROW['user']['screen_name'])}) self.ARR_Twitter[wIndex].update( {"send_user": wRes_check['send_user']}) self.ARR_Twitter[wIndex].update({"tags": wRes_check['tags']}) self.ARR_Twitter[wIndex].update({"lupdate": ""}) self.ARR_Twitter[wIndex].update({"sended": False}) #更新時間 (twitter時間) wTime = CLS_OSIF.sGetTimeformat_Twitter(wROW['created_at']) if wTime['Result'] != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: __run: sGetTimeformat_Twitter is failed" ) continue self.ARR_Twitter[wIndex]['lupdate'] = wTime['TimeDate'] wIndex += 1 self.STR_Cope["Now_Cope"] += 1 ############################# # 新・過去LTL保存 wRes = self.Set_RateTwitterTL() if wRes != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: __run: Set_RateTwitterTL failed") return ############################# # DBに格納 self.Set_DBctrl_Twitter() ############################# # トゥート送信 self.Send_Toot() ############################# # 処理結果ログ wStr = self.CHR_LogName + " 結果: Cope=" + str(self.STR_Cope['Now_Cope']) wStr = wStr + " Twitter=" + str(self.STR_Cope['Twitter']) wStr = wStr + " Sended=" + str(self.STR_Cope['Sended']) wStr = wStr + " DB=[Insert:" + str( self.STR_Cope['Insert']) + " Update:" + str( self.STR_Cope['Update']) + " Delete:" + str( self.STR_Cope['Delete']) + "]" if self.STR_Cope['TrendSended'] > 0: wStr = wStr + " Trend Sended" if gVal.FLG_Test_Mode == False: self.Obj_Parent.OBJ_Mylog.Log('b', wStr) else: self.Obj_Parent.OBJ_Mylog.Log('b', wStr, inView=True) return
def Send_Trend(self): ############################# # Twitterトレンド取得 wRes = self.Obj_Parent.OBJ_Twitter.GetTrends() if wRes['Result'] != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Send_Trend: Twitter API Error: " + wRes['Reason']) return False if len(wRes['Responce']) == 0: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Send_Trend: Twitter Trends get is Zero") return False if 'trends' not in wRes['Responce'][0]: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Send_Trend: Twitter Trends data is not exist" ) return False ############################# # 時刻の取り出し wTime = CLS_OSIF.sGetTimeformat_Twitter( wRes['Responce'][0]['created_at']) wGetTimeDate = None if wTime['Result'] != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Send_Trend: sGetTimeformat_Twitter is failed" ) return False wGetTimeDate = wTime['TimeDate'] ############################# # トレンドの取り出し wARR_Trends = wRes['Responce'][0]['trends'] ############################# # トゥートの組み立て wCHR_Title = "Twitter Trend" wCHR_Body = "" wIndex = 0 for wLine in wARR_Trends: ############################# # 最大数 if self.DEF_MAX_TREND <= wIndex: break wInd = wLine['name'].find("#") if wInd < 0: wLine['name'] = "#" + wLine['name'] ### Body wCHR_Body = wCHR_Body + wLine['name'] ### Point if wLine['tweet_volume'] != None: wCHR_Body = wCHR_Body + " (" + str(wLine['tweet_volume']) + ")" wCHR_Body = wCHR_Body + '\n' + '\n' wIndex += 1 if wIndex == 0: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Send_Trend: Trend Body size is zero") return wCHR_Body = wCHR_Body + "[" + wGetTimeDate + "] " wCHR_Body = wCHR_Body + "#" + gVal.STR_MasterConfig[ 'TwitterReaderTag'] + '\n' wCHR_Toot = wCHR_Body + "**上から最新順 (Top " + str( self.DEF_MAX_TREND) + " Tag)" + '\n' + "**() タグ使用数" ############################# # トゥートの送信 wRes = self.Obj_Parent.OBJ_MyDon.Toot( status=wCHR_Toot, spoiler_text=wCHR_Title, visibility=self.DEF_SENDRANGE_TREND) if wRes['Result'] != True: self.Obj_Parent.OBJ_Mylog.Log( 'a', "CLS_TwitterReader: Send_Trend: Mastodon error: " + wRes['Reason']) return ############################# # カウント self.STR_Cope['TrendSended'] += 1 return