Erase all your tweets

Introduction

Occasionally, I want to be a shaven. With that feeling, I wanted to delete all my tweets.

There are various tweet deletion tools. However, due to API reasons, I couldn't delete more than a certain number, I had to become a paid member, and it didn't work well in my environment, so I decided to prepare it myself.

In addition, since I touched both Twitter API and Python for the first time, there may be usages that are out of the ordinary. If you want to refer to it, please do one thing at your own risk.

things to do

Delete all tweets in your Twitter account.

environment

OS:Windows 10 Language: Python 3.8.3

procedure

1. Apply for Twitter API

I applied for use by referring to this site. https://www.itti.jp/web-direction/how-to-apply-for-twitter-api/ https://qiita.com/kngsym2018/items/2524d21455aac111cdee

Apply with the account you want to delete tweets. As soon as I applied, I received an email. Click Confirm Address in the email to use the dashboard and create app information. In the article I referred to, it seems that there was an exchange of usage confirmation, but I did not have such a thing. It may be because the intention of using the app was just "learning and erasing my tweets". The application passed without any problem even in poor English.

2. Python installation

I downloaded and installed the one that suits my environment. https://www.python.org/downloads/windows/

3. Library installation

Open a command prompt and install the library.

py -m pip install python-twitter

The following two have been added to the environment variable Path. The order is reversed.

[Place of installation]\Python38
[Place of installation]\Python38\Scripts

4. Get tweet data

Get an archive of tweet data from your Twitter account. The procedure was written in an easy-to-understand manner on this site. https://passion-blue.com/twitter-account-backup

When you unzip the obtained archive, you will find tweet.js in the data directory. To make it json format, I opened it, deleted the first line "window.YTD.tweet.part0 =", and saved it as tweet.json. (It's just a personal tool, you'll rarely use it, and you'll do it manually.)

5. Script creation

# -*- coding: utf-8 -*-

import twitter
import json
import signal

#Ctrl on the way+Description to be stopped by C
signal.signal(signal.SIGINT, signal.SIG_DFL)

#Information for using the Twitter API
api = twitter.Api(
    consumer_key='YOUR_CONSUMER_KEY',
    consumer_secret='YOUR_CONSUMER_SECRET',
    access_token_key='YOUR_ACCESS_TOKEN_KEY',
    access_token_secret='YOUR_ACCESS_TOKEN_SECRET'
)
screen_name = 'YOUR_TWITTER_ID'

#Read json file
json_open = open('tweet.json', 'r', encoding="utf-8_sig")
json_load = json.load(json_open)

for v in json_load:
    try:
        #Specify the ID and delete
        api.DestroyStatus(v['tweet']['id'])
        print("delete:" + v['tweet']['id'])
    except:
        #If the deletion fails because it has already been deleted
        print("error :" + v['tweet']['id'])

I referred to this article. https://qiita.com/aeas44/items/a5b82da69b64b32aada4 https://qiita.com/junkoda/items/50ca8604ecbb04e9f772

Tokens can be found in the Twitter Developers app-> Details-> Keys and tokens.

6. Run the script

Place the tweet.json created in 4. in the same directory as the script (app.py) and execute it.

python app.py

The deleted ID and the failed ID are spit out to the console one after another. I didn't measure the time properly, so it's a sensory value, but I think it took about an hour to delete about 10,000 tweets at a pace of several tweets per second.

Digression

At first, I got the token with an account other than the Twitter account I wanted to delete, so I got "You may not delete another user's status." At runtime. So, I reacquired the tweet with the account I want to delete. That's right ... but how do paid deletion tools and the like realize the deletion of tweets by applicants ...? It may be possible to do well by performing Twitter authentication. Anyway, the purpose was achieved.

Recommended Posts

Erase all your tweets
Get all live tweets of professional baseball
Get lots of your tweets with Tweepy