Skip to content

Instantly share code, notes, and snippets.

@n-eq
Forked from intentionally-left-nil/deloldtweets.py
Last active March 21, 2021 13:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save n-eq/d6bd3438cd87ab419888edbc4dc1f0f5 to your computer and use it in GitHub Desktop.
Save n-eq/d6bd3438cd87ab419888edbc4dc1f0f5 to your computer and use it in GitHub Desktop.
Delete old tweets based on twitter archive
#!/bin/python3
# Fork of https://gist.github.com/davej/113241
# Requirements:
# - twitter API credentials (replace the correponding variables)
# - tweet.js file you get by extracting your twitter archive zip file (located in data/)
# License : Unlicense http://unlicense.org/
import tweepy
import
import json
from datetime import datetime, timedelta
from dateutil import parser
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
days_to_keep = 365
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
utc = pytz.UTC
cutoff_date = utc.localize(datetime.now() - timedelta(days=days_to_keep))
fp = open("tweet.js","r")
fp.seek(25) # skip to '[' which is the beginning of the json part
myjson = json.load(fp)
for i in range(len(myjson)):
parsed_date = parser.parse(myjson[i]['tweet']['created_at'])
# print(parsed_date)
if parsed_date < cutoff_date:
# beware, this operation is irreversible
try:
# print("Destroying tweet %s" % myjson[i]['tweet']['id_str'])
api.destroy_status(myjson[i]['tweet']['id_str'])
except Exception as e:
print("There was an exception: %s." % e)
pass
@martinikem
Copy link

I am getting this error message when i run your script

Traceback (most recent call last):
File "deloldtweets.py", line 30, in
myjson = json.load(fp)
File "/usr/lib/python2.7/json/init.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/init.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

how do i rectify it

@n-eq
Copy link
Author

n-eq commented Apr 30, 2020

Could you successfully dry-run the script ? by commenting line 37 and simply display the tweet to destroy for example.

@danielfeloiola
Copy link

