Open franasal opened 12 months ago
I'm kind of confused by this - the tutorial you linked seems to just be... wrong, and I don't think that would have worked in any past version either.
What you want to do instead is something like (just from memory, didn't test, might not be 100% correct):
from mastodon.streaming import CallbackStreamListener
# ... setup ...
def handle_mention(notification):
if notification.type == "mention":
print(notification.status)
listener = CallbackStreamListener(notification_handler = handle_mention)
mastodon.stream_user(listener)
You might also want to look at run_async and reconnect_async if you want the stream to just run forever and reconnect if it loses connection.
You can find a full example of how to use the streaming API here: https://github.com/halcy/MastodonpyExamples/blob/bba210623a31d2f148ec4560669d22786e8102e3/01_latency_observatory/latencies.py
thanks for the feedback and the example, closing as it's not an actual Issue ( :
Did you get it working? If so, could you tell me if the snippet above was correct? I should probably ask the author of that tutorial to fix it
hmm not really. Sorry, I should have made a proper test before closing it. I modified your code as follows using public stream:
from mastodon.streaming import CallbackStreamListener
# ... setup ...
hashtag="vegan"
def handle_mention(status):
if hashtag in status.content.lower():
print(status.content)
else:
print(status.content)
listener = CallbackStreamListener(notification_handler = handle_mention)
mastodon.stream_public(listener)
but it doesn't catch any use of the hashtag, nor in the else statement gives any output. It should be printing every toot in real time, right? i also tried with stream_hashtag
using the tag
parameter but with the same result
For hashtags, you would want the on_update handler, so you'd want to modify that CallbackStreamListener line like so:
listener = CallbackStreamListener(update_handler = handle_mention)
That should make your code work and print all the statuses. You can have a look at the docs: https://mastodonpy.readthedocs.io/en/stable/10_streaming.html
but maybe that API just is a bit confusing, especially now that there are quite a few events, instead of just like, three, a few versions back. I wonder if we could add some simpler way where all events just go to one function.
when calling the
mastodon.stream_user(handle_mention)
function as per this tutorialthe following error prompts: