| author | Raphael Velt <raph.velt@gmail.com> |
| Tue, 17 Apr 2012 14:56:03 +0200 | |
| changeset 588 | 3bc8d732314e |
| parent 527 | 80e5b9543cac |
| permissions | -rw-r--r-- |
|
207
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
1 |
from .streamclasses import FilterStream, SampleStream, ConnectionError |
|
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
2 |
import time |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
3 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
4 |
class DeprecatedStream(FilterStream): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
5 |
def __init__(self, *args, **kwargs): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
6 |
import warnings |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
7 |
warnings.warn("%s is deprecated. Use FilterStream instead" % self.__class__.__name__, DeprecationWarning) |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
8 |
super(DeprecatedStream, self).__init__(*args, **kwargs) |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
9 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
10 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
11 |
class FollowStream(DeprecatedStream): |
|
207
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
12 |
def __init__(self, auth, follow, catchup=None, url=None): |
|
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
13 |
super(FollowStream, self).__init__(auth, follow=follow, catchup=catchup, url=url) |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
14 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
15 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
16 |
class TrackStream(DeprecatedStream): |
|
527
80e5b9543cac
Merge with 9d7218514a08006f925f138df88d770b29fae4cc
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
diff
changeset
|
17 |
def __init__(self, auth, track, catchup=None, url=None, slow=False): |
|
207
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
18 |
super(TrackStream, self).__init__(auth, track=track, catchup=catchup, url=url) |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
19 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
20 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
21 |
class LocationStream(DeprecatedStream): |
|
527
80e5b9543cac
Merge with 9d7218514a08006f925f138df88d770b29fae4cc
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
diff
changeset
|
22 |
def __init__(self, auth, locations, catchup=None, url=None, slow=False): |
|
207
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
23 |
super(LocationStream, self).__init__(auth, locations=locations, catchup=catchup, url=url) |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
24 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
25 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
26 |
class TweetStream(SampleStream): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
27 |
def __init__(self, *args, **kwargs): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
28 |
import warnings |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
29 |
warnings.warn("%s is deprecated. Use SampleStream instead" % self.__class__.__name__, DeprecationWarning) |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
30 |
SampleStream.__init__(self, *args, **kwargs) |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
31 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
32 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
33 |
class ReconnectingTweetStream(TweetStream): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
34 |
"""TweetStream class that automatically tries to reconnect if the |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
35 |
connecting goes down. Reconnecting, and waiting for reconnecting, is |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
36 |
blocking. |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
37 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
38 |
:param username: See :TweetStream: |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
39 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
40 |
:param password: See :TweetStream: |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
41 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
42 |
:keyword url: See :TweetStream: |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
43 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
44 |
:keyword reconnects: Number of reconnects before a ConnectionError is |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
45 |
raised. Default is 3 |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
46 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
47 |
:error_cb: Optional callable that will be called just before trying to |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
48 |
reconnect. The callback will be called with a single argument, the |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
49 |
exception that caused the reconnect attempt. Default is None |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
50 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
51 |
:retry_wait: Time to wait before reconnecting in seconds. Default is 5 |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
52 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
53 |
""" |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
54 |
|
|
207
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
55 |
def __init__(self, auth, url="sample", |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
56 |
reconnects=3, error_cb=None, retry_wait=5): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
57 |
self.max_reconnects = reconnects |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
58 |
self.retry_wait = retry_wait |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
59 |
self._reconnects = 0 |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
60 |
self._error_cb = error_cb |
|
207
621fa6caec0c
Merge with f093196961e770387cc7cd3e11f2b7c0881b003b
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
13
diff
changeset
|
61 |
TweetStream.__init__(self, auth, url=url) |
|
13
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
62 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
63 |
def next(self): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
64 |
while True: |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
65 |
try: |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
66 |
return TweetStream.next(self) |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
67 |
except ConnectionError, e: |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
68 |
self._reconnects += 1 |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
69 |
if self._reconnects > self.max_reconnects: |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
70 |
raise ConnectionError("Too many retries") |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
71 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
72 |
# Note: error_cb is not called on the last error since we |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
73 |
# raise a ConnectionError instead |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
74 |
if callable(self._error_cb): |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
75 |
self._error_cb(e) |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
76 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
77 |
time.sleep(self.retry_wait) |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
78 |
# Don't listen to auth error, since we can't reasonably reconnect |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
79 |
# when we get one. |
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
80 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
81 |
|
|
79b6e132e3d7
upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
82 |