aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon/Mastodon.py')
-rw-r--r--mastodon/Mastodon.py50
1 files changed, 47 insertions, 3 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index f3861c2..ee11496 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -9,10 +9,9 @@ import time
9import random 9import random
10import string 10import string
11import datetime 11import datetime
12import dateutil.parser
13
14import pytz
15import dateutil 12import dateutil
13import dateutil.parser
14from contextlib import closing
16import requests 15import requests
17 16
18class Mastodon: 17class Mastodon:
@@ -658,6 +657,37 @@ class Mastodon:
658 media_file_description = (file_name, media_file, mime_type) 657 media_file_description = (file_name, media_file, mime_type)
659 return self.__api_request('POST', '/api/v1/media', files = {'file': media_file_description}) 658 return self.__api_request('POST', '/api/v1/media', files = {'file': media_file_description})
660 659
660 def user_stream(self, listener):
661 """
662 Streams events that are relevant to the authorized user, i.e. home
663 timeline and notifications. 'listener' should be a subclass of
664 StreamListener.
665
666 This method blocks forever, calling callbacks on 'listener' for
667 incoming events.
668 """
669 return self.__stream('/api/v1/streaming/user', listener)
670
671 def public_stream(self, listener):
672 """
673 Streams public events. 'listener' should be a subclass of
674 StreamListener.
675
676 This method blocks forever, calling callbacks on 'listener' for
677 incoming events.
678 """
679 return self.__stream('/api/v1/streaming/public', listener)
680
681 def hashtag_stream(self, tag, listener):
682 """
683 Returns all public statuses for the hashtag 'tag'. 'listener' should be
684 a subclass of StreamListener.
685
686 This method blocks forever, calling callbacks on 'listener' for
687 incoming events.
688 """
689 return self.__stream('/api/v1/streaming/hashtag', listener, params={'tag': tag})
690
661 ### 691 ###
662 # Internal helpers, dragons probably 692 # Internal helpers, dragons probably
663 ### 693 ###
@@ -790,6 +820,20 @@ class Mastodon:
790 820
791 return response 821 return response
792 822
823 def __stream(self, endpoint, listener, params = {}):
824 """
825 Internal streaming API helper.
826 """
827
828 headers = {}
829 if self.access_token != None:
830 headers = {'Authorization': 'Bearer ' + self.access_token}
831
832 url = self.api_base_url + endpoint
833 with closing(requests.get(url, headers = headers, data = params, stream = True)) as r:
834 listener.handle_stream(r.iter_lines())
835
836
793 def __generate_params(self, params, exclude = []): 837 def __generate_params(self, params, exclude = []):
794 """ 838 """
795 Internal named-parameters-to-dict helper. 839 Internal named-parameters-to-dict helper.
Powered by cgit v1.2.3 (git 2.41.0)