forked from simonw/mytweets
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetch.py
executable file
·227 lines (190 loc) · 6.5 KB
/
fetch.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/python
import oauth2 as oauth
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
"""
Saves your tweets to a local file
"""
import httplib2, urllib, time, sys, re
try:
import json
except ImportError:
import simplejson as json
# Valid options for TIMELINE or -m, and the corresponding endpoints at twitter.com/statuses/ and the local filenames we use.
timelines = {
'user': {
'remote': 'statuses/user_timeline',
'local': 'my_tweets'
},
'friends': {
'remote': 'statuses/home_timeline',
'local': 'my_friends_tweets'
},
'mentions': {
'remote': 'statuses/mentions_timeline',
'local': 'mentions'
},
'direct': {
'remote': 'direct_messages',
'local': 'direct'
},
'direct-sent': {
'remote': 'direct_messages/sent',
'local': 'direct-sent'
},
'favorites': {
'remote': 'favorites/list',
'local': 'favourites'
}
}
if '-k' in sys.argv and '-s' in sys.argv and '-o' in sys.argv and '-e' in sys.argv:
CONSUMER_KEY = sys.argv[sys.argv.index('-k')+1]
CONSUMER_SECRET = sys.argv[sys.argv.index('-s')+1]
ACCESS_TOKEN = sys.argv[sys.argv.index('-o')+1]
ACCESS_TOKEN_SECRET = sys.argv[sys.argv.index('-e')+1]
else:
try:
from config import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
except ImportError:
print "Keys and tokens not specified. Create a config.py file or use the -k, -s, -o and -e command line options"
sys.exit(1)
# Work out whether we're doing user_timeline or friends_timeline.
TIMELINE = 'user'
if '-m' in sys.argv:
TIMELINE = sys.argv[sys.argv.index('-m')+1]
else:
try:
from config import TIMELINE
except ImportError:
pass
try:
REMOTE_TIMELINE = "https://api.twitter.com/1.1/%s.json" % timelines[TIMELINE]['remote']
except KeyError:
print "Invalid timeline: ", TIMELINE
sys.exit(1)
FILE = timelines[TIMELINE]['local']
if '-t' in sys.argv:
FILE = "%s.txt" % FILE
import pickle
def load_all():
try:
return pickle.load(open(FILE))
except IOError:
return []
def write_all(tweets):
pickle.dump(tweets, open(FILE, 'w'))
else:
FILE = "%s.json" % FILE
def load_all():
try:
return json.load(open(FILE))
except IOError:
return []
def write_all(tweets):
json.dump(tweets, open(FILE, 'w'), indent = 2)
if '-f' in sys.argv:
FILE_PATH = sys.argv[sys.argv.index('-f')+1]
else:
try:
from config import FILE_PATH
except ImportError:
print "File_path not specified. Create a config.py file or use the -f command line options"
sys.exit(1)
FILE = FILE_PATH + FILE
def normalize_url(url):
# Simple length heuristic
if len(url) < 10: return None
# Make sure we have some sort of protocol
if not re.search('://', url):
url = 'http://' + url
return url
def lookup_short_urls(tweet):
# If short_urls are already there, skip
if 'short_urls' in tweet: return
# (Start of line or word)
# (Maybe something like http://)
# (A vaguely domain-like section, at least one dot which is not a double dot)
# (Whatever else follows, liberally via non-whitespace)
url_regex = '(\A|\\b)([\w-]+://)?\S+[.][^\s.]\S*'
redir = httplib2.Http(timeout=10)
redir.follow_redirects = False
redir.force_exception_to_status_code = True
short_urls = {}
new_text = tweet['text']
for sub in tweet['text'].split():
orig_url_match = re.search(url_regex, sub)
if not orig_url_match:
continue
orig_url = normalize_url(orig_url_match.group(0))
if not orig_url: continue
try:
response = redir.request(orig_url)[0]
if 'status' in response and response['status'] == '301':
short_urls[response['location']] = orig_url
new_text = new_text.replace(orig_url, response['location'])
except:
pass
tweet['short_urls'] = short_urls
tweet['text'] = new_text
def fetch_and_save_new_tweets():
tweets=load_all()
old_tweet_ids = set(t['id'] for t in tweets)
if tweets:
since_id = max(t['id'] for t in tweets)
else:
since_id = None
try:
new_tweets = fetch_all(since_id)
except ValueError, msg:
print "An error occurred while getting your tweets: ", msg
sys.exit(1)
num_new_saved = 0
for tweet in new_tweets:
if tweet['id'] not in old_tweet_ids:
tweets.append(tweet)
num_new_saved += 1
tweets.sort(key = lambda t: t['id'], reverse=True)
# Delete the 'user' key (unless this is the friends' timeline), lookup short URLs
for t in tweets:
if TIMELINE == 'user' and 'user' in t:
del t['user']
lookup_short_urls(t)
# Save back to disk
write_all(tweets)
print "Saved %s new tweets" % num_new_saved
def fetch_all(since_id = None):
all_tweets = []
seen_ids = set()
page = 0
args = {'count': 200}
if since_id is not None:
args['since_id'] = since_id
all_tweets_len = len(all_tweets)
while True:
args['page'] = page
# Via http://blog.yjl.im/2010/04/first-step-to-twitter-oauth-streaming.html
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
token = oauth.Token(key=ACCESS_TOKEN, secret=ACCESS_TOKEN_SECRET)
client = oauth.Client(consumer, token)
resp, content = client.request("%s?%s" % (REMOTE_TIMELINE, urllib.urlencode(args)), 'GET')
if resp['status'] == '502':
# This usually seems to mean the request has timed out, but if we try again
# the result has been cached and will work second time round.
time.sleep(2)
resp, content = client.request("%s?%s" % (REMOTE_TIMELINE, urllib.urlencode(args)), 'GET')
page += 1
tweets = json.loads(content)
if 'errors' in tweets:
raise ValueError, tweets['errors'][0]['message'] + ' (code ' + str(tweets['errors'][0]['code']) + ')'
if not tweets:
break
for tweet in tweets:
if tweet['id'] not in seen_ids:
seen_ids.add(tweet['id'])
all_tweets.append(tweet)
all_tweets_len = len(all_tweets)
time.sleep(2)
all_tweets.sort(key = lambda t: t['id'], reverse=True)
return all_tweets
if __name__ == '__main__':
fetch_and_save_new_tweets()