-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor-cleanup.py
executable file
·114 lines (87 loc) · 3.46 KB
/
cursor-cleanup.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
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
#!/usr/local/bin/python
#########################################################################################
# Use the Twitter Cursor to get liked (favorited) tweets and remove that status one by one.
#
# Nick Feamster
# December 18, 2020
#########################################################################################
import tweepy
import json
import re
import datetime
import dateutil.parser
import sys
import time
import argparse
#############################################################################################
def oauth_login(consumer_key, consumer_secret):
"""Authenticate with twitter using OAuth"""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
return tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
#############################################################################################
N = 100
parser = argparse.ArgumentParser(description='Clean Tweets.')
parser.add_argument('-n', '--num', type=int, help='number of tweets to load in cursor')
parser.add_argument('-p', '--path', type=str, help='path to config file')
parser.add_argument('-l', '--likes', help='clean likes', action="store_true")
parser.add_argument('-r', '--replies', help='clean replies', action="store_true")
args = parser.parse_args()
if args.num:
num = args.num
else:
num = N
if args.path:
path = args.path
else:
path = '.'
configfile = '{}/config.json'.format(path)
print (configfile)
#############################################################################################
# Fill in these values by creating an app on the Twitter site that has access to your account
with open(configfile, 'rt', encoding='utf-8') as data_file:
json_data = data_file.read()
credentials = json.loads(json_data)[0]
user = credentials['user']
consumer_key = credentials['consumer_key']
consumer_secret = credentials['consumer_secret']
access_key = credentials['access_key']
access_secret = credentials['access_secret']
#############################################################################################
# MAIN
print("[{}]".format(str(datetime.datetime.now())))
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
print("Authenticated as: %s" % api.me().screen_name)
###############
# Cleanup Likes
like_delete_count = 0
for tweet in tweepy.Cursor(api.favorites, id=user,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True).items(num):
status_id = tweet.id
try:
api.destroy_favorite(int(status_id))
print(tweet.text, status_id, 'like removed!')
like_delete_count += 1
except:
print(status_id, 'could not be un-liked.')
print(like_delete_count, 'likes removed.')
###############
# Cleanup Replies
reply_delete_count = 0
for tweet in tweepy.Cursor(api.user_timeline, id=user,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True).items(num):
if tweet.in_reply_to_status_id_str is not None:
status_id = tweet.id
try:
api.destroy_status(int(status_id))
print(tweet.text, status_id, 'reply removed!')
reply_delete_count += 1
except:
print(status_id, 'reply could not be deleted.')
time.sleep(0.1)
print('.', end='', flush=True)
print('\n{} replies removed.'.format(reply_delete_count))