2 Colorful user's timeline stream
5 from __future__
import print_function
6 from multiprocessing
import Process
8 import os
, os
.path
, argparse
, sys
, time
10 from twitter
.stream
import TwitterStream
, Timeout
, HeartbeatTimeout
, Hangup
11 from twitter
.api
import *
12 from twitter
.oauth
import OAuth
, read_token_file
13 from twitter
.oauth_dance
import oauth_dance
14 from twitter
.util
import printNicely
15 from dateutil
import parser
28 screen_name
= t
['user']['screen_name']
29 name
= t
['user']['name']
30 created_at
= t
['created_at']
31 date
= parser
.parse(created_at
)
32 time
= date
.strftime('%Y/%m/%d %H:%M:%S')
35 user
= cycle_color(name
) + grey(' ' + '@' + screen_name
+ ' ')
36 clock
= grey('[' + time
+ ']')
38 tweet
= map(lambda x
: grey(x
) if x
== 'RT' else x
, tweet
)
39 tweet
= map(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
40 tweet
= map(lambda x
: cyan(x
) if x
[0:7] == 'http://' else x
, tweet
)
41 tweet
= ' '.join(tweet
)
44 line1
= u
"{u:>{uw}}:".format(
48 line2
= u
"{c:>{cw}}".format(
55 return line1
, line2
, line3
, line4
58 def parse_arguments():
63 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
68 help='Timeout for the stream (seconds).')
71 '--heartbeat-timeout',
72 help='Set heartbeat timeout.',
78 help='Set stream to non-blocking.')
82 help='Search the stream for specific text.')
83 return parser
.parse_args()
88 authenticate with Twitter OAuth
90 # When using rainbow stream you must authorize.
91 twitter_credential
= os
.environ
.get(
95 '')) + os
.sep
+ '.rainbow_oauth'
96 if not os
.path
.exists(twitter_credential
):
97 oauth_dance("Rainbow Stream",
101 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
109 def get_decorated_name():
111 Beginning of every line
113 t
= Twitter(auth
=authen())
114 name
= '@' + t
.statuses
.user_timeline()[-1]['user']['screen_name']
115 auth_obj
['decorated_name'] = grey('[') + grey(name
) + grey(']: ')
121 t
= Twitter(auth
=authen())
122 t
.statuses
.update(status
=stuff
)
127 Listen to user's input
129 for line
in iter(stdin
.readline
, ''):
131 if line
.startswith('!'):
134 sys
.stdout
.write(auth_obj
['decorated_name'])
142 args
= parse_arguments()
146 print("Tip: Press ENTER and put a '!' in the beginning to start composing a new tweet")
148 # These arguments are optional:
150 timeout
=args
.timeout
,
151 block
=not args
.no_block
,
152 heartbeat_timeout
=args
.heartbeat_timeout
)
156 if args
.track_keywords
:
157 query_args
['track'] = args
.track_keywords
160 stream
= TwitterStream(
162 domain
= 'userstream.twitter.com',
164 tweet_iter
= stream
.user(**query_args
)
166 # Iterate over the sample stream.
167 for tweet
in tweet_iter
:
169 printNicely("-- None --")
170 elif tweet
is Timeout
:
171 printNicely("-- Timeout --")
172 elif tweet
is HeartbeatTimeout
:
173 printNicely("-- Heartbeat Timeout --")
174 elif tweet
is Hangup
:
175 printNicely("-- Hangup --")
176 elif tweet
.get('text'):
177 line1
, line2
, line3
, line4
= draw(t
=tweet
)
189 p
= Process(target
= stream
)