BREAKING: Make streaming use json hooks

This commit is contained in:
Lorenz Diener 2017-11-24 13:59:13 +01:00
pare 8987590545
commit de2114b92b
S'han modificat 2 arxius amb 27 adicions i 25 eliminacions

Veure arxiu

@ -1083,8 +1083,8 @@ class Mastodon:
return (date_time_utc - epoch_utc).total_seconds()
def __json_date_parse(self, json_object):
@staticmethod
def __json_date_parse(json_object):
"""
Parse dates in certain known json fields, if possible.
"""
@ -1100,7 +1100,8 @@ class Mastodon:
raise MastodonAPIError('Encountered invalid date.')
return json_object
def __json_id_to_bignum(self, json_object):
@staticmethod
def __json_id_to_bignum(json_object):
"""
Converts json string IDs to native python bignums.
"""
@ -1118,9 +1119,10 @@ class Mastodon:
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)
@staticmethod
def __json_hooks(json_object):
json_object = Mastodon.__json_date_parse(json_object)
json_object = Mastodon.__json_id_to_bignum(json_object)
return json_object
def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True):

Veure arxiu

@ -6,11 +6,10 @@ https://github.com/tootsuite/mastodon/blob/master/docs/Using-the-API/Streaming-A
import json
import logging
import six
from mastodon import Mastodon
log = logging.getLogger(__name__)
class MalformedEventError(Exception):
"""Raised when the server-sent event stream is malformed."""
pass
@ -24,7 +23,7 @@ class StreamListener(object):
def on_update(self, status):
"""A new status has appeared! 'status' is the parsed JSON dictionary
describing the status."""
describing the status."""
pass
def on_notification(self, notification):
@ -40,6 +39,7 @@ describing the status."""
"""The server has sent us a keep-alive message. This callback may be
useful to carry out periodic housekeeping tasks, or just to confirm
that the connection is still open."""
pass
def handle_stream(self, lines):
"""
@ -63,7 +63,7 @@ describing the status."""
self.handle_heartbeat()
elif line == '':
# end of event
self._despatch(event)
self._dispatch(event)
event = {}
else:
key, value = line.split(': ', 1)
@ -78,11 +78,11 @@ describing the status."""
if event:
log.warn("outstanding partial event at end of stream: %s", event)
def _despatch(self, event):
def _dispatch(self, event):
try:
name = event['event']
data = event['data']
payload = json.loads(data)
payload = json.loads(data, object_hook = Mastodon._Mastodon__json_hooks)
except KeyError as err:
six.raise_from(
MalformedEventError('Missing field', err.args[0], event),