-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patholdtweets.py
executable file
·176 lines (141 loc) · 6.04 KB
/
oldtweets.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# encoding: utf-8
"""
READ ME FIRST!
This script is a way to back up (and delete, if you so desire) all your tweets beyond the latest 100
This script will work if, and only if, you:
1/ Install python-twitter: http://code.google.com/p/python-twitter/ and dependencies
(or `pip install -Ur requirements.txt`)
2/ go to dev.twitter.com, sign up with your account and create a new app (the details can be bogus, your app will be private)
3/ copy the consumer key and secret from your app in a credentials file
4/ go to "my access token" in the (righthand) menu of your app and copy the token and key in a credentials file
(a credentials file is distributed with this script, as a sample)
"""
import time
import datetime
import twitter
import sys
import getopt
import math
help_message = '''
oldtweets.py - backup and delete your tweets older than 4 weeks ago
Based on a script by David Larlet @davidbgk
* see all tweets older than 4 weeks
cat credentials | ./oldtweets.py
* see all _liked_ tweets older than 4 weeks
cat credentials | ./oldtweets.py --likes
* print *and delete from twitter* tweets older than 4 weeks (oldest at the top)
cat credentials | ./oldtweets.py --delete >> mytweetsbackupfile.txt
* print *and un-like* liked tweets older than 4 weeks
cat credentials | ./oldtweets.py --delete --likes
* [FIXME] The tweets can still sometimes output in the wrong order, with some duplicates.
cat credentials | ./oldtweets.py | sort | uniq >> mytweetsbackupfile.txt
'''
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
option_delete = False
option_likes = False
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["help", "delete", "likes"])
except getopt.error as msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option in ("-h", "--help"):
raise Usage(help_message)
if option == "--delete":
option_delete = True
if option == "--likes":
option_likes = True
except Usage as err:
print(sys.argv[0].split("/")[-1] + ": " + str(err.msg), file=sys.stderr)
print("\t for help use --help", file=sys.stderr)
return 2
consumer_key = ''
consumer_secret = ''
access_token_key = ''
access_token_secret = ''
for line in sys.stdin.readlines():
params = line.split()
if len(params) == 2:
if params[0] == "consumer_key":
consumer_key = params[1]
if params[0] == "consumer_secret":
consumer_secret = params[1]
if params[0] == "access_token_key":
access_token_key = params[1]
if params[0] == "access_token_secret":
access_token_secret = params[1]
api = twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret,
tweet_mode="extended")
statuses = list()
latest_tweet_id = None
get_more = True
# get all the tweets
while get_more:
add_to_timeline = False
if option_likes:
if latest_tweet_id:
add_statuses = api.GetFavorites(count=200, max_id=latest_tweet_id)
else:
add_statuses = api.GetFavorites(count=200)
else:
if latest_tweet_id:
add_statuses = api.GetFavorites(count=200, include_rts=True, max_id=latest_tweet_id)
else:
add_statuses = api.GetUserTimeline(count=200, include_rts=True)
if len(add_statuses) > 0 and len(statuses) == 0 : #tweets returned, we begin the list
add_to_timeline = True
elif len(add_statuses) > 0 and (add_statuses[-1].id != statuses[-1].id): # tweets returned and it's not just the last one over and over again
add_to_timeline = True
if add_to_timeline:
statuses = statuses + add_statuses
latest_tweet_id = statuses[-1].id
else:
get_more = 0
latest_tweet_id= None
print(latest_tweet_id)
time.sleep(1)
start_delete_at = None
# discard tweets posted between now and 4 weeks ago
fourweeksago = datetime.date.today()-datetime.timedelta(28)
while start_delete_at == None:
status = statuses.pop(0)
status_created_at = datetime.datetime.strptime(status.created_at, "%a %b %d %H:%M:%S +0000 %Y")
if datetime.date(status_created_at.year, status_created_at.month, status_created_at.day) < fourweeksago:
start_delete_at = status.id
for tweet in statuses[::-1]:
status_created_at = datetime.datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y")
# [FIXME] Making sure not to delete new stuff, which for some odd reason seems to be necessary
if datetime.date(status_created_at.year, status_created_at.month, status_created_at.day) < fourweeksago:
tweet_text = (tweet.full_text if hasattr(tweet, "full_text") else tweet.text).encode('utf-8').strip()
if option_likes:
print("Tweet id: ", tweet.id, " -- Date: ", tweet.created_at, " || ", tweet_text)
else:
print("Tweet id: ", tweet.id, " -- Date: ", tweet.created_at, " || ", tweet_text)
# delete
if option_delete:
if option_likes:
try:
status = api.DestroyFavorite(status_id=tweet.id)
# wait a bit, throttled api.
time.sleep(2)
except Exception as e:
pass
else:
try:
status = api.DestroyStatus(tweet.id)
# wait a bit, throttled api.
time.sleep(2)
except Exception as e:
pass
if __name__ == "__main__":
sys.exit(main())