kopia lustrzana https://github.com/halcy/Mastodon.py
commit
b45a27a406
|
@ -4,7 +4,6 @@ https://github.com/mastodon/documentation/blob/master/content/en/methods/timelin
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import six
|
|
||||||
try:
|
try:
|
||||||
from inspect import signature
|
from inspect import signature
|
||||||
except:
|
except:
|
||||||
|
@ -131,10 +130,7 @@ class StreamListener(object):
|
||||||
exception = MastodonMalformedEventError(
|
exception = MastodonMalformedEventError(
|
||||||
"Malformed UTF-8")
|
"Malformed UTF-8")
|
||||||
self.on_abort(exception)
|
self.on_abort(exception)
|
||||||
six.raise_from(
|
raise exception from err
|
||||||
exception,
|
|
||||||
err
|
|
||||||
)
|
|
||||||
if line == '':
|
if line == '':
|
||||||
self._dispatch(event)
|
self._dispatch(event)
|
||||||
event = {}
|
event = {}
|
||||||
|
@ -146,26 +142,17 @@ class StreamListener(object):
|
||||||
except ChunkedEncodingError as err:
|
except ChunkedEncodingError as err:
|
||||||
exception = MastodonNetworkError("Server ceased communication.")
|
exception = MastodonNetworkError("Server ceased communication.")
|
||||||
self.on_abort(exception)
|
self.on_abort(exception)
|
||||||
six.raise_from(
|
raise exception from err
|
||||||
exception,
|
|
||||||
err
|
|
||||||
)
|
|
||||||
except ReadTimeout as err:
|
except ReadTimeout as err:
|
||||||
exception = MastodonReadTimeout(
|
exception = MastodonReadTimeout(
|
||||||
"Timed out while reading from server."),
|
"Timed out while reading from server."),
|
||||||
self.on_abort(exception)
|
self.on_abort(exception)
|
||||||
six.raise_from(
|
raise exception from err
|
||||||
exception,
|
|
||||||
err
|
|
||||||
)
|
|
||||||
except ConnectionError as err:
|
except ConnectionError as err:
|
||||||
exception = MastodonNetworkError(
|
exception = MastodonNetworkError(
|
||||||
"Requests reports connection error."),
|
"Requests reports connection error."),
|
||||||
self.on_abort(exception)
|
self.on_abort(exception)
|
||||||
six.raise_from(
|
raise exception from err
|
||||||
exception,
|
|
||||||
err
|
|
||||||
)
|
|
||||||
|
|
||||||
def _parse_line(self, line, event):
|
def _parse_line(self, line, event):
|
||||||
if line.startswith(':'):
|
if line.startswith(':'):
|
||||||
|
@ -200,19 +187,13 @@ class StreamListener(object):
|
||||||
exception = MastodonMalformedEventError(
|
exception = MastodonMalformedEventError(
|
||||||
'Missing field', err.args[0], event)
|
'Missing field', err.args[0], event)
|
||||||
self.on_abort(exception)
|
self.on_abort(exception)
|
||||||
six.raise_from(
|
raise exception from err
|
||||||
exception,
|
|
||||||
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
|
||||||
exception = MastodonMalformedEventError('Bad JSON', data)
|
exception = MastodonMalformedEventError('Bad JSON', data)
|
||||||
self.on_abort(exception)
|
self.on_abort(exception)
|
||||||
six.raise_from(
|
raise exception from err
|
||||||
exception,
|
|
||||||
err
|
|
||||||
)
|
|
||||||
|
|
||||||
# New mastodon API also supports event names with dots,
|
# New mastodon API also supports event names with dots,
|
||||||
# specifically, status_update.
|
# specifically, status_update.
|
||||||
|
@ -287,10 +268,7 @@ class CallbackStreamListener(StreamListener):
|
||||||
if self.local_update_handler is not None and not "@" in status["account"]["acct"]:
|
if self.local_update_handler is not None and not "@" in status["account"]["acct"]:
|
||||||
self.local_update_handler(status)
|
self.local_update_handler(status)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
six.raise_from(
|
raise MastodonMalformedEventError('received bad update', status) from err
|
||||||
MastodonMalformedEventError('received bad update', status),
|
|
||||||
err
|
|
||||||
)
|
|
||||||
|
|
||||||
def on_delete(self, deleted_id):
|
def on_delete(self, deleted_id):
|
||||||
if self.delete_handler is not None:
|
if self.delete_handler is not None:
|
||||||
|
|
|
@ -23,7 +23,6 @@ classifiers = [
|
||||||
dependencies = [
|
dependencies = [
|
||||||
'requests>=2.4.2',
|
'requests>=2.4.2',
|
||||||
'python-dateutil',
|
'python-dateutil',
|
||||||
'six',
|
|
||||||
'python-magic-bin ; platform_system=="Windows"',
|
'python-magic-bin ; platform_system=="Windows"',
|
||||||
'python-magic ; platform_system!="Windows"',
|
'python-magic ; platform_system!="Windows"',
|
||||||
'decorator>=4.0.0',
|
'decorator>=4.0.0',
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import six
|
|
||||||
import pytest
|
import pytest
|
||||||
import itertools
|
import itertools
|
||||||
from mastodon.streaming import StreamListener, CallbackStreamListener
|
from mastodon.streaming import StreamListener, CallbackStreamListener
|
||||||
|
@ -95,10 +94,13 @@ class Listener(StreamListener):
|
||||||
|
|
||||||
def handle_stream_(self, lines):
|
def handle_stream_(self, lines):
|
||||||
"""Test helper to avoid littering all tests with six.b()."""
|
"""Test helper to avoid littering all tests with six.b()."""
|
||||||
|
def six_b(s):
|
||||||
|
return s.encode("latin-1")
|
||||||
|
|
||||||
class MockResponse():
|
class MockResponse():
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
def iter_content(self, chunk_size):
|
def iter_content(self, chunk_size):
|
||||||
for line in self.data:
|
for line in self.data:
|
||||||
for byte in line:
|
for byte in line:
|
||||||
|
@ -106,7 +108,7 @@ class Listener(StreamListener):
|
||||||
bytearr.append(byte)
|
bytearr.append(byte)
|
||||||
yield(bytearr)
|
yield(bytearr)
|
||||||
yield(b'\n')
|
yield(b'\n')
|
||||||
return self.handle_stream(MockResponse(map(six.b, lines)))
|
return self.handle_stream(MockResponse(map(six_b, lines)))
|
||||||
|
|
||||||
|
|
||||||
def test_heartbeat():
|
def test_heartbeat():
|
||||||
|
|
Ładowanie…
Reference in New Issue