aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py61
1 files changed, 33 insertions, 28 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 819d252..620b303 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -108,9 +108,9 @@ class Mastodon:
108 self._token_expired = datetime.datetime.now() 108 self._token_expired = datetime.datetime.now()
109 self._refresh_token = None 109 self._refresh_token = None
110 110
111 self.ratelimit_limit = 300 111 self.ratelimit_limit = 150
112 self.ratelimit_reset = time.time() 112 self.ratelimit_reset = time.time()
113 self.ratelimit_remaining = 300 113 self.ratelimit_remaining = 150
114 self.ratelimit_lastcall = time.time() 114 self.ratelimit_lastcall = time.time()
115 self.ratelimit_pacefactor = ratelimit_pacefactor 115 self.ratelimit_pacefactor = ratelimit_pacefactor
116 116
@@ -828,9 +828,8 @@ class Mastodon:
828 If async is False, this method blocks forever. 828 If async is False, this method blocks forever.
829 829
830 If async is True, 'listener' will listen on another thread and this method 830 If async is True, 'listener' will listen on another thread and this method
831 will return a requests.Response instance corresponding to the open 831 will return a handle corresponding to the open connection. The
832 connection. The connection may be closed at any time by calling its 832 connection may be closed at any time by calling its close() method.
833 close() method.
834 """ 833 """
835 return self.__stream('/api/v1/streaming/user', listener, async=async) 834 return self.__stream('/api/v1/streaming/user', listener, async=async)
836 835
@@ -842,9 +841,8 @@ class Mastodon:
842 If async is False, this method blocks forever. 841 If async is False, this method blocks forever.
843 842
844 If async is True, 'listener' will listen on another thread and this method 843 If async is True, 'listener' will listen on another thread and this method
845 will return a requests.Response instance corresponding to the open 844 will return a handle corresponding to the open connection. The
846 connection. The connection may be closed at any time by calling its 845 connection may be closed at any time by calling its close() method.
847 close() method.
848 """ 846 """
849 return self.__stream('/api/v1/streaming/public', listener, async=async) 847 return self.__stream('/api/v1/streaming/public', listener, async=async)
850 848
@@ -856,9 +854,8 @@ class Mastodon:
856 If async is False, this method blocks forever. 854 If async is False, this method blocks forever.
857 855
858 If async is True, 'listener' will listen on another thread and this method 856 If async is True, 'listener' will listen on another thread and this method
859 will return a requests.Response instance corresponding to the open 857 will return a handle corresponding to the open connection. The
860 connection. The connection may be closed at any time by calling its 858 connection may be closed at any time by calling its close() method.
861 close() method.
862 """ 859 """
863 return self.__stream('/api/v1/streaming/public/local', listener, async=async) 860 return self.__stream('/api/v1/streaming/public/local', listener, async=async)
864 861
@@ -871,9 +868,8 @@ class Mastodon:
871 If async is False, this method blocks forever. 868 If async is False, this method blocks forever.
872 869
873 If async is True, 'listener' will listen on another thread and this method 870 If async is True, 'listener' will listen on another thread and this method
874 will return a requests.Response instance corresponding to the open 871 will return a handle corresponding to the open connection. The
875 connection. The connection may be closed at any time by calling its 872 connection may be closed at any time by calling its close() method.
876 close() method.
877 """ 873 """
878 return self.__stream('/api/v1/streaming/hashtag', listener, params={'tag': tag}, async=async) 874 return self.__stream('/api/v1/streaming/hashtag', listener, params={'tag': tag}, async=async)
879 875
@@ -1040,8 +1036,8 @@ class Mastodon:
1040 """ 1036 """
1041 Internal streaming API helper. 1037 Internal streaming API helper.
1042 1038
1043 Returns the requests.Response instance corresponding to the open websocket 1039 Returns a handle to the open connection that the user can close if they
1044 connection. 1040 wish to terminate it.
1045 """ 1041 """
1046 1042
1047 headers = {} 1043 headers = {}
@@ -1051,23 +1047,32 @@ class Mastodon:
1051 1047
1052 connection = requests.get(url, headers = headers, data = params, stream = True) 1048 connection = requests.get(url, headers = headers, data = params, stream = True)
1053 1049
1054 def __stream_threadproc(): 1050 class __stream_handle():
1055 with closing(connection) as r: 1051 def __init__(self, connection):
1056 try: 1052 self.connection = connection
1057 listener.handle_stream(r.iter_lines()) 1053
1058 except AttributeError as e: 1054 def close(self):
1059 # TODO If the user closes the connection early, requests gets 1055 self.connection.close()
1060 # confused and throws an AttributeError 1056
1061 pass 1057 def _threadproc(self):
1062 return 0 1058 with closing(connection) as r:
1059 try:
1060 listener.handle_stream(r.iter_lines())
1061 except AttributeError as e:
1062 # Eat AttributeError from requests if user closes early
1063 pass
1064 return 0
1065
1066 handle = __stream_handle(connection)
1063 1067
1064 if async: 1068 if async:
1065 t = threading.Thread(args=(), target=__stream_threadproc) 1069 t = threading.Thread(args=(), target=handle._threadproc)
1066 t.start() 1070 t.start()
1067 return connection 1071 return handle
1068 else: 1072 else:
1069 # Blocking, never returns (can only leave via exception) 1073 # Blocking, never returns (can only leave via exception)
1070 return __stream_threadproc() 1074 with closing(connection) as r:
1075 listener.handle_stream(r.iter_lines())
1071 1076
1072 def __generate_params(self, params, exclude = []): 1077 def __generate_params(self, params, exclude = []):
1073 """ 1078 """
Powered by cgit v1.2.3 (git 2.41.0)