From de2114b92b1d9dd67935fb3be96bce73576ceaea Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Fri, 24 Nov 2017 13:59:13 +0100 Subject: BREAKING: Make streaming use json hooks --- mastodon/Mastodon.py | 16 +++++++++------- mastodon/streaming.py | 36 ++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 25 deletions(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index b4d4aa3..ab0071b 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -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. """ @@ -1117,10 +1118,11 @@ class Mastodon: 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) + + @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): diff --git a/mastodon/streaming.py b/mastodon/streaming.py index 290ed44..c202ff6 100644 --- a/mastodon/streaming.py +++ b/mastodon/streaming.py @@ -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,7 +39,8 @@ 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): """ Handles a stream of events from the Mastodon server. When each event @@ -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,24 +78,24 @@ 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), - err - ) + six.raise_from( + MalformedEventError('Missing field', err.args[0], event), + err + ) except ValueError as err: - # py2: plain ValueError - # py3: json.JSONDecodeError, a subclass of ValueError - six.raise_from( - MalformedEventError('Bad JSON', data), - err - ) - + # py2: plain ValueError + # py3: json.JSONDecodeError, a subclass of ValueError + six.raise_from( + MalformedEventError('Bad JSON', data), + err + ) + handler_name = 'on_' + name try: handler = getattr(self, handler_name) -- cgit v1.2.3