I tried your code, but on lines 36-39 only the except part is getting executed. Any ideas? (I've already checked API credentials and they work fine...)

@n-eq
Copy link
Author

n-eq commented May 5, 2020

Hi Daniel, Could you replace except with :

except Exception as e:
    print e

And try again so we can figure out the exception that is raised ?

Cheers,

@n-eq
Copy link
Author

n-eq commented May 5, 2020

Could you successfully dry-run the script ? by commenting line 37 and simply display the tweet to destroy for example.

I am getting this error message when i run your script

Traceback (most recent call last):
File "deloldtweets.py", line 30, in
myjson = json.load(fp)
File "/usr/lib/python2.7/json/init.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/init.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

how do i rectify it

It seems your file could not be parsed by json, could you double-check that ?

@danielfeloiola
Copy link

It was actually a silly mistake. I was loading an old tweets.js file that had more tweets in it. The code was actually going ok. Sorry for bothering

@n-eq
Copy link
Author

n-eq commented May 5, 2020

No worries, glad to know it worked.

@Achifaifa
Copy link

Achifaifa commented May 18, 2020

Hello!

Used this today and it worked great, thanks a ton for the work. Had to do some changes for it to work properly, letting you know just in case you want to update this.

  • The tweet.js file starts with a js variable assignment, which breaks the json parser. Leaving the [ as first character fixes it. Can be done with something like sed -i 's/variablename = //g' tweet.js. I don't remember the exact variable name, but it's in the first line of the file so that's easy to find. Make sure to replace the space after the equal sign, if it's left as the first character in the file the parser will break.

  • the except:pass has caused some trouble for me (Left the script to run overnight, then found out nothing was deleted because the token was old and didn't have the new write permissions). I'd prevent a newline from being added when printing the date (In python2 it was with a comma, no idea how p3 works with this), then print "delete" after tweet deletion so there is a clear feedback of what's being done.

  • I'd also change the exception to except Exception as e: to catch whatever, then use print e.message instead of the pass. That way you can see which tweets fail to be deleted (Normally because they are not there anymore) and why, and you can abort the thing if there's something weird going on

Sorry I didn't made the changes myself, I don't think there is a way to contribute to gists and I definitely don't want people asking me stuff about this after seeing this script in my profile 😅

Cheers, and again thanks for the work.

@n-eq
Copy link
Author

n-eq commented May 20, 2020

Hi @Achifaifa,

Thanks for the great feedback!
I'm glad you managed to use the script successfully. I'll publish a modified version of the gist to take into account your input.
By the way, the variable name is window.YTD.tweet.part0.

Kind regards.

@martinikem
Copy link

Eventually got it to work and managed to delete over 40,000 tweets but i have got about 2,000 left that just won't go away and have the days to keep set at 0.
And to encounter some limitations, where the script ran for hours and no tweets (or very few were deleted, about 10 to 20).

@drownranger
Copy link

Ok i got it working, adding encoding='UTF-8' in fp = open after i have problem with unicode decode error, and changing the tweet.js variable leaving [ only and it worked.. Thanks for the awesome code!

@n-eq
Copy link
Author

n-eq commented Jun 23, 2020

Hi @drownranger, thanks for the comment. Glad you managed to make it work!

@n-eq
Copy link
Author

n-eq commented Jun 24, 2020

Script updated to take into account your comments : added more context and fixed the issue with json parsing.

Thanks everyone! (especially @Achifaifa @drownranger)

@Carolinewk
Copy link

Hi!! I'm a totally beginner here. How do I run it? I create a folder, put my tweet.js file there together with your script. How do I run it? In CMD, it doesn't work :(

@n-eq
Copy link
Author

n-eq commented Jul 7, 2020

Hi @Carolinewk, no worries.
There are two main requirements actually:

  1. You need to get access to Twitter API, navigate http://apps.twitter.com/ and create an app from your account. You'll then need to fill some basic information but this operation is quite straightforward. Upon success, you'll get 4 elements (consumer_key, consumer_secret, access_token, access_token_secret). Replace with the corresponding values in lines 15-19, make sure you are the only one to know the values.
  2. You need to download your personal twitter archive. For this, please see https://help.twitter.com/en/managing-your-account/how-to-download-your-twitter-archive. Once downloaded, you'll get a zip file, unzip it and extract a file named tweet.js located in data/.

Once you completed these 2 steps you'll just have to configure and run the script above.
Are you using windows ?

@Carolinewk
Copy link

Hi @Carolinewk, no worries.
There are two main requirements actually:

  1. You need to get access to Twitter API, navigate http://apps.twitter.com/ and create an app from your account. You'll then need to fill some basic information but this operation is quite straightforward. Upon success, you'll get 4 elements (consumer_key, consumer_secret, access_token, access_token_secret). Replace with the corresponding values in lines 15-19, make sure you are the only one to know the values.
  2. You need to download your personal twitter archive. For this, please see https://help.twitter.com/en/managing-your-account/how-to-download-your-twitter-archive. Once downloaded, you'll get a zip file, unzip it and extract a file named tweet.js located in data/.

Once you completed these 2 steps you'll just have to configure and run the script above.
Are you using windows ?

Yeah. I'm using windows. Actually I did everything and the keys are already with me. I just can't run the code haha, pretty stupid but It still very confusing to me (I have no experience with python).

I think it's supposed to configure it with IDLE, right? I just put the keys there.
I created a folder, put your script there (deloldtweets.py), edited it with the IDLE (putting the keys in the beginning), and finally, put the tweet.js file there.

So, in the folder there's only these 2 files. But I don't know how to run them. I tried CMD and Powershell. With cmd: I put (cd + folder location) and then python deloldtweets.py. It didn't work. And with powershell I did the same. Nothing happens :(

Sorry if it's not clear.

@n-eq
Copy link
Author

n-eq commented Jul 7, 2020

Hi @Carolinewk, no worries.
There are two main requirements actually:

  1. You need to get access to Twitter API, navigate http://apps.twitter.com/ and create an app from your account. You'll then need to fill some basic information but this operation is quite straightforward. Upon success, you'll get 4 elements (consumer_key, consumer_secret, access_token, access_token_secret). Replace with the corresponding values in lines 15-19, make sure you are the only one to know the values.
  2. You need to download your personal twitter archive. For this, please see https://help.twitter.com/en/managing-your-account/how-to-download-your-twitter-archive. Once downloaded, you'll get a zip file, unzip it and extract a file named tweet.js located in data/.

Once you completed these 2 steps you'll just have to configure and run the script above.
Are you using windows ?

Yeah. I'm using windows. Actually I did everything and the keys are already with me. I just can't run the code haha, pretty stupid but It still very confusing to me (I have no experience with python).

I think it's supposed to configure it with IDLE, right? I just put the keys there.
I created a folder, put your script there (deloldtweets.py), edited it with the IDLE (putting the keys in the beginning), and finally, put the tweet.js file there.

So, in the folder there's only these 2 files. But I don't know how to run them. I tried CMD and Powershell. With cmd: I put (cd + folder location) and then python deloldtweets.py. It didn't work. And with powershell I did the same. Nothing happens :(

Sorry if it's not clear.

Okay, it might be tricky as I don't have a Windows machine on hand.
Basically you're all set once you have defined the parameters in lines 15-19 and put the tweet.js file in the same directory.
Can you try running execfile('deloldtweets.py') from the IDLE shell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment