diff options
author | Will Thompson <[email protected]> | 2017-04-09 10:21:56 +0100 |
---|---|---|
committer | Will Thompson <[email protected]> | 2017-04-10 08:18:08 +0100 |
commit | ab60931620066f6704be3010903f779b3cb9c71a (patch) | |
tree | 86211f5f6d5ca7ffb495d48ce98c6d71c30e100b /mastodon/Mastodon.py | |
parent | 280c60120beb13d00c807c418c765b93da248b19 (diff) | |
download | mastodon.py-ab60931620066f6704be3010903f779b3cb9c71a.tar.gz |
Initial implementation of streaming API
This is missing any error handling and rate-limiting around the stream
itself, but once the stream is established, the full range of events are
supported.
Fixes issue #14.
Diffstat (limited to 'mastodon/Mastodon.py')
-rw-r--r-- | mastodon/Mastodon.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 9967bdb..493fb40 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -12,6 +12,9 @@ import datetime | |||
12 | import dateutil | 12 | import dateutil |
13 | import dateutil.parser | 13 | import dateutil.parser |
14 | 14 | ||
15 | from contextlib import closing | ||
16 | |||
17 | |||
15 | class Mastodon: | 18 | class Mastodon: |
16 | """ | 19 | """ |
17 | Super basic but thorough and easy to use mastodon.social | 20 | Super basic but thorough and easy to use mastodon.social |
@@ -578,6 +581,37 @@ class Mastodon: | |||
578 | media_file_description = (file_name, media_file, mime_type) | 581 | media_file_description = (file_name, media_file, mime_type) |
579 | return self.__api_request('POST', '/api/v1/media', files = {'file': media_file_description}) | 582 | return self.__api_request('POST', '/api/v1/media', files = {'file': media_file_description}) |
580 | 583 | ||
584 | def user_stream(self, listener): | ||
585 | """ | ||
586 | Streams events that are relevant to the authorized user, i.e. home | ||
587 | timeline and notifications. 'listener' should be a subclass of | ||
588 | StreamListener. | ||
589 | |||
590 | This method blocks forever, calling callbacks on 'listener' for | ||
591 | incoming events. | ||
592 | """ | ||
593 | return self.__stream('/api/v1/streaming/user', listener) | ||
594 | |||
595 | def public_stream(self, listener): | ||
596 | """ | ||
597 | Streams public events. 'listener' should be a subclass of | ||
598 | StreamListener. | ||
599 | |||
600 | This method blocks forever, calling callbacks on 'listener' for | ||
601 | incoming events. | ||
602 | """ | ||
603 | return self.__stream('/api/v1/streaming/public', listener) | ||
604 | |||
605 | def hashtag_stream(self, tag, listener): | ||
606 | """ | ||
607 | Returns all public statuses for the hashtag 'tag'. 'listener' should be | ||
608 | a subclass of StreamListener. | ||
609 | |||
610 | This method blocks forever, calling callbacks on 'listener' for | ||
611 | incoming events. | ||
612 | """ | ||
613 | return self.__stream('/api/v1/streaming/hashtag', listener, params={'tag': tag}) | ||
614 | |||
581 | ### | 615 | ### |
582 | # Internal helpers, dragons probably | 616 | # Internal helpers, dragons probably |
583 | ### | 617 | ### |
@@ -710,6 +744,20 @@ class Mastodon: | |||
710 | 744 | ||
711 | return response | 745 | return response |
712 | 746 | ||
747 | def __stream(self, endpoint, listener, params = {}): | ||
748 | """ | ||
749 | Internal streaming API helper. | ||
750 | """ | ||
751 | |||
752 | headers = {} | ||
753 | if self.access_token != None: | ||
754 | headers = {'Authorization': 'Bearer ' + self.access_token} | ||
755 | |||
756 | url = self.api_base_url + endpoint | ||
757 | with closing(requests.get(url, headers = headers, data = params, stream = True)) as r: | ||
758 | listener.handle_stream(r.iter_lines()) | ||
759 | |||
760 | |||
713 | def __generate_params(self, params, exclude = []): | 761 | def __generate_params(self, params, exclude = []): |
714 | """ | 762 | """ |
715 | Internal named-parameters-to-dict helper. | 763 | Internal named-parameters-to-dict helper. |