halcy / Mastodon.py

Python wrapper for the Mastodon ( https://github.com/mastodon/mastodon/ ) API.
MIT License
873 stars 149 forks source link

how do I use min_id and since_id? #270

Closed db0 closed 1 year ago

db0 commented 1 year ago

I'm trying to get all notifications since a specific ID

    notifications = mastodon.notifications(
        min_id=last_parsed_notification, 
        exclude_types=["follow", "favourite", "reblog", "poll", "follow_request"]
    )

I've tried with min_id and with since_id

I've also tried with Id being in a dictionary like so {'id': 109381418697881490} and with it being sent as a simple integer 109381418697881490. I always get an empty list returned.

However I know I have a notification with a higher ID available

If I comment out the min_id key and put some prints:

DEBUG      | 2022-11-21 12:29:39 | __main__:check_for_requests:54 - Last notification ID: 109381418697881490
DEBUG      | 2022-11-21 12:29:40 | __main__:check_for_requests:63 - 109381539787549538
DEBUG      | 2022-11-21 12:29:40 | __main__:check_for_requests:63 - 109381418697881490
halcy commented 1 year ago

What you're doing seems broadly correct, but the ID used seems weird. I think you're maybe passing in the ID of the status, rather than of the notification?

Example of what works (just tested), returns "2":

len(mastodon.notifications(min_id = mastodon.notifications()[2]))

halcy commented 1 year ago

should note that, despite what mastodon.py currently does, notification ids are not actually snowflake IDs, either, and I need to remove date unpacking from those parameters. But searching by min/max/since-ID should work, and does, as far as I can tell

db0 commented 1 year ago

So I pass a whole element of the notifications() array? Let me try and I'll tell you

halcy commented 1 year ago

Either that, or the "id" element from that, so for example

len(mastodon.notifications(min_id = mastodon.notifications()[2].id))

is the same as

len(mastodon.notifications(min_id = mastodon.notifications()[2]))

Mastodon.py will internally unpack the ID from the object for you if you pass the object, because it makes code a little easier to read.

Generally, everything that takes the pagination attributes (min/max/since_id) will use the IDs from the objects returned from that call, so notification id for notification, status id for status, etc.

P.S.: If what you're trying to do is get up to date notifications as they come in, have a look at the Streaming API (specifically stream_user) - it's a bit more complicated to use, but may let you do what you want to more efficiently (with some caveats)

db0 commented 1 year ago

No nothing like that. I'm building this https://sigmoid.social/@stablehorde_generator :)

halcy commented 1 year ago

I'm assuming what you want to do is react to when the bot get mentioned? In that case, the streaming API would actually probably work quite well, but so will just polling notifications every few minutes.

In any case, I guess I can close this? if not, feel free to reopen.

db0 commented 1 year ago

Worked. Yes you can close. Thanks for the support!