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
, signal
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(
61 def parse_arguments():
66 parser
= argparse
.ArgumentParser(description
=__doc__
or "")
71 help='Timeout for the stream (seconds).')
74 '--heartbeat-timeout',
75 help='Set heartbeat timeout.',
81 help='Set stream to non-blocking.')
85 help='Search the stream for specific text.')
86 return parser
.parse_args()
91 authenticate with Twitter OAuth
93 # When using rainbow stream you must authorize.
94 twitter_credential
= os
.environ
.get(
98 '')) + os
.sep
+ '.rainbow_oauth'
99 if not os
.path
.exists(twitter_credential
):
100 oauth_dance("Rainbow Stream",
104 oauth_token
, oauth_token_secret
= read_token_file(twitter_credential
)
112 def get_decorated_name():
114 Beginning of every line
116 t
= Twitter(auth
=authen())
117 name
= '@' + t
.statuses
.user_timeline()[-1]['user']['screen_name']
118 g
['decorated_name'] = grey('[') + grey(name
) + grey(']: ')
125 t
= Twitter(auth
=authen())
126 t
.statuses
.update(status
=g
['stuff'])
133 t
= Twitter(auth
=authen())
134 rel
= t
.search
.tweets(q
='#' + g
['stuff'])['statuses']
135 printNicely(grey('**************************************************************************************\n'))
136 print('Newest',SEARCH_MAX_RECORD
, 'tweet: \n')
139 printNicely(grey('**************************************************************************************\n'))
147 Hi boss! I'm ready to serve you right now!
148 ----------------------------------------------------
149 "!" at the beginning will start to tweet immediately
150 "/" at the beginning will search for 5 lastest twwet
151 "?" will print this help once again
153 ----------------------------------------------------
154 Hvae fun and hang tight!
157 sys
.stdout
.write(g
['decorated_name'])
164 os
.kill(g
['stream_pid'], signal
.SIGKILL
)
170 Process switch by start of line
177 }.get(line
[0],lambda: sys
.stdout
.write(g
['decorated_name']))
182 Listen to user's input
184 for line
in iter(stdin
.readline
, ''):
185 # Save cmd to global variable and call process
186 g
['stuff'] = line
[1:]
195 args
= parse_arguments()
199 # These arguments are optional:
201 timeout
=args
.timeout
,
202 block
=not args
.no_block
,
203 heartbeat_timeout
=args
.heartbeat_timeout
)
207 if args
.track_keywords
:
208 query_args
['track'] = args
.track_keywords
211 stream
= TwitterStream(
213 domain
= 'userstream.twitter.com',
215 tweet_iter
= stream
.user(**query_args
)
217 # Iterate over the sample stream.
218 for tweet
in tweet_iter
:
220 printNicely("-- None --")
221 elif tweet
is Timeout
:
222 printNicely("-- Timeout --")
223 elif tweet
is HeartbeatTimeout
:
224 printNicely("-- Heartbeat Timeout --")
225 elif tweet
is Hangup
:
226 printNicely("-- Hangup --")
227 elif tweet
.get('text'):
236 p
= Process(target
= stream
)
238 g
['stream_pid'] = p
.pid