-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtwitterwipe.py
More file actions
124 lines (80 loc) · 3.04 KB
/
twitterwipe.py
File metadata and controls
124 lines (80 loc) · 3.04 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import tweepy
import json
import yaml
import logging
import concurrent.futures
from datetime import timedelta, datetime
logging.basicConfig(filename='log.log', level=logging.INFO,
format='%(asctime)s %(message)s')
logger = logging.getLogger(__name__)
def main():
logger.info('starting twitterwipe')
with open('config.yaml', 'r') as yamlfile:
config = yaml.load(yamlfile, Loader=yaml.FullLoader)
delete_timestamps = get_delete_timestamps(config)
purge_activity(delete_timestamps)
logger.info('done')
def get_delete_timestamps(config):
curr_dt_utc = datetime.utcnow()
days = config['days_to_save']
likes = days['likes']
retweets = days['retweets']
tweets = days['tweets']
likes_delta = timedelta(likes)
retweets_delta = timedelta(tweets)
tweets_delta = timedelta(tweets)
likes_time = curr_dt_utc - likes_delta
retweets_time = curr_dt_utc - retweets_delta
tweets_time = curr_dt_utc - tweets_delta
return (likes_time, retweets_time, tweets_time)
def purge_activity(delete_timestamps):
api = get_api()
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as e:
e.submit(delete_tweets, api, delete_timestamps[2])
e.submit(delete_retweets, api, delete_timestamps[1])
e.submit(delete_favorites, api, delete_timestamps[0])
def delete_tweets(api, ts):
logger.info('deleting tweets before {}'.format(str(ts)))
count = 0
for status in tweepy.Cursor(api.user_timeline).items():
if status.created_at < ts:
try:
api.destroy_status(status.id)
count += 1
except Exception as e:
logger.error("failed to delete {}".format(status.id), exc_info=True)
logger.info('{} tweets deleted'.format(count))
return
def delete_retweets(api, ts):
logger.info('deleting retweets before {}'.format(str(ts)))
count = 0
for status in tweepy.Cursor(api.user_timeline).items():
if status.created_at < ts:
try:
api.unretweet(status.id)
count+=1
except:
logger.error('failed to unretweet {}'.format(status.id),exc_info=True)
logger.info('{} retweets deleted'.format(count))
return
def delete_favorites(api, ts):
logger.info('deleting favorites before {}'.format(str(ts)))
count = 0
for status in tweepy.Cursor(api.favorites).items():
if status.created_at < ts:
try:
api.destroy_favorite(status.id)
count+=1
except:
logger.error('failed to delete favorite'.format(status.id), exc_info=True)
logger.info('{} favorites deleted'.format(count))
return
def get_api():
with open('keys.json', 'r') as f:
d = json.load(f)
auth = tweepy.OAuthHandler(d['consumer_key'], d['consumer_secret'])
auth.set_access_token(d['app_key'], d['app_secret'])
return tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
if __name__ == '__main__':
main()