+ auth = OAuth(
+ consumer_key='[your consumer key]',
+ consumer_secret='[your consumer secret]',
+ token='[your token]',
+ token_secret='[your token secret]'
+ )
+ twitter_userstream = TwitterStream(auth=auth, domain='userstream.twitter.com')
+ for msg in twitter_userstream.user():
+ if 'direct_message' in msg:
+ print msg['direct_message']['text']
+
+ The iterator will yield until the TCP connection breaks. When the
+ connection breaks, the iterator yields `{'hangup': True}`, and
+ raises `StopIteration` if iterated again.
+
+ Similarly, if the stream does not produce heartbeats for more than
+ 90 seconds, the iterator yields `{'hangup': True,
+ 'heartbeat_timeout': True}`, and raises `StopIteration` if
+ iterated again.
+
+ The `timeout` parameter controls the maximum time between
+ yields. If it is nonzero, then the iterator will yield either
+ stream data or `{'timeout': True}` within the timeout period. This
+ is useful if you want your program to do other stuff in between
+ waiting for tweets.
+
+ The `block` parameter sets the stream to be fully non-blocking. In
+ this mode, the iterator always yields immediately. It returns
+ stream data, or `None`. Note that `timeout` supercedes this
+ argument, so it should also be set `None` to use this mode.