diff options
Diffstat (limited to 'mastodon/Mastodon.py')
-rw-r--r-- | mastodon/Mastodon.py | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index d2d634e..88f4906 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -17,10 +17,11 @@ import re | |||
17 | import copy | 17 | import copy |
18 | import threading | 18 | import threading |
19 | import sys | 19 | import sys |
20 | |||
20 | try: | 21 | try: |
21 | from urllib.parse import urlparse | 22 | from urllib.parse import urlparse |
22 | except ImportError: | 23 | except ImportError: |
23 | from urlparse import urlparse | 24 | from urlparse import urlparse |
24 | 25 | ||
25 | 26 | ||
26 | class Mastodon: | 27 | class Mastodon: |
@@ -1009,57 +1010,34 @@ class Mastodon: | |||
1009 | ### | 1010 | ### |
1010 | # Streaming | 1011 | # Streaming |
1011 | ### | 1012 | ### |
1012 | def user_stream(self, listener, async=False): | 1013 | def stream_user(self, listener, async=False): |
1013 | """ | 1014 | """ |
1014 | Streams events that are relevant to the authorized user, i.e. home | 1015 | Streams events that are relevant to the authorized user, i.e. home |
1015 | timeline and notifications. 'listener' should be a subclass of | 1016 | timeline and notifications. 'listener' should be a subclass of |
1016 | StreamListener which will receive callbacks for incoming events. | 1017 | StreamListener which will receive callbacks for incoming events. |
1017 | |||
1018 | If async is False, this method blocks forever. | ||
1019 | |||
1020 | If async is True, 'listener' will listen on another thread and this method | ||
1021 | will return a handle corresponding to the open connection. The | ||
1022 | connection may be closed at any time by calling its close() method. | ||
1023 | """ | 1018 | """ |
1024 | return self.__stream('/api/v1/streaming/user', listener, async=async) | 1019 | return self.__stream('/api/v1/streaming/user', listener, async=async) |
1025 | 1020 | ||
1026 | def public_stream(self, listener, async=False): | 1021 | def stream_public(self, listener, async=False): |
1027 | """ | 1022 | """ |
1028 | Streams public events. 'listener' should be a subclass of StreamListener | 1023 | Streams public events. 'listener' should be a subclass of StreamListener |
1029 | which will receive callbacks for incoming events. | 1024 | which will receive callbacks for incoming events. |
1030 | |||
1031 | If async is False, this method blocks forever. | ||
1032 | |||
1033 | If async is True, 'listener' will listen on another thread and this method | ||
1034 | will return a handle corresponding to the open connection. The | ||
1035 | connection may be closed at any time by calling its close() method. | ||
1036 | """ | 1025 | """ |
1037 | return self.__stream('/api/v1/streaming/public', listener, async=async) | 1026 | return self.__stream('/api/v1/streaming/public', listener, async=async) |
1038 | 1027 | ||
1039 | def local_stream(self, listener, async=False): | 1028 | def stream_local(self, listener, async=False): |
1040 | """ | 1029 | """ |
1041 | Streams local events. 'listener' should be a subclass of StreamListener | 1030 | Streams local events. 'listener' should be a subclass of StreamListener |
1042 | which will receive callbacks for incoming events. | 1031 | which will receive callbacks for incoming events. |
1043 | 1032 | ||
1044 | If async is False, this method blocks forever. | ||
1045 | |||
1046 | If async is True, 'listener' will listen on another thread and this method | ||
1047 | will return a handle corresponding to the open connection. The | ||
1048 | connection may be closed at any time by calling its close() method. | ||
1049 | """ | 1033 | """ |
1050 | return self.__stream('/api/v1/streaming/public/local', listener, async=async) | 1034 | return self.__stream('/api/v1/streaming/public/local', listener, async=async) |
1051 | 1035 | ||
1052 | def hashtag_stream(self, tag, listener, async=False): | 1036 | def stream_hashtag(self, tag, listener, async=False): |
1053 | """ | 1037 | """ |
1054 | Returns all public statuses for the hashtag 'tag'. 'listener' should be | 1038 | Returns all public statuses for the hashtag 'tag'. 'listener' should be |
1055 | a subclass of StreamListener which will receive callbacks for incoming | 1039 | a subclass of StreamListener which will receive callbacks for incoming |
1056 | events. | 1040 | events. |
1057 | |||
1058 | If async is False, this method blocks forever. | ||
1059 | |||
1060 | If async is True, 'listener' will listen on another thread and this method | ||
1061 | will return a handle corresponding to the open connection. The | ||
1062 | connection may be closed at any time by calling its close() method. | ||
1063 | """ | 1041 | """ |
1064 | return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener) | 1042 | return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener) |
1065 | 1043 | ||
@@ -1083,8 +1061,8 @@ class Mastodon: | |||
1083 | 1061 | ||
1084 | return (date_time_utc - epoch_utc).total_seconds() | 1062 | return (date_time_utc - epoch_utc).total_seconds() |
1085 | 1063 | ||
1086 | 1064 | @staticmethod | |
1087 | def __json_date_parse(self, json_object): | 1065 | def __json_date_parse(json_object): |
1088 | """ | 1066 | """ |
1089 | Parse dates in certain known json fields, if possible. | 1067 | Parse dates in certain known json fields, if possible. |
1090 | """ | 1068 | """ |
@@ -1100,7 +1078,8 @@ class Mastodon: | |||
1100 | raise MastodonAPIError('Encountered invalid date.') | 1078 | raise MastodonAPIError('Encountered invalid date.') |
1101 | return json_object | 1079 | return json_object |
1102 | 1080 | ||
1103 | def __json_id_to_bignum(self, json_object): | 1081 | @staticmethod |
1082 | def __json_id_to_bignum(json_object): | ||
1104 | """ | 1083 | """ |
1105 | Converts json string IDs to native python bignums. | 1084 | Converts json string IDs to native python bignums. |
1106 | """ | 1085 | """ |
@@ -1117,10 +1096,11 @@ class Mastodon: | |||
1117 | pass | 1096 | pass |
1118 | 1097 | ||
1119 | return json_object | 1098 | return json_object |
1120 | 1099 | ||
1121 | def __json_hooks(self, json_object): | 1100 | @staticmethod |
1122 | json_object = self.__json_date_parse(json_object) | 1101 | def __json_hooks(json_object): |
1123 | json_object = self.__json_id_to_bignum(json_object) | 1102 | json_object = Mastodon.__json_date_parse(json_object) |
1103 | json_object = Mastodon.__json_id_to_bignum(json_object) | ||
1124 | return json_object | 1104 | return json_object |
1125 | 1105 | ||
1126 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): | 1106 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): |
@@ -1424,6 +1404,7 @@ class MastodonError(Exception): | |||
1424 | 1404 | ||
1425 | 1405 | ||
1426 | class MastodonIllegalArgumentError(ValueError, MastodonError): | 1406 | class MastodonIllegalArgumentError(ValueError, MastodonError): |
1407 | """Raised when an incorrect parameter is passed to a function""" | ||
1427 | pass | 1408 | pass |
1428 | 1409 | ||
1429 | 1410 | ||
@@ -1432,16 +1413,24 @@ class MastodonIOError(IOError, MastodonError): | |||
1432 | 1413 | ||
1433 | 1414 | ||
1434 | class MastodonFileNotFoundError(MastodonIOError): | 1415 | class MastodonFileNotFoundError(MastodonIOError): |
1416 | """Raised when a file requested to be loaded can not be opened""" | ||
1435 | pass | 1417 | pass |
1436 | 1418 | ||
1437 | 1419 | ||
1438 | class MastodonNetworkError(MastodonIOError): | 1420 | class MastodonNetworkError(MastodonIOError): |
1421 | """Raised when network communication with the server fails""" | ||
1439 | pass | 1422 | pass |
1440 | 1423 | ||
1441 | 1424 | ||
1442 | class MastodonAPIError(MastodonError): | 1425 | class MastodonAPIError(MastodonError): |
1426 | """Raised when the mastodon API generates a response that cannot be handled""" | ||
1443 | pass | 1427 | pass |
1444 | 1428 | ||
1445 | 1429 | ||
1446 | class MastodonRatelimitError(MastodonError): | 1430 | class MastodonRatelimitError(MastodonError): |
1431 | """Raised when rate limiting is set to manual mode and the rate limit is exceeded""" | ||
1432 | pass | ||
1433 | |||
1434 | class MastodonMalformedEventError(MastodonError): | ||
1435 | """Raised when the server-sent event stream is malformed""" | ||
1447 | pass | 1436 | pass |