-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet-delete.py
77 lines (66 loc) · 2.12 KB
/
tweet-delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import tweepy
import time
from datetime import datetime, timedelta
from credentials import *
# options
test_mode = False
verbose = False
delete_tweets = True
delete_favs = True
days_to_keep = 10
# tweets and favorites to keep
tweets_to_save = [
910473178169868288 # keybase tweet
]
favs_to_save = [
# 000000000000000000
]
# auth and api
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# set cutoff date, use utc to match twitter
cutoff_date = datetime.utcnow() - timedelta(days=days_to_keep)
# delete old tweets
if delete_tweets:
# get all timeline tweets
print("Retrieving timeline tweets")
timeline = tweepy.Cursor(api.user_timeline).items()
deletion_count = 0
ignored_count = 0
for tweet in timeline:
# where tweets are not in save list and older than cutoff date
if tweet.id not in tweets_to_save and tweet.created_at < cutoff_date:
if verbose:
print("Deleting %d: [%s] %s" %
(tweet.id, tweet.created_at, tweet.text))
if not test_mode:
api.destroy_status(tweet.id)
deletion_count += 1
else:
ignored_count += 1
print("Deleted %d tweets, ignored %d" % (deletion_count, ignored_count))
else:
print("Not deleting tweets")
# unfavor old favorites
if delete_favs:
# get all favorites
print("Retrieving favorite tweets")
favorites = tweepy.Cursor(api.favorites).items()
unfav_count = 0
kept_count = 0
for tweet in favorites:
# where tweets are not in save list and older than cutoff date
if tweet.id not in favs_to_save and tweet.created_at < cutoff_date:
if verbose:
print("Unfavoring %d: [%s] %s" %
(tweet.id, tweet.created_at, tweet.text))
if not test_mode:
api.destroy_favorite(tweet.id)
unfav_count += 1
else:
kept_count += 1
print("Unfavored %d tweets, ignored %d" % (unfav_count, kept_count))
else:
print("Not unfavoring tweets")
time.sleep(3)