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) ~~
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.
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
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"
}
}
Install python-twitter using pip.
pip install python-twitter
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