diff options
author | Lorenz Diener <[email protected]> | 2017-10-16 11:19:05 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2017-10-16 11:19:05 +0200 |
commit | 186b7135ff970feebe9442f813e7ccd1c414cdc1 (patch) | |
tree | b5e573b9fbea9bc5ff1cb7f1c16d2814c8a4e8b0 /mastodon | |
parent | b18b6f201b75f704655e0784930a74050fb196b9 (diff) | |
parent | 9a7efa365762347e78e7b0e26ea909958ce63103 (diff) | |
download | mastodon.py-186b7135ff970feebe9442f813e7ccd1c414cdc1.tar.gz |
Merge pull request #96 from ragingscholar/master
Use urlparse instead of urllib.parse for python2.7
Diffstat (limited to 'mastodon')
-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 5b198a5..6ffb0e4 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, |
@@ -1058,13 +1061,13 @@ class Mastodon: | |||
1058 | response = response_object.json() | 1061 | response = response_object.json() |
1059 | except: | 1062 | except: |
1060 | raise MastodonAPIError('Endpoint not found.') | 1063 | raise MastodonAPIError('Endpoint not found.') |
1061 | 1064 | ||
1062 | if isinstance(response, dict) and 'error' in response: | 1065 | if isinstance(response, dict) and 'error' in response: |
1063 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) | 1066 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) |
1064 | else: | 1067 | else: |
1065 | raise MastodonAPIError('Endpoint not found.') | 1068 | raise MastodonAPIError('Endpoint not found.') |
1066 | 1069 | ||
1067 | 1070 | ||
1068 | if response_object.status_code == 500: | 1071 | if response_object.status_code == 500: |
1069 | raise MastodonAPIError('General API problem.') | 1072 | raise MastodonAPIError('General API problem.') |
1070 | 1073 | ||
@@ -1075,11 +1078,11 @@ class Mastodon: | |||
1075 | "Could not parse response as JSON, response code was %s, " | 1078 | "Could not parse response as JSON, response code was %s, " |
1076 | "bad json content was '%s'" % (response_object.status_code, | 1079 | "bad json content was '%s'" % (response_object.status_code, |
1077 | response_object.content)) | 1080 | response_object.content)) |
1078 | 1081 | ||
1079 | # See if the returned dict is an error dict even though status is 200 | 1082 | # See if the returned dict is an error dict even though status is 200 |
1080 | if isinstance(response, dict) and 'error' in response: | 1083 | if isinstance(response, dict) and 'error' in response: |
1081 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) | 1084 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) |
1082 | 1085 | ||
1083 | # Parse link headers | 1086 | # Parse link headers |
1084 | if isinstance(response, list) and \ | 1087 | if isinstance(response, list) and \ |
1085 | 'Link' in response_object.headers and \ | 1088 | 'Link' in response_object.headers and \ |