diff options
-rw-r--r-- | mastodon/Mastodon.py | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 7a1d701..b118421 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -16,7 +16,10 @@ import dateutil.parser | |||
16 | import re | 16 | import re |
17 | import copy | 17 | import copy |
18 | import threading | 18 | import threading |
19 | from urllib.parse import urlparse | 19 | try: |
20 | from urllib.parse import urlparse | ||
21 | except ImportError: | ||
22 | from urlparse import urlparse | ||
20 | 23 | ||
21 | 24 | ||
22 | class Mastodon: | 25 | class Mastodon: |
@@ -569,7 +572,7 @@ class Mastodon: | |||
569 | def toot(self, status): | 572 | def toot(self, status): |
570 | """ | 573 | """ |
571 | Synonym for status_post that only takes the status text as input. | 574 | Synonym for status_post that only takes the status text as input. |
572 | 575 | ||
573 | Usage in production code is not recommended. | 576 | Usage in production code is not recommended. |
574 | 577 | ||
575 | Returns a toot dict with the new status. | 578 | Returns a toot dict with the new status. |
@@ -901,7 +904,7 @@ class Mastodon: | |||
901 | 904 | ||
902 | If async is False, this method blocks forever. | 905 | If async is False, this method blocks forever. |
903 | 906 | ||
904 | If async is True, 'listener' will listen on another thread and this method | 907 | If async is True, 'listener' will listen on another thread and this method |
905 | will return a handle corresponding to the open connection. The | 908 | will return a handle corresponding to the open connection. The |
906 | connection may be closed at any time by calling its close() method. | 909 | connection may be closed at any time by calling its close() method. |
907 | """ | 910 | """ |
@@ -914,7 +917,7 @@ class Mastodon: | |||
914 | 917 | ||
915 | If async is False, this method blocks forever. | 918 | If async is False, this method blocks forever. |
916 | 919 | ||
917 | If async is True, 'listener' will listen on another thread and this method | 920 | If async is True, 'listener' will listen on another thread and this method |
918 | will return a handle corresponding to the open connection. The | 921 | will return a handle corresponding to the open connection. The |
919 | connection may be closed at any time by calling its close() method. | 922 | connection may be closed at any time by calling its close() method. |
920 | """ | 923 | """ |
@@ -927,7 +930,7 @@ class Mastodon: | |||
927 | 930 | ||
928 | If async is False, this method blocks forever. | 931 | If async is False, this method blocks forever. |
929 | 932 | ||
930 | If async is True, 'listener' will listen on another thread and this method | 933 | If async is True, 'listener' will listen on another thread and this method |
931 | will return a handle corresponding to the open connection. The | 934 | will return a handle corresponding to the open connection. The |
932 | connection may be closed at any time by calling its close() method. | 935 | connection may be closed at any time by calling its close() method. |
933 | """ | 936 | """ |
@@ -941,12 +944,12 @@ class Mastodon: | |||
941 | 944 | ||
942 | If async is False, this method blocks forever. | 945 | If async is False, this method blocks forever. |
943 | 946 | ||
944 | If async is True, 'listener' will listen on another thread and this method | 947 | If async is True, 'listener' will listen on another thread and this method |
945 | will return a handle corresponding to the open connection. The | 948 | will return a handle corresponding to the open connection. The |
946 | connection may be closed at any time by calling its close() method. | 949 | connection may be closed at any time by calling its close() method. |
947 | """ | 950 | """ |
948 | return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener) | 951 | return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener) |
949 | 952 | ||
950 | ### | 953 | ### |
951 | # Internal helpers, dragons probably | 954 | # Internal helpers, dragons probably |
952 | ### | 955 | ### |
@@ -982,7 +985,7 @@ class Mastodon: | |||
982 | except: | 985 | except: |
983 | raise MastodonAPIError('Encountered invalid date.') | 986 | raise MastodonAPIError('Encountered invalid date.') |
984 | return json_object | 987 | return json_object |
985 | 988 | ||
986 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): | 989 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): |
987 | """ | 990 | """ |
988 | Internal API request helper. | 991 | Internal API request helper. |
@@ -1027,8 +1030,8 @@ class Mastodon: | |||
1027 | response_object = None | 1030 | response_object = None |
1028 | try: | 1031 | try: |
1029 | if method == 'GET': | 1032 | if method == 'GET': |
1030 | response_object = requests.get(self.api_base_url + endpoint, params=params, | 1033 | response_object = requests.get(self.api_base_url + endpoint, params=params, |
1031 | headers=headers, files=files, | 1034 | headers=headers, files=files, |
1032 | timeout=self.request_timeout) | 1035 | timeout=self.request_timeout) |
1033 | if method == 'POST': | 1036 | if method == 'POST': |
1034 | response_object = requests.post(self.api_base_url + endpoint, data=params, headers=headers, | 1037 | response_object = requests.post(self.api_base_url + endpoint, data=params, headers=headers, |
@@ -1077,13 +1080,13 @@ class Mastodon: | |||
1077 | response = response_object.json() | 1080 | response = response_object.json() |
1078 | except: | 1081 | except: |
1079 | raise MastodonAPIError('Endpoint not found.') | 1082 | raise MastodonAPIError('Endpoint not found.') |
1080 | 1083 | ||
1081 | if isinstance(response, dict) and 'error' in response: | 1084 | if isinstance(response, dict) and 'error' in response: |
1082 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) | 1085 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) |
1083 | else: | 1086 | else: |
1084 | raise MastodonAPIError('Endpoint not found.') | 1087 | raise MastodonAPIError('Endpoint not found.') |
1085 | 1088 | ||
1086 | 1089 | ||
1087 | if response_object.status_code == 500: | 1090 | if response_object.status_code == 500: |
1088 | raise MastodonAPIError('General API problem.') | 1091 | raise MastodonAPIError('General API problem.') |
1089 | 1092 | ||
@@ -1107,11 +1110,11 @@ class Mastodon: | |||
1107 | "Could not parse response as JSON, response code was %s, " | 1110 | "Could not parse response as JSON, response code was %s, " |
1108 | "bad json content was '%s'" % (response_object.status_code, | 1111 | "bad json content was '%s'" % (response_object.status_code, |
1109 | response_object.content)) | 1112 | response_object.content)) |
1110 | 1113 | ||
1111 | # See if the returned dict is an error dict even though status is 200 | 1114 | # See if the returned dict is an error dict even though status is 200 |
1112 | if isinstance(response, dict) and 'error' in response: | 1115 | if isinstance(response, dict) and 'error' in response: |
1113 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) | 1116 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) |
1114 | 1117 | ||
1115 | # Parse link headers | 1118 | # Parse link headers |
1116 | if isinstance(response, list) and \ | 1119 | if isinstance(response, list) and \ |
1117 | 'Link' in response_object.headers and \ | 1120 | 'Link' in response_object.headers and \ |