File: 05-twitter-stream.py

package info (click to toggle)
python-pattern 2.6%2Bgit20150109-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,672 kB
  • sloc: python: 53,865; xml: 11,965; ansic: 2,318; makefile: 94
file content (29 lines) | stat: -rw-r--r-- 910 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os, sys; sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

import time

from pattern.web import Twitter

# Another way to mine Twitter is to set up a stream.
# A Twitter stream maintains an open connection to Twitter, 
# and waits for data to pour in.
# Twitter.search() allows us to look at older tweets,
# Twitter.stream() gives us the most recent tweets.

# It might take a few seconds to set up the stream.
stream = Twitter().stream("I hate", timeout=30)

#while True:
for i in range(100):
    print i
    # Poll Twitter to see if there are new tweets.
    stream.update()
    # The stream is a list of buffered tweets so far,
    # with the latest tweet at the end of the list.
    for tweet in reversed(stream):
        print tweet.text
        print tweet.language
    # Clear the buffer every so often.
    stream.clear()
    # Wait awhile between polls.
    time.sleep(1)