| author | Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com> |
| Mon, 19 Dec 2011 00:56:02 +0100 | |
| changeset 428 | 8b51d23b758f |
| parent 13 | 79b6e132e3d7 |
| permissions | -rw-r--r-- |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
1 |
.. -*- restructuredtext -*- |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
2 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
3 |
########################################## |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
4 |
tweetstream - Simple twitter streaming API |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
5 |
########################################## |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
6 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
7 |
Introduction |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
8 |
------------ |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
9 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
10 |
tweetstream provides two classes, SampleStream and FollowStream, that can be |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
11 |
used to get tweets from Twitter's streaming API. An instance of one of the |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
12 |
classes can be used as an iterator. In addition to fetching tweets, the |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
13 |
object keeps track of the number of tweets collected and the rate at which |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
14 |
tweets are received. |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
15 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
16 |
SampleStream delivers a sample of all tweets. FilterStream delivers |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
17 |
tweets that match one or more criteria. Note that it's not possible |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
18 |
to get all tweets without access to the "firehose" stream, which |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
19 |
is not currently avaliable to the public. |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
20 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
21 |
Twitter's documentation about the streaming API can be found here: |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
22 |
http://dev.twitter.com/pages/streaming_api_methods . |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
23 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
24 |
**Note** that the API is blocking. If for some reason data is not immediatly |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
25 |
available, calls will block until enough data is available to yield a tweet. |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
26 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
27 |
Examples |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
28 |
-------- |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
29 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
30 |
Printing incoming tweets: |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
31 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
32 |
>>> stream = tweetstream.SampleStream("username", "password")
|
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
33 |
>>> for tweet in stream: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
34 |
... print tweet |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
35 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
36 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
37 |
The stream object can also be used as a context, as in this example that |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
38 |
prints the author for each tweet as well as the tweet count and rate: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
39 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
40 |
>>> with tweetstream.SampleStream("username", "password") as stream
|
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
41 |
... for tweet in stream: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
42 |
... print "Got tweet from %-16s\t( tweet %d, rate %.1f tweets/sec)" % ( |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
43 |
... tweet["user"]["screen_name"], stream.count, stream.rate ) |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
44 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
45 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
46 |
Stream objects can raise ConnectionError or AuthenticationError exceptions: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
47 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
48 |
>>> try: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
49 |
... with tweetstream.TweetStream("username", "password") as stream
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
50 |
... for tweet in stream: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
51 |
... print "Got tweet from %-16s\t( tweet %d, rate %.1f tweets/sec)" % ( |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
52 |
... tweet["user"]["screen_name"], stream.count, stream.rate ) |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
53 |
... except tweetstream.ConnectionError, e: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
54 |
... print "Disconnected from twitter. Reason:", e.reason |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
55 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
56 |
To get tweets that match specific criteria, use the FilterStream. FilterStreams |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
57 |
take three keyword arguments: "locations", "follow" and "track". |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
58 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
59 |
Locations are a list of bounding boxes in which geotagged tweets should originate. |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
60 |
The argument should be an iterable of longitude/latitude pairs. |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
61 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
62 |
Track specifies keywords to track. The argument should be an iterable of |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
63 |
strings. |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
64 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
65 |
Follow returns statuses that reference given users. Argument should be an iterable |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
66 |
of twitter user IDs. The IDs are userid ints, not the screen names. |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
67 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
68 |
>>> words = ["opera", "firefox", "safari"] |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
69 |
>>> people = [123,124,125] |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
70 |
>>> locations = ["-122.75,36.8", "-121.75,37.8"] |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
71 |
>>> with tweetstream.FilterStream("username", "password", track=words,
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
72 |
... follow=people, locations=locations) as stream |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
73 |
... for tweet in stream: |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
74 |
... print "Got interesting tweet:", tweet |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
75 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
76 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
77 |
Deprecated classes |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
78 |
------------------ |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
79 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
80 |
tweetstream used to contain the classes TweetStream, FollowStream, TrackStream |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
81 |
LocationStream and ReconnectingTweetStream. These were deprecated when twitter |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
82 |
changed its API end points. The same functionality is now available in |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
83 |
SampleStream and FilterStream. The deprecated methods will emit a warning when |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
84 |
used, but will remain functional for a while longer. |
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
85 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
86 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
87 |
Changelog |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
88 |
--------- |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
89 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
90 |
See the CHANGELOG file |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
91 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
92 |
Contact |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
93 |
------- |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
94 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
95 |
The author is Rune Halvorsen <runefh@gmail.com>. The project resides at |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
96 |
http://bitbucket.org/runeh/tweetstream . If you find bugs, or have feature |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
97 |
requests, please report them in the project site issue tracker. Patches are |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
98 |
also very welcome. |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
99 |
|
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
100 |
Contributors |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
101 |
------------ |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
102 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
103 |
- Rune Halvorsen |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
104 |
- Christopher Schierkolk |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
12
diff
changeset
|
105 |
|
|
12
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
106 |
License |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
107 |
------- |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
108 |
|
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
109 |
This software is licensed under the ``New BSD License``. See the ``LICENCE`` |
|
4daf47fcf792
move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
110 |
file in the top distribution directory for the full license text. |