]>
jfr.im git - z_archive/twitter.git/blob - twitter/logger.py
e18d7a0630a88987fa1e620d4734426b8ca012a4
2 twitter-log - Twitter Logger/Archiver
6 twitter-log <screen_name> [max_id]
10 Produce a complete archive in text form of a user's tweets. The
13 screen_name <tweet_id>
15 [In-Reply-To: a_tweet_id]
17 Tweet text possibly spanning multiple lines with
18 each line indented by four spaces.
21 Each tweet is separated by two blank lines.
25 from __future__
import print_function
29 from time
import sleep
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
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'
44 print(msg
, file=sys
.stderr
)
46 def get_tweets(twitter
, screen_name
, max_id
=None):
47 kwargs
= dict(count
=3200, screen_name
=screen_name
)
49 kwargs
['max_id'] = max_id
52 tweets
= twitter
.statuses
.user_timeline(**kwargs
)
54 if tweet
['id'] == max_id
:
56 print("%s %s\nDate: %s" % (tweet
['user']['screen_name'],
59 if tweet
.get('in_reply_to_status_id'):
60 print("In-Reply-To: %s" % tweet
['in_reply_to_status_id'])
62 for line
in tweet
['text'].splitlines():
63 printNicely(' ' + line
+ '\n')
68 return n_tweets
, max_id
70 def main(args
=sys
.argv
[1:]):
75 if not os
.path
.exists(OAUTH_FILENAME
):
77 "the Python Twitter Logger", CONSUMER_KEY
, CONSUMER_SECRET
,
80 oauth_token
, oauth_token_secret
= read_token_file(OAUTH_FILENAME
)
84 oauth_token
, oauth_token_secret
, CONSUMER_KEY
, CONSUMER_SECRET
),
85 domain
='api.twitter.com')
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.")
103 except TwitterError
as e
:
104 log_debug("Twitter bailed out. I'm going to sleep a bit then try again")