-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patharchive-tweets.py
53 lines (45 loc) · 1.46 KB
/
archive-tweets.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
#!/usr/bin/python
import tweepy
import pytz
import os
# Parameters.
me = 'username'
urlprefix = 'http://twitter.com/%s/status/' % me
tweetdir = os.environ['HOME'] + '/Dropbox/twitter/'
tweetfile = tweetdir + 'twitter.txt'
idfile = tweetdir + 'lastID.txt'
datefmt = '%B %-d, %Y at %-I:%M %p'
homeTZ = pytz.timezone('US/Central')
utc = pytz.utc
def setup_api():
"""Authorize the use of the Twitter API."""
a = {}
with open(os.environ['HOME'] + '/.twitter-credentials') as credentials:
for line in credentials:
k, v = line.split(': ')
a[k] = v.strip()
auth = tweepy.OAuthHandler(a['consumerKey'], a['consumerSecret'])
auth.set_access_token(a['token'], a['tokenSecret'])
return tweepy.API(auth)
# Authorize.
api = setup_api()
# Get the ID of the last downloaded tweet.
with open(idfile, 'r') as f:
lastID = f.read().rstrip()
# Collect all the tweets since the last one.
tweets = api.user_timeline(me, since_id=lastID, count=200, include_rts=True)
# Write them out to the twitter.txt file.
with open(tweetfile, 'a') as f:
for t in reversed(tweets):
ts = utc.localize(t.created_at).astimezone(homeTZ)
lines = ['',
t.text,
ts.strftime(datefmt).decode('utf8'),
urlprefix + t.id_str,
'- - - - -',
'']
f.write('\n'.join(lines).encode('utf8'))
lastID = t.id_str
# Update the ID of the last downloaded tweet.
with open(idfile, 'w') as f:
lastID = f.write(lastID)