diff options
author | codl <[email protected]> | 2017-11-05 13:37:45 +0100 |
---|---|---|
committer | codl <[email protected]> | 2017-11-05 14:17:49 +0100 |
commit | 6b5deb4898d2817c484ccbdfceadfdb242ba7aa0 (patch) | |
tree | a41d3885f74761771799cce9b41fd853d377b8b0 | |
parent | 61552f9f84c736d447e0fccd54040d3b956a83e9 (diff) | |
download | mastodon.py-6b5deb4898d2817c484ccbdfceadfdb242ba7aa0.tar.gz |
add support for mastodon v2.0's string IDs
-rw-r--r-- | mastodon/Mastodon.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index b118421..cc16a10 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -16,6 +16,7 @@ import dateutil.parser | |||
16 | import re | 16 | import re |
17 | import copy | 17 | import copy |
18 | import threading | 18 | import threading |
19 | import sys | ||
19 | try: | 20 | try: |
20 | from urllib.parse import urlparse | 21 | from urllib.parse import urlparse |
21 | except ImportError: | 22 | except ImportError: |
@@ -970,6 +971,7 @@ class Mastodon: | |||
970 | 971 | ||
971 | return (date_time_utc - epoch_utc).total_seconds() | 972 | return (date_time_utc - epoch_utc).total_seconds() |
972 | 973 | ||
974 | |||
973 | def __json_date_parse(self, json_object): | 975 | def __json_date_parse(self, json_object): |
974 | """ | 976 | """ |
975 | Parse dates in certain known json fields, if possible. | 977 | Parse dates in certain known json fields, if possible. |
@@ -986,6 +988,29 @@ class Mastodon: | |||
986 | raise MastodonAPIError('Encountered invalid date.') | 988 | raise MastodonAPIError('Encountered invalid date.') |
987 | return json_object | 989 | return json_object |
988 | 990 | ||
991 | def __json_id_to_bignum(self, json_object): | ||
992 | """ | ||
993 | Converts json string IDs to native python bignums. | ||
994 | """ | ||
995 | if sys.version_info.major >= 3: | ||
996 | str_type = str | ||
997 | else: | ||
998 | str_type = unicode | ||
999 | |||
1000 | if ('id' in json_object and | ||
1001 | isinstance(json_object['id'], str_type)): | ||
1002 | try: | ||
1003 | json_object['id'] = int(json_object['id']) | ||
1004 | except ValueError: | ||
1005 | pass | ||
1006 | |||
1007 | return json_object | ||
1008 | |||
1009 | def __json_hooks(self, json_object): | ||
1010 | json_object = self.__json_date_parse(json_object) | ||
1011 | json_object = self.__json_id_to_bignum(json_object) | ||
1012 | return json_object | ||
1013 | |||
989 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): | 1014 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): |
990 | """ | 1015 | """ |
991 | Internal API request helper. | 1016 | Internal API request helper. |
@@ -1104,7 +1129,7 @@ class Mastodon: | |||
1104 | continue | 1129 | continue |
1105 | 1130 | ||
1106 | try: | 1131 | try: |
1107 | response = response_object.json(object_hook=self.__json_date_parse) | 1132 | response = response_object.json(object_hook=self.__json_hooks) |
1108 | except: | 1133 | except: |
1109 | raise MastodonAPIError( | 1134 | raise MastodonAPIError( |
1110 | "Could not parse response as JSON, response code was %s, " | 1135 | "Could not parse response as JSON, response code was %s, " |