]> jfr.im git - z_archive/twitter.git/blame - twitter/logger.py
Version 1.14.1.
[z_archive/twitter.git] / twitter / logger.py
CommitLineData
146b4f29 1"""
c115f12f 2twitter-log - Twitter Logger/Archiver
146b4f29
MV
3
4USAGE:
5
c115f12f 6 twitter-log <screen_name> [max_id]
146b4f29
MV
7
8DESCRIPTION:
9
10 Produce a complete archive in text form of a user's tweets. The
11 archive format is:
12
5f3ddd05
MV
13 screen_name <tweet_id>
14 Date: <tweet_time>
15 [In-Reply-To: a_tweet_id]
146b4f29 16
c115f12f 17 Tweet text possibly spanning multiple lines with
146b4f29
MV
18 each line indented by four spaces.
19
5f3ddd05
MV
20
21 Each tweet is separated by two blank lines.
146b4f29
MV
22
23"""
24
5f3ddd05
MV
25from __future__ import print_function
26
146b4f29
MV
27import sys
28import os
7f9d60c6
MV
29from time import sleep
30
098660ce
MV
31from .api import Twitter, TwitterError
32from .cmdline import CONSUMER_KEY, CONSUMER_SECRET
33from .auth import NoAuth
af4bb0ff
MV
34from .oauth import OAuth, write_token_file, read_token_file
35from .oauth_dance import oauth_dance
098660ce
MV
36from .util import printNicely
37
af4bb0ff
MV
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'
146b4f29 42
7f9d60c6 43def log_debug(msg):
5f3ddd05 44 print(msg, file=sys.stderr)
146b4f29 45
c115f12f
MV
46def get_tweets(twitter, screen_name, max_id=None):
47 kwargs = dict(count=3200, screen_name=screen_name)
7f9d60c6
MV
48 if max_id:
49 kwargs['max_id'] = max_id
146b4f29 50
7f9d60c6 51 n_tweets = 0
146b4f29
MV
52 tweets = twitter.statuses.user_timeline(**kwargs)
53 for tweet in tweets:
7f9d60c6
MV
54 if tweet['id'] == max_id:
55 continue
5f3ddd05
MV
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()
146b4f29 62 for line in tweet['text'].splitlines():
098660ce 63 printNicely(' ' + line + '\n')
5f3ddd05
MV
64 print()
65 print()
7f9d60c6
MV
66 max_id = tweet['id']
67 n_tweets += 1
68 return n_tweets, max_id
69
70def main(args=sys.argv[1:]):
c115f12f 71 if not args:
5f3ddd05 72 print(__doc__)
c115f12f
MV
73 return 1
74
af4bb0ff
MV
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
c115f12f
MV
87 screen_name = args[0]
88
89 if args[1:]:
90 max_id = args[1]
7f9d60c6
MV
91 else:
92 max_id = None
93
94 n_tweets = 0
95 while True:
96 try:
c115f12f 97 tweets_processed, max_id = get_tweets(twitter, screen_name, max_id)
7f9d60c6
MV
98 n_tweets += tweets_processed
99 log_debug("Processed %i tweets (max_id %s)" %(n_tweets, max_id))
100 if tweets_processed == 0:
af4bb0ff 101 log_debug("That's it, we got all the tweets we could. Done.")
7f9d60c6 102 break
098660ce 103 except TwitterError as e:
7f9d60c6
MV
104 log_debug("Twitter bailed out. I'm going to sleep a bit then try again")
105 sleep(3)
c115f12f
MV
106
107 return 0