]>
Commit | Line | Data |
---|---|---|
146b4f29 | 1 | """ |
c115f12f | 2 | twitter-log - Twitter Logger/Archiver |
146b4f29 MV |
3 | |
4 | USAGE: | |
5 | ||
c115f12f | 6 | twitter-log <screen_name> [max_id] |
146b4f29 MV |
7 | |
8 | DESCRIPTION: | |
9 | ||
10 | Produce a complete archive in text form of a user's tweets. The | |
11 | archive format is: | |
12 | ||
13 | screen_name tweet_id tweet_time | |
14 | ||
c115f12f | 15 | Tweet text possibly spanning multiple lines with |
146b4f29 MV |
16 | each line indented by four spaces. |
17 | ||
18 | Each tweet is separated by a blank line. | |
19 | ||
20 | """ | |
21 | ||
22 | import sys | |
23 | import os | |
7f9d60c6 MV |
24 | from time import sleep |
25 | ||
7f9d60c6 | 26 | from api import Twitter, TwitterError |
146b4f29 | 27 | from cmdline import CONSUMER_KEY, CONSUMER_SECRET |
c115f12f | 28 | from auth import NoAuth |
146b4f29 | 29 | |
7f9d60c6 MV |
30 | def log_debug(msg): |
31 | print >> sys.stderr, msg | |
146b4f29 | 32 | |
c115f12f MV |
33 | def get_tweets(twitter, screen_name, max_id=None): |
34 | kwargs = dict(count=3200, screen_name=screen_name) | |
7f9d60c6 MV |
35 | if max_id: |
36 | kwargs['max_id'] = max_id | |
146b4f29 | 37 | |
7f9d60c6 | 38 | n_tweets = 0 |
146b4f29 MV |
39 | tweets = twitter.statuses.user_timeline(**kwargs) |
40 | for tweet in tweets: | |
7f9d60c6 MV |
41 | if tweet['id'] == max_id: |
42 | continue | |
146b4f29 MV |
43 | print "%s %s %s" % (tweet['user']['screen_name'], |
44 | tweet['id'], | |
45 | tweet['created_at']) | |
c115f12f | 46 | |
146b4f29 MV |
47 | for line in tweet['text'].splitlines(): |
48 | print ' ' + line.encode('utf-8') | |
49 | ||
7f9d60c6 MV |
50 | max_id = tweet['id'] |
51 | n_tweets += 1 | |
52 | return n_tweets, max_id | |
53 | ||
54 | def main(args=sys.argv[1:]): | |
7f9d60c6 | 55 | twitter = Twitter( |
c115f12f | 56 | auth=NoAuth(), |
7f9d60c6 MV |
57 | api_version='1', |
58 | domain='api.twitter.com') | |
59 | ||
c115f12f MV |
60 | if not args: |
61 | print __doc__ | |
62 | return 1 | |
63 | ||
64 | screen_name = args[0] | |
65 | ||
66 | if args[1:]: | |
67 | max_id = args[1] | |
7f9d60c6 MV |
68 | else: |
69 | max_id = None | |
70 | ||
71 | n_tweets = 0 | |
72 | while True: | |
73 | try: | |
c115f12f | 74 | tweets_processed, max_id = get_tweets(twitter, screen_name, max_id) |
7f9d60c6 MV |
75 | n_tweets += tweets_processed |
76 | log_debug("Processed %i tweets (max_id %s)" %(n_tweets, max_id)) | |
77 | if tweets_processed == 0: | |
78 | log_debug("That's it, we got all the tweets. Done.") | |
79 | break | |
80 | except TwitterError, e: | |
81 | log_debug("Twitter bailed out. I'm going to sleep a bit then try again") | |
82 | sleep(3) | |
c115f12f MV |
83 | |
84 | return 0 |