diff options
author | halcy <halcy@ARARAGI-KUN> | 2022-11-21 22:17:20 +0200 |
---|---|---|
committer | halcy <halcy@ARARAGI-KUN> | 2022-11-21 22:17:20 +0200 |
commit | 1d5b308016b8762d255290add53f84dbd6f7d439 (patch) | |
tree | affb5c96cef67360910386786dfb19b576b67a7f /mastodon | |
parent | 6d4ec618f2f02a20874973647f34cbca9e9936cc (diff) | |
download | mastodon.py-1d5b308016b8762d255290add53f84dbd6f7d439.tar.gz |
Add more streaming events, some tests
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/streaming.py | 105 |
1 files changed, 84 insertions, 21 deletions
diff --git a/mastodon/streaming.py b/mastodon/streaming.py index 08f5670..ed09705 100644 --- a/mastodon/streaming.py +++ b/mastodon/streaming.py | |||
@@ -22,18 +22,50 @@ class StreamListener(object): | |||
22 | Mastodon.hashtag_stream().""" | 22 | Mastodon.hashtag_stream().""" |
23 | 23 | ||
24 | def on_update(self, status): | 24 | def on_update(self, status): |
25 | """A new status has appeared. 'status' is the parsed JSON dictionary | 25 | """A new status has appeared. `status` is the parsed `status dict` |
26 | describing the status.""" | 26 | describing the status.""" |
27 | pass | 27 | pass |
28 | 28 | ||
29 | def on_delete(self, status_id): | ||
30 | """A status has been deleted. `status_id` is the status' integer ID.""" | ||
31 | pass | ||
32 | |||
33 | def on_notification(self, notification): | ||
34 | """A new notification. `notification` is the parsed `notification dict` | ||
35 | describing the notification.""" | ||
36 | pass | ||
37 | |||
38 | def on_filters_changed(self): | ||
39 | """Filters have changed. Does not contain a payload, you will have to | ||
40 | refetch filters yourself.""" | ||
41 | pass | ||
42 | |||
43 | def on_conversation(self, conversation): | ||
44 | """A direct message (in the direct stream) has been received. `conversation` | ||
45 | is the parsed `conversation dict` dictionary describing the conversation""" | ||
46 | pass | ||
47 | |||
48 | def on_announcement(self, annoucement): | ||
49 | """A new announcement has been published. `announcement` is the parsed | ||
50 | `announcement dict` describing the newly posted announcement.""" | ||
51 | pass | ||
52 | |||
53 | def on_announcement_reaction(self, TODO): | ||
54 | """Someone has reacted to an announcement. TODO: what is payload lol""" | ||
55 | pass | ||
56 | |||
57 | def on_announcement_delete(self, annoucement_id): | ||
58 | """An announcement has been deleted. `annoucement_id` is the id of the | ||
59 | deleted announcement.""" | ||
60 | pass | ||
61 | |||
29 | def on_status_update(self, status): | 62 | def on_status_update(self, status): |
30 | """A status has been edited. 'status' is the parsed JSON dictionary | 63 | """A status has been edited. 'status' is the parsed JSON dictionary |
31 | describing the updated status.""" | 64 | describing the updated status.""" |
32 | pass | 65 | pass |
33 | 66 | ||
34 | def on_notification(self, notification): | 67 | def on_encrypted_message(self, unclear): |
35 | """A new notification. 'notification' is the parsed JSON dictionary | 68 | """An encrypted message has been received. Currently unused.""" |
36 | describing the notification.""" | ||
37 | pass | 69 | pass |
38 | 70 | ||
39 | def on_abort(self, err): | 71 | def on_abort(self, err): |
@@ -47,15 +79,6 @@ class StreamListener(object): | |||
47 | """ | 79 | """ |
48 | pass | 80 | pass |
49 | 81 | ||
50 | def on_delete(self, status_id): | ||
51 | """A status has been deleted. status_id is the status' integer ID.""" | ||
52 | pass | ||
53 | |||
54 | def on_conversation(self, conversation): | ||
55 | """A direct message (in the direct stream) has been received. conversation | ||
56 | contains the resulting conversation dict.""" | ||
57 | pass | ||
58 | |||
59 | def on_unknown_event(self, name, unknown_event=None): | 82 | def on_unknown_event(self, name, unknown_event=None): |
60 | """An unknown mastodon API event has been received. The name contains the event-name and unknown_event | 83 | """An unknown mastodon API event has been received. The name contains the event-name and unknown_event |
61 | contains the content of the unknown event. | 84 | contains the content of the unknown event. |
@@ -148,8 +171,7 @@ class StreamListener(object): | |||
148 | for_stream = json.loads(event['stream']) | 171 | for_stream = json.loads(event['stream']) |
149 | except: | 172 | except: |
150 | for_stream = None | 173 | for_stream = None |
151 | payload = json.loads( | 174 | payload = json.loads(data, object_hook=Mastodon._Mastodon__json_hooks) |
152 | data, object_hook=Mastodon._Mastodon__json_hooks) | ||
153 | except KeyError as err: | 175 | except KeyError as err: |
154 | exception = MastodonMalformedEventError( | 176 | exception = MastodonMalformedEventError( |
155 | 'Missing field', err.args[0], event) | 177 | 'Missing field', err.args[0], event) |
@@ -188,11 +210,13 @@ class StreamListener(object): | |||
188 | handler(name, payload, for_stream) | 210 | handler(name, payload, for_stream) |
189 | else: | 211 | else: |
190 | if handler != self.on_unknown_event: | 212 | if handler != self.on_unknown_event: |
191 | handler(payload) | 213 | if handler == self.on_filters_changed: |
214 | handler() | ||
215 | else: | ||
216 | handler(payload) | ||
192 | else: | 217 | else: |
193 | handler(name, payload) | 218 | handler(name, payload) |
194 | 219 | ||
195 | |||
196 | class CallbackStreamListener(StreamListener): | 220 | class CallbackStreamListener(StreamListener): |
197 | """ | 221 | """ |
198 | Simple callback stream handler class. | 222 | Simple callback stream handler class. |
@@ -202,15 +226,34 @@ class CallbackStreamListener(StreamListener): | |||
202 | for diagnostics. | 226 | for diagnostics. |
203 | """ | 227 | """ |
204 | 228 | ||
205 | def __init__(self, update_handler=None, local_update_handler=None, delete_handler=None, notification_handler=None, conversation_handler=None, unknown_event_handler=None, status_update_handler=None): | 229 | def __init__(self, |
230 | update_handler=None, | ||
231 | local_update_handler=None, | ||
232 | delete_handler=None, | ||
233 | notification_handler=None, | ||
234 | conversation_handler=None, | ||
235 | unknown_event_handler=None, | ||
236 | status_update_handler=None, | ||
237 | filters_changed_handler=None, | ||
238 | announcement_handler=None, | ||
239 | announcement_reaction_handler=None, | ||
240 | announcement_delete_handler=None, | ||
241 | encryted_message_handler=None | ||
242 | |||
243 | ): | ||
206 | super(CallbackStreamListener, self).__init__() | 244 | super(CallbackStreamListener, self).__init__() |
207 | self.update_handler = update_handler | 245 | self.update_handler = update_handler |
208 | self.local_update_handler = local_update_handler | 246 | self.local_update_handler = local_update_handler |
209 | self.delete_handler = delete_handler | 247 | self.delete_handler = delete_handler |
210 | self.notification_handler = notification_handler | 248 | self.notification_handler = notification_handler |
249 | self.filters_changed_handler = filters_changed_handler | ||
211 | self.conversation_handler = conversation_handler | 250 | self.conversation_handler = conversation_handler |
212 | self.unknown_event_handler = unknown_event_handler | 251 | self.unknown_event_handler = unknown_event_handler |
213 | self.status_update_handler = status_update_handler | 252 | self.status_update_handler = status_update_handler |
253 | self.announcement_handler = announcement_handler | ||
254 | self.announcement_reaction_handler = announcement_reaction_handler | ||
255 | self.announcement_delete_handler = announcement_delete_handler | ||
256 | self.encryted_message_handler = encryted_message_handler | ||
214 | 257 | ||
215 | def on_update(self, status): | 258 | def on_update(self, status): |
216 | if self.update_handler is not None: | 259 | if self.update_handler is not None: |
@@ -233,14 +276,34 @@ class CallbackStreamListener(StreamListener): | |||
233 | if self.notification_handler is not None: | 276 | if self.notification_handler is not None: |
234 | self.notification_handler(notification) | 277 | self.notification_handler(notification) |
235 | 278 | ||
279 | def on_filters_changed(self): | ||
280 | if self.filters_changed_handler is not None: | ||
281 | self.filters_changed_handler() | ||
282 | |||
236 | def on_conversation(self, conversation): | 283 | def on_conversation(self, conversation): |
237 | if self.conversation_handler is not None: | 284 | if self.conversation_handler is not None: |
238 | self.conversation_handler(conversation) | 285 | self.conversation_handler(conversation) |
239 | 286 | ||
240 | def on_unknown_event(self, name, unknown_event=None): | 287 | def on_announcement(self, annoucement): |
241 | if self.unknown_event_handler is not None: | 288 | if self.announcement_handler is not None: |
242 | self.unknown_event_handler(name, unknown_event) | 289 | self.announcement_handler(annoucement) |
290 | |||
291 | def on_announcement_reaction(self, TODO): | ||
292 | if self.announcement_reaction_handler is not None: | ||
293 | self.announcement_reaction_handler(TODO) | ||
294 | |||
295 | def on_announcement_delete(self, annoucement_id): | ||
296 | if self.announcement_delete_handler is not None: | ||
297 | self.announcement_delete_handler(annoucement_id) | ||
243 | 298 | ||
244 | def on_status_update(self, status): | 299 | def on_status_update(self, status): |
245 | if self.status_update_handler is not None: | 300 | if self.status_update_handler is not None: |
246 | self.status_update_handler(status) | 301 | self.status_update_handler(status) |
302 | |||
303 | def on_encrypted_message(self, unclear): | ||
304 | if self.encryted_message_handler is not None: | ||
305 | self.encryted_message_handler(unclear) | ||
306 | |||
307 | def on_unknown_event(self, name, unknown_event=None): | ||
308 | if self.unknown_event_handler is not None: | ||
309 | self.unknown_event_handler(name, unknown_event) \ No newline at end of file | ||