add support for mastodon v2.0's string IDs

This commit is contained in:
codl 2017-11-05 13:37:45 +01:00
pare 61552f9f84
commit 6b5deb4898
No se encontró ninguna clave conocida en la base de datos para esta firma
ID de clave GPG: 6CD7C8891ED1233A

Veure arxiu

@ -16,6 +16,7 @@ import dateutil.parser
import re import re
import copy import copy
import threading import threading
import sys
try: try:
from urllib.parse import urlparse from urllib.parse import urlparse
except ImportError: except ImportError:
@ -970,6 +971,7 @@ class Mastodon:
return (date_time_utc - epoch_utc).total_seconds() return (date_time_utc - epoch_utc).total_seconds()
def __json_date_parse(self, json_object): def __json_date_parse(self, json_object):
""" """
Parse dates in certain known json fields, if possible. Parse dates in certain known json fields, if possible.
@ -986,6 +988,29 @@ class Mastodon:
raise MastodonAPIError('Encountered invalid date.') raise MastodonAPIError('Encountered invalid date.')
return json_object return json_object
def __json_id_to_bignum(self, json_object):
"""
Converts json string IDs to native python bignums.
"""
if sys.version_info.major >= 3:
str_type = str
else:
str_type = unicode
if ('id' in json_object and
isinstance(json_object['id'], str_type)):
try:
json_object['id'] = int(json_object['id'])
except ValueError:
pass
return json_object
def __json_hooks(self, json_object):
json_object = self.__json_date_parse(json_object)
json_object = self.__json_id_to_bignum(json_object)
return json_object
def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True):
""" """
Internal API request helper. Internal API request helper.
@ -1104,7 +1129,7 @@ class Mastodon:
continue continue
try: try:
response = response_object.json(object_hook=self.__json_date_parse) response = response_object.json(object_hook=self.__json_hooks)
except: except:
raise MastodonAPIError( raise MastodonAPIError(
"Could not parse response as JSON, response code was %s, " "Could not parse response as JSON, response code was %s, "