aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py26
1 files changed, 14 insertions, 12 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 5f1edb3..5461661 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -89,6 +89,7 @@ class Mastodon:
89 """ 89 """
90 __DEFAULT_BASE_URL = 'https://mastodon.social' 90 __DEFAULT_BASE_URL = 'https://mastodon.social'
91 __DEFAULT_TIMEOUT = 300 91 __DEFAULT_TIMEOUT = 300
92 __DEFAULT_STREAM_TIMEOUT = 300
92 __DEFAULT_STREAM_RECONNECT_WAIT_SEC = 5 93 __DEFAULT_STREAM_RECONNECT_WAIT_SEC = 5
93 __SUPPORTED_MASTODON_VERSION = "2.2.0" 94 __SUPPORTED_MASTODON_VERSION = "2.2.0"
94 95
@@ -1388,45 +1389,45 @@ class Mastodon:
1388 # Streaming 1389 # Streaming
1389 ### 1390 ###
1390 @api_version("1.1.0", "1.4.2") 1391 @api_version("1.1.0", "1.4.2")
1391 def stream_user(self, listener, run_async=False, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC): 1392 def stream_user(self, listener, run_async=False, timeout=__DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC):
1392 """ 1393 """
1393 Streams events that are relevant to the authorized user, i.e. home 1394 Streams events that are relevant to the authorized user, i.e. home
1394 timeline and notifications. 1395 timeline and notifications.
1395 """ 1396 """
1396 return self.__stream('/api/v1/streaming/user', listener, run_async=run_async, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) 1397 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)
1397 1398
1398 @api_version("1.1.0", "1.4.2") 1399 @api_version("1.1.0", "1.4.2")
1399 def stream_public(self, listener, run_async=False, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC): 1400 def stream_public(self, listener, run_async=False, timeout=__DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC):
1400 """ 1401 """
1401 Streams public events. 1402 Streams public events.
1402 """ 1403 """
1403 return self.__stream('/api/v1/streaming/public', listener, run_async=run_async, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) 1404 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)
1404 1405
1405 @api_version("1.1.0", "1.4.2") 1406 @api_version("1.1.0", "1.4.2")
1406 def stream_local(self, listener, run_async=False, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC): 1407 def stream_local(self, listener, run_async=False, timeout=__DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC):
1407 """ 1408 """
1408 Streams local public events. 1409 Streams local public events.
1409 """ 1410 """
1410 return self.__stream('/api/v1/streaming/public/local', listener, run_async=run_async, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) 1411 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)
1411 1412
1412 @api_version("1.1.0", "1.4.2") 1413 @api_version("1.1.0", "1.4.2")
1413 def stream_hashtag(self, tag, listener, run_async=False, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC): 1414 def stream_hashtag(self, tag, listener, run_async=False, timeout=__DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC):
1414 """ 1415 """
1415 Stream for all public statuses for the hashtag 'tag' seen by the connected 1416 Stream for all public statuses for the hashtag 'tag' seen by the connected
1416 instance. 1417 instance.
1417 """ 1418 """
1418 if tag.startswith("#"): 1419 if tag.startswith("#"):
1419 raise MastodonIllegalArgumentError("Tag parameter should omit leading #") 1420 raise MastodonIllegalArgumentError("Tag parameter should omit leading #")
1420 return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener, run_async=run_async, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) 1421 return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec)
1421 1422
1422 @api_version("2.1.0", "2.1.0") 1423 @api_version("2.1.0", "2.1.0")
1423 def stream_list(self, id, listener, run_async=False, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC): 1424 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):
1424 """ 1425 """
1425 Stream events for the current user, restricted to accounts on the given 1426 Stream events for the current user, restricted to accounts on the given
1426 list. 1427 list.
1427 """ 1428 """
1428 id = self.__unpack_id(id) 1429 id = self.__unpack_id(id)
1429 return self.__stream("/api/v1/streaming/list?list={}".format(id), listener, run_async=run_async, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) 1430 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)
1430 1431
1431 ### 1432 ###
1432 # Internal helpers, dragons probably 1433 # Internal helpers, dragons probably
@@ -1668,7 +1669,7 @@ class Mastodon:
1668 1669
1669 return response 1670 return response
1670 1671
1671 def __stream(self, endpoint, listener, params={}, run_async=False, reconnect_async=False, reconnect_async_wait_sec=5): 1672 def __stream(self, endpoint, listener, params={}, run_async=False, timeout=__DEFAULT_STREAM_TIMEOUT, reconnect_async=False, reconnect_async_wait_sec=__DEFAULT_STREAM_RECONNECT_WAIT_SEC):
1672 """ 1673 """
1673 Internal streaming API helper. 1674 Internal streaming API helper.
1674 1675
@@ -1700,7 +1701,8 @@ class Mastodon:
1700 # Connect function (called and then potentially passed to async handler) 1701 # Connect function (called and then potentially passed to async handler)
1701 def connect_func(): 1702 def connect_func():
1702 headers = {"Authorization": "Bearer " + self.access_token} 1703 headers = {"Authorization": "Bearer " + self.access_token}
1703 connection = requests.get(url + endpoint, headers = headers, data = params, stream = True) 1704 connection = requests.get(url + endpoint, headers = headers, data = params, stream = True,
1705 timeout=(self.request_timeout, timeout))
1704 1706
1705 if connection.status_code != 200: 1707 if connection.status_code != 200:
1706 raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) 1708 raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason)
Powered by cgit v1.2.3 (git 2.41.0)