BREAKING: Make streaming use json hooks
This commit is contained in:
pare
8987590545
commit
de2114b92b
S'han modificat 2 arxius amb 27 adicions i 25 eliminacions
|
@ -1083,8 +1083,8 @@ class Mastodon:
|
||||||
|
|
||||||
return (date_time_utc - epoch_utc).total_seconds()
|
return (date_time_utc - epoch_utc).total_seconds()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def __json_date_parse(self, json_object):
|
def __json_date_parse(json_object):
|
||||||
"""
|
"""
|
||||||
Parse dates in certain known json fields, if possible.
|
Parse dates in certain known json fields, if possible.
|
||||||
"""
|
"""
|
||||||
|
@ -1100,7 +1100,8 @@ 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):
|
@staticmethod
|
||||||
|
def __json_id_to_bignum(json_object):
|
||||||
"""
|
"""
|
||||||
Converts json string IDs to native python bignums.
|
Converts json string IDs to native python bignums.
|
||||||
"""
|
"""
|
||||||
|
@ -1117,10 +1118,11 @@ class Mastodon:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return json_object
|
return json_object
|
||||||
|
|
||||||
def __json_hooks(self, json_object):
|
@staticmethod
|
||||||
json_object = self.__json_date_parse(json_object)
|
def __json_hooks(json_object):
|
||||||
json_object = self.__json_id_to_bignum(json_object)
|
json_object = Mastodon.__json_date_parse(json_object)
|
||||||
|
json_object = Mastodon.__json_id_to_bignum(json_object)
|
||||||
return 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):
|
||||||
|
|
|
@ -6,11 +6,10 @@ https://github.com/tootsuite/mastodon/blob/master/docs/Using-the-API/Streaming-A
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import six
|
import six
|
||||||
|
from mastodon import Mastodon
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class MalformedEventError(Exception):
|
class MalformedEventError(Exception):
|
||||||
"""Raised when the server-sent event stream is malformed."""
|
"""Raised when the server-sent event stream is malformed."""
|
||||||
pass
|
pass
|
||||||
|
@ -24,7 +23,7 @@ class StreamListener(object):
|
||||||
|
|
||||||
def on_update(self, status):
|
def on_update(self, status):
|
||||||
"""A new status has appeared! 'status' is the parsed JSON dictionary
|
"""A new status has appeared! 'status' is the parsed JSON dictionary
|
||||||
describing the status."""
|
describing the status."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_notification(self, notification):
|
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
|
"""The server has sent us a keep-alive message. This callback may be
|
||||||
useful to carry out periodic housekeeping tasks, or just to confirm
|
useful to carry out periodic housekeeping tasks, or just to confirm
|
||||||
that the connection is still open."""
|
that the connection is still open."""
|
||||||
|
pass
|
||||||
|
|
||||||
def handle_stream(self, lines):
|
def handle_stream(self, lines):
|
||||||
"""
|
"""
|
||||||
Handles a stream of events from the Mastodon server. When each event
|
Handles a stream of events from the Mastodon server. When each event
|
||||||
|
@ -63,7 +63,7 @@ describing the status."""
|
||||||
self.handle_heartbeat()
|
self.handle_heartbeat()
|
||||||
elif line == '':
|
elif line == '':
|
||||||
# end of event
|
# end of event
|
||||||
self._despatch(event)
|
self._dispatch(event)
|
||||||
event = {}
|
event = {}
|
||||||
else:
|
else:
|
||||||
key, value = line.split(': ', 1)
|
key, value = line.split(': ', 1)
|
||||||
|
@ -78,24 +78,24 @@ describing the status."""
|
||||||
if event:
|
if event:
|
||||||
log.warn("outstanding partial event at end of stream: %s", event)
|
log.warn("outstanding partial event at end of stream: %s", event)
|
||||||
|
|
||||||
def _despatch(self, event):
|
def _dispatch(self, event):
|
||||||
try:
|
try:
|
||||||
name = event['event']
|
name = event['event']
|
||||||
data = event['data']
|
data = event['data']
|
||||||
payload = json.loads(data)
|
payload = json.loads(data, object_hook = Mastodon._Mastodon__json_hooks)
|
||||||
except KeyError as err:
|
except KeyError as err:
|
||||||
six.raise_from(
|
six.raise_from(
|
||||||
MalformedEventError('Missing field', err.args[0], event),
|
MalformedEventError('Missing field', err.args[0], event),
|
||||||
err
|
err
|
||||||
)
|
)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
# py2: plain ValueError
|
# py2: plain ValueError
|
||||||
# py3: json.JSONDecodeError, a subclass of ValueError
|
# py3: json.JSONDecodeError, a subclass of ValueError
|
||||||
six.raise_from(
|
six.raise_from(
|
||||||
MalformedEventError('Bad JSON', data),
|
MalformedEventError('Bad JSON', data),
|
||||||
err
|
err
|
||||||
)
|
)
|
||||||
|
|
||||||
handler_name = 'on_' + name
|
handler_name = 'on_' + name
|
||||||
try:
|
try:
|
||||||
handler = getattr(self, handler_name)
|
handler = getattr(self, handler_name)
|
||||||
|
|
Loading…
Referencia en una nova incidència