指定した単語を含むツイートを消去する簡易プログラム書いた(Python)
過去のネガティブなツイートを消したい
前提
Pythonの環境構築、必要なパッケージのインストール、ツイート履歴の取得、Twitterの各種トークンの取得が必要ですが、説明は省略しています。
検索をかけて出てくるツイート消去ツールがうまく使えなかったので、自分で書きました。以下コード。
コード
バグがないことを保証しません。これを利用して発生したいかなる損害についても責任を負いません。利用は自己責任でお願いします。
#!/usr/bin/env python # coding: utf-8 # In[1]: import os import sys import pandas as pd import twitter from requests_oauthlib import OAuth1Session # In[2]: consumer_key = os.environ["CONSUMER_KEY"] consumer_secret = os.environ["CONSUMER_SECRET"] access_token = os.environ["ACCESS_TOKEN_KEY"] access_token_secret = os.environ["ACCESS_TOKEN_SECRET"] # In[3]: def return_twitter_api(): api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token_key=access_token, access_token_secret=access_token_secret) return api # In[4]: def delete_tweets(tweet_ids): api = return_twitter_api() count = 0 for id in tweet_ids: status = None try: status = api.DestroyStatus(id) except: if 'No status found with that ID.': continue finally: if status != None: count += 1 else: pass return print("{}件のツイートを消去しました。".format(count)) # In[5]: tweet_df = pd.read_csv("/hoge/tweets.csv") # In[6]: tw_id_and_text_df = tweet_df.reindex(columns=["timestamp", "tweet_id", "text"]) # In[7]: print("消したいキーワードを入力してください。複数の単語を入力するにはスペースで区切る(cを入力でキャンセル)") input_words = input().split() if input_words == ["c"]: print("キャンセルしました") sys.exit() elif input_words == []: print("単語を入力してください") sys.exit() else: pass # In[8]: wanna_delete_words = "|".join(input_words) wanna_delete_df = tw_id_and_text_df[tw_id_and_text_df["text"].str.contains(wanna_delete_words) == True] # In[9]: wd_tweet_ids = wanna_delete_df["tweet_id"] # In[10]: columns = ["timestamp", "text"] new_df = wanna_delete_df.reindex(columns=columns) # In[11]: print(new_df) print("対象ツイートは{}件あります。実行しますか? y/N".format(len(wd_tweet_ids))) ans = input() if ans == "y": delete_tweets(wd_tweet_ids) else: print("キャンセルしました。")
補足
In[2]では環境変数に設定した各種トークンを変数に代入しています。
そのコードをどこにも上げないなら直接ここにconsumer_key = "hogehogefugafuga"
みたいに入力してもOK。
In[4]のexcept内では既に別の単語に引っかかって消去済みのツイートに当たった場合No status found with that ID.
と返ってくるのでそれを無視しています。
本当はそれが増えてくると実行時間の無駄なので、消去済みのものにはフラグを立てていった方がいいんですが、面倒なのでやってません。
やるならpandasのDataFrameに新しいcolumnを作って、消去済みかどうかの真偽値を入れる感じかな。
In[5]にはダウンロードした自分のtweets.csvファイルのパスを入力してください。
キーワードを複数含むツイートなど、既に消去済みの場合があるので対象ツイート数と消去したツイートの数は異なる場合があります。