]> jfr.im git - z_archive/twitter.git/blame_incremental - twitter/logger.py
bandaid unicode/str.encode-related crash bug
[z_archive/twitter.git] / twitter / logger.py
... / ...
CommitLineData
1"""
2twitter-log - Twitter Logger/Archiver
3
4USAGE:
5
6 twitter-log <screen_name> [max_id]
7
8DESCRIPTION:
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
25from __future__ import print_function
26
27import sys
28import os
29from time import sleep
30
31from .api import Twitter, TwitterError
32from .cmdline import CONSUMER_KEY, CONSUMER_SECRET
33from .auth import NoAuth
34from .oauth import OAuth, write_token_file, read_token_file
35from .oauth_dance import oauth_dance
36from .util import printNicely
37
38# Registered by @sixohsix
39CONSUMER_KEY = "OifqLIQIufeY9znQCkbvg"
40CONSUMER_SECRET = "IedFvi0JitR9yaYw9HwcCCEy4KYaLxf4p4rHRqGgX80"
41OAUTH_FILENAME = os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.twitter_log_oauth'
42
43def log_debug(msg):
44 print(msg, file=sys.stderr)
45
46def 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
70def 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