[PYTHON] Try to delete tweets in bulk using Twitter API

Erase dark tweets with python-twitter

Trigger

A friend sent me a screenshot of a tweet from my junior high school days, and while thinking that I missed it, looking back at my past tweets was so terrible that I decided to delete them all at once. I can't put it on because it's really terrible and painful.

It's a good opportunity to touch Twitter's API, so I tried to delete all tweets. ~~ (I felt like using the Black History Cleaner was a loss) ~~

API usage application

Apply for Twitter API usage from the following site. https://developer.twitter.com/en/apps

It seems that each person takes time to approve, but I did not pass the application once and it took about a month.

Get Twitter data

It is said that the data that can be acquired from the API is up to 3200 at a time, so this time we will download the Twitter data and refer to the data from there. You can get it from the following URL.

https://twitter.com/settings/account

スクショ3.jpg

After downloading, check the JSON file in it to see if you can download the Twitter data.

The information of one tweet is written in the JSON file as follows.

tweet.json


{
  "tweet" : {
    "retweeted" : false,
    "source" : "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
    "entities" : {
      "hashtags" : [ ],
      "symbols" : [ ],
      "user_mentions" : [ ],
      "urls" : [ ]
    },
    "display_text_range" : [ "0", "52" ],
    "favorite_count" : "1",
    "id_str" : "1249886861201309697",
    "truncated" : false,
    "retweet_count" : "0",
    "id" : "1249886861201309697",
    "created_at" : "Tue Apr 14 02:27:04 +0000 2020",
    "favorited" : false,
    "full_text" : "nasne's HDD rattles and the support has ended in earnest, so it's a replacement, but I wonder if it's a good alternative product",
    "lang" : "ja"
  }
}

Library installation

Install python-twitter using pip.

pip install python-twitter

Delete code

The actual Python code. In twitter.Api (), write Consumer API keys and ʻAccess token & access token secret` of the app to be created after approval.

This time, I want to erase the painful tweets before 2016, so I will get the id whose created_at is smaller than 2016 and pass it to DestroyStatus.

delete_tweets.py


import twitter
import json

tweet_json_path = 'resource/tweet.json'
json_open = open(tweet_json_path, 'r')
json_load = json.load(json_open)

api = twitter.Api(
    consumer_key='*****************',
    consumer_secret='*****************',
    access_token_key='*****************',
    access_token_secret='*****************'
)

for n in json_load:
    if int(n["tweet"]["created_at"][-4:]) < 2016:
        api.DestroyStatus(n["tweet"]["id"])

api.PostUpdates('Deleted tweets before 2016 completed from Python')

All you have to do is run it. With this, I was able to bury the darkness of the past while studying.

Congratulations: blush:

Recommended Posts

Try to delete tweets in bulk using Twitter API
Try using the Twitter API
Try using the Twitter API
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Bulk posting to Qiita: Team using Qiita API
Tweet using the Twitter API in Python
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
Try to separate Controllers using Blueprint in Flask
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Continue to retrieve tweets containing specific keywords using the Streaming API in Python
Try drawing a social graph using Twitter API v2
Try to make RESTful API with MVC using Flask 1.0.2
Post to your account using the API on Twitter
Post to Twitter using Python
Try using the PeeringDB 2.0 API
Try to make it using GUI and PyQt in Python
Try to determine food photos using Google Cloud Vision API
Tweet Now Playing to Twitter using the Spotify API. [Python]
Image analysis with Object Detection API to try in 1 hour
Try using Janus gateway's Admin API
Try using Spyder included in Anaconda
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using pynag to configure Nagios
Try to calculate Trace in Python
Try to put data in MongoDB
How to kill processes in bulk
Try to log in to Netflix automatically using python on your PC
Try to get statistics using e-Stat
Try using Python argparse's action API
Cython to try in the shortest
Regularly upload files to Google Drive using the Google Drive API in Python
Access the Twitter API in Python
Try using Leap Motion in Python
Try using SpatiaLite to store spatial information such as maps in SQLite
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
I tried to make PyTorch model API in Azure environment using TorchServe
Mouse operation using Windows API in Python
Try logging in to qiita with Python
I tried using Twitter api and Line api
Try using the HL band in order
How to delete expired sessions in Django
Get mail using Gmail API in Java
Try to reproduce NumPy's add.at in Julia
Try using Dropbox API v2 with Go
Try to detect fusion movement using AnyMotion
Regularly post to Twitter using AWS lambda!
Get Youtube data in Python using Youtube Data API
Try to operate Excel using Python (Xlwings)
Quickly try Microsoft's Face API in Python
Try hitting the YouTube API in Python
Try hitting the Spotify API in Django.
Try to download Youtube videos using Pytube
Try to build a pipeline to store the result in Bigquery by hitting the Youtube API regularly using Cloud Composer
Use twitter API to get the number of tweets related to a certain keyword
Python beginners tried it in 3 days from OS installation to running Twitter API
To automatically send an email with an attachment using the Gmail API in Python
How to selectively delete past tweets with Tweepy