diff options
author | Lorenz Diener <[email protected]> | 2017-09-08 16:27:16 +0200 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2017-09-08 16:27:16 +0200 |
commit | 982fde2b569cc6415f5854e0d72ffe3fea1a82e1 (patch) | |
tree | 08f907f1822aeff8e410df7665ef38c8380fe5c6 | |
parent | 9badbce67b25f8159785b42723ad8a66da9f47b1 (diff) | |
download | mastodon.py-982fde2b569cc6415f5854e0d72ffe3fea1a82e1.tar.gz |
Add date parsing
-rw-r--r-- | mastodon/Mastodon.py | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 0374427..1dfba7a 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -925,6 +925,7 @@ class Mastodon: | |||
925 | connection may be closed at any time by calling its close() method. | 925 | connection may be closed at any time by calling its close() method. |
926 | """ | 926 | """ |
927 | return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener) | 927 | return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener) |
928 | |||
928 | ### | 929 | ### |
929 | # Internal helpers, dragons probably | 930 | # Internal helpers, dragons probably |
930 | ### | 931 | ### |
@@ -945,6 +946,19 @@ class Mastodon: | |||
945 | 946 | ||
946 | return (date_time_utc - epoch_utc).total_seconds() | 947 | return (date_time_utc - epoch_utc).total_seconds() |
947 | 948 | ||
949 | def __json_date_parse(self, json_object): | ||
950 | """ | ||
951 | Parse dates in certain known json fields, if possible. | ||
952 | """ | ||
953 | known_date_fields = ["created_at"] | ||
954 | for k, v in json_object.items(): | ||
955 | if k in known_date_fields: | ||
956 | try: | ||
957 | json_object[k] = dateutil.parser.parse(v) | ||
958 | except: | ||
959 | raise MastodonAPIError('Encountered invalid date.') | ||
960 | return json_object | ||
961 | |||
948 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): | 962 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): |
949 | """ | 963 | """ |
950 | Internal API request helper. | 964 | Internal API request helper. |
@@ -1016,19 +1030,32 @@ class Mastodon: | |||
1016 | print('Response text content: ' + str(response_object.text)) | 1030 | print('Response text content: ' + str(response_object.text)) |
1017 | 1031 | ||
1018 | if response_object.status_code == 404: | 1032 | if response_object.status_code == 404: |
1019 | raise MastodonAPIError('Endpoint not found.') | 1033 | try: |
1020 | 1034 | response = response_object.json() | |
1035 | except: | ||
1036 | raise MastodonAPIError('Endpoint not found.') | ||
1037 | |||
1038 | if isinstance(response, dict) and 'error' in response: | ||
1039 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) | ||
1040 | else: | ||
1041 | raise MastodonAPIError('Endpoint not found.') | ||
1042 | |||
1043 | |||
1021 | if response_object.status_code == 500: | 1044 | if response_object.status_code == 500: |
1022 | raise MastodonAPIError('General API problem.') | 1045 | raise MastodonAPIError('General API problem.') |
1023 | 1046 | ||
1024 | try: | 1047 | try: |
1025 | response = response_object.json() | 1048 | response = response_object.json(object_hook=self.__json_date_parse) |
1026 | except: | 1049 | except: |
1027 | raise MastodonAPIError( | 1050 | raise MastodonAPIError( |
1028 | "Could not parse response as JSON, response code was %s, " | 1051 | "Could not parse response as JSON, response code was %s, " |
1029 | "bad json content was '%s'" % (response_object.status_code, | 1052 | "bad json content was '%s'" % (response_object.status_code, |
1030 | response_object.content)) | 1053 | response_object.content)) |
1031 | 1054 | ||
1055 | # See if the returned dict is an error dict even though status is 200 | ||
1056 | if isinstance(response, dict) and 'error' in response: | ||
1057 | raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) | ||
1058 | |||
1032 | # Parse link headers | 1059 | # Parse link headers |
1033 | if isinstance(response, list) and \ | 1060 | if isinstance(response, list) and \ |
1034 | 'Link' in response_object.headers and \ | 1061 | 'Link' in response_object.headers and \ |