]>
Commit | Line | Data |
---|---|---|
1 | """ | |
2 | twitter-log - Twitter Logger/Archiver | |
3 | ||
4 | USAGE: | |
5 | ||
6 | twitter-log <screen_name> [max_id] | |
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> | |
14 | Date: <tweet_time> | |
15 | [In-Reply-To: a_tweet_id] | |
16 | ||
17 | Tweet text possibly spanning multiple lines with | |
18 | each line indented by four spaces. | |
19 | ||
20 | ||
21 | Each tweet is separated by two blank lines. | |
22 | ||
23 | """ | |
24 | ||
25 | from __future__ import print_function | |
26 | ||
27 | import sys | |
28 | import os | |
29 | from time import sleep | |
30 | ||
31 | from .api import Twitter, TwitterError | |
32 | from .cmdline import CONSUMER_KEY, CONSUMER_SECRET | |
33 | from .auth import NoAuth | |
34 | from .oauth import OAuth, write_token_file, read_token_file | |
35 | from .oauth_dance import oauth_dance | |
36 | from .util import printNicely | |
37 | ||
38 | # Registered by @sixohsix | |
39 | CONSUMER_KEY = "OifqLIQIufeY9znQCkbvg" | |
40 | CONSUMER_SECRET = "IedFvi0JitR9yaYw9HwcCCEy4KYaLxf4p4rHRqGgX80" | |
41 | OAUTH_FILENAME = os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.twitter_log_oauth' | |
42 | ||
43 | def log_debug(msg): | |
44 | print(msg, file=sys.stderr) | |
45 | ||
46 | def get_tweets(twitter, screen_name, max_id=None): | |
47 | kwargs = dict(count=3200, screen_name=screen_name) | |
48 | if max_id: | |
49 | kwargs['max_id'] = max_id | |
50 | ||
51 | n_tweets = 0 | |
52 | tweets = twitter.statuses.user_timeline(**kwargs) | |
53 | for tweet in tweets: | |
54 | if tweet['id'] == max_id: | |
55 | continue | |
56 | print("%s %s\nDate: %s" % (tweet['user']['screen_name'], | |
57 | tweet['id'], | |
58 | tweet['created_at'])) | |
59 | if tweet.get('in_reply_to_status_id'): | |
60 | print("In-Reply-To: %s" % tweet['in_reply_to_status_id']) | |
61 | print() | |
62 | for line in tweet['text'].splitlines(): | |
63 | printNicely(' ' + line + '\n') | |
64 | print() | |
65 | print() | |
66 | max_id = tweet['id'] | |
67 | n_tweets += 1 | |
68 | return n_tweets, max_id | |
69 | ||
70 | def main(args=sys.argv[1:]): | |
71 | if not args: | |
72 | print(__doc__) | |
73 | return 1 | |
74 | ||
75 | if not os.path.exists(OAUTH_FILENAME): | |
76 | oauth_dance( | |
77 | "the Python Twitter Logger", CONSUMER_KEY, CONSUMER_SECRET, | |
78 | OAUTH_FILENAME) | |
79 | ||
80 | oauth_token, oauth_token_secret = read_token_file(OAUTH_FILENAME) | |
81 | ||
82 | twitter = Twitter( | |
83 | auth=OAuth( | |
84 | oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET), | |
85 | domain='api.twitter.com') | |
86 | ||
87 | screen_name = args[0] | |
88 | ||
89 | if args[1:]: | |
90 | max_id = args[1] | |
91 | else: | |
92 | max_id = None | |
93 | ||
94 | n_tweets = 0 | |
95 | while True: | |
96 | try: | |
97 | tweets_processed, max_id = get_tweets(twitter, screen_name, max_id) | |
98 | n_tweets += tweets_processed | |
99 | log_debug("Processed %i tweets (max_id %s)" %(n_tweets, max_id)) | |
100 | if tweets_processed == 0: | |
101 | log_debug("That's it, we got all the tweets we could. Done.") | |
102 | break | |
103 | except TwitterError as e: | |
104 | log_debug("Twitter bailed out. I'm going to sleep a bit then try again") | |
105 | sleep(3) | |
106 | ||
107 | return 0 |