Git Product home page Git Product logo

Comments (5)

halcy avatar halcy commented on June 25, 2024 1

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

from mastodon.py.

franasal avatar franasal commented on June 25, 2024

thanks for the feedback and the example, closing as it's not an actual Issue ( :

from mastodon.py.

halcy avatar halcy commented on June 25, 2024

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

from mastodon.py.

franasal avatar franasal commented on June 25, 2024

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

from mastodon.py.

halcy avatar halcy commented on June 25, 2024

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.

from mastodon.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.