diff options
Diffstat (limited to 'mastodon/streaming_endpoints.py')
-rw-r--r-- | mastodon/streaming_endpoints.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/mastodon/streaming_endpoints.py b/mastodon/streaming_endpoints.py new file mode 100644 index 0000000..9ff72f5 --- /dev/null +++ b/mastodon/streaming_endpoints.py | |||
@@ -0,0 +1,75 @@ | |||
1 | # relationships.py - endpoints for user and domain blocks and mutes as well as follow requests | ||
2 | |||
3 | from .versions import _DICT_VERSION_STATUS | ||
4 | from .errors import MastodonIllegalArgumentError | ||
5 | from .defaults import _DEFAULT_STREAM_TIMEOUT, _DEFAULT_STREAM_RECONNECT_WAIT_SEC | ||
6 | from .utility import api_version | ||
7 | |||
8 | from .internals import Mastodon as Internals | ||
9 | |||
10 | class Mastodon(Internals): | ||
11 | ### | ||
12 | # Streaming | ||
13 | ### | ||
14 | @api_version("1.1.0", "1.4.2", _DICT_VERSION_STATUS) | ||
15 | def stream_user(self, listener, run_async=False, timeout=_DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=_DEFAULT_STREAM_RECONNECT_WAIT_SEC): | ||
16 | """ | ||
17 | Streams events that are relevant to the authorized user, i.e. home | ||
18 | timeline and notifications. | ||
19 | """ | ||
20 | return self.__stream('/api/v1/streaming/user', listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) | ||
21 | |||
22 | @api_version("1.1.0", "1.4.2", _DICT_VERSION_STATUS) | ||
23 | def stream_public(self, listener, run_async=False, timeout=_DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=_DEFAULT_STREAM_RECONNECT_WAIT_SEC): | ||
24 | """ | ||
25 | Streams public events. | ||
26 | """ | ||
27 | return self.__stream('/api/v1/streaming/public', listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) | ||
28 | |||
29 | @api_version("1.1.0", "1.4.2", _DICT_VERSION_STATUS) | ||
30 | def stream_local(self, listener, run_async=False, timeout=_DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=_DEFAULT_STREAM_RECONNECT_WAIT_SEC): | ||
31 | """ | ||
32 | Streams local public events. | ||
33 | """ | ||
34 | return self.__stream('/api/v1/streaming/public/local', listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) | ||
35 | |||
36 | @api_version("1.1.0", "1.4.2", _DICT_VERSION_STATUS) | ||
37 | def stream_hashtag(self, tag, listener, local=False, run_async=False, timeout=_DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=_DEFAULT_STREAM_RECONNECT_WAIT_SEC): | ||
38 | """ | ||
39 | Stream for all public statuses for the hashtag 'tag' seen by the connected | ||
40 | instance. | ||
41 | |||
42 | Set local to True to only get local statuses. | ||
43 | """ | ||
44 | if tag.startswith("#"): | ||
45 | raise MastodonIllegalArgumentError("Tag parameter should omit leading #") | ||
46 | base = '/api/v1/streaming/hashtag' | ||
47 | if local: | ||
48 | base += '/local' | ||
49 | return self.__stream("{}?tag={}".format(base, tag), listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) | ||
50 | |||
51 | @api_version("2.1.0", "2.1.0", _DICT_VERSION_STATUS) | ||
52 | def stream_list(self, id, listener, run_async=False, timeout=_DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=_DEFAULT_STREAM_RECONNECT_WAIT_SEC): | ||
53 | """ | ||
54 | Stream events for the current user, restricted to accounts on the given | ||
55 | list. | ||
56 | """ | ||
57 | id = self.__unpack_id(id) | ||
58 | return self.__stream("/api/v1/streaming/list?list={}".format(id), listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) | ||
59 | |||
60 | @api_version("2.6.0", "2.6.0", _DICT_VERSION_STATUS) | ||
61 | def stream_direct(self, listener, run_async=False, timeout=_DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=_DEFAULT_STREAM_RECONNECT_WAIT_SEC): | ||
62 | """ | ||
63 | Streams direct message events for the logged-in user, as conversation events. | ||
64 | """ | ||
65 | return self.__stream('/api/v1/streaming/direct', listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) | ||
66 | |||
67 | @api_version("2.5.0", "2.5.0", "2.5.0") | ||
68 | def stream_healthy(self): | ||
69 | """ | ||
70 | Returns without True if streaming API is okay, False or raises an error otherwise. | ||
71 | """ | ||
72 | api_okay = self.__api_request('GET', '/api/v1/streaming/health', base_url_override=self.__get_streaming_base(), parse=False) | ||
73 | if api_okay in [b'OK', b'success']: | ||
74 | return True | ||
75 | return False | ||