aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'docs/01_general.rst')
-rw-r--r--docs/01_general.rst130
1 files changed, 130 insertions, 0 deletions
diff --git a/docs/01_general.rst b/docs/01_general.rst
new file mode 100644
index 0000000..0ea3ac4
--- /dev/null
+++ b/docs/01_general.rst
@@ -0,0 +1,130 @@
1Rate limiting
2-------------
3Mastodon's API rate limits per user account. By default, the limit is 300 requests
4per 5 minute time slot. This can differ from instance to instance and is subject to change.
5Mastodon.py has three modes for dealing with rate limiting that you can pass to
6the constructor, "throw", "wait" and "pace", "wait" being the default.
7
8In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When
9a request hits the rate limit, it simply throws a `MastodonRateLimitError`. This is
10for applications that need to handle all rate limiting themselves (i.e. interactive apps),
11or applications wanting to use Mastodon.py in a multi-threaded context ("wait" and "pace"
12modes are not thread safe).
13
14.. note::
15 Rate limit information is available on the `Mastodon` object for applications that
16 implement their own rate limit handling.
17
18 .. attribute:: Mastodon.ratelimit_remaining
19
20 Number of requests allowed until the next reset.
21
22 .. attribute:: Mastodon.ratelimit_reset
23
24 Time at which the rate limit will next be reset, as a POSIX timestamp.
25
26 .. attribute:: Mastodon.ratelimit_limit
27
28 Total number of requests allowed between resets. Typically 300.
29
30 .. attribute:: Mastodon.ratelimit_lastcall
31
32 Time at which these values have last been seen and updated, as a POSIX timestamp.
33
34In "wait" mode, once a request hits the rate limit, Mastodon.py will wait until
35the rate limit resets and then try again, until the request succeeds or an error
36is encountered. This mode is for applications that would rather just not worry about rate limits
37much, don't poll the API all that often, and are okay with a call sometimes just taking
38a while.
39
40In "pace" mode, Mastodon.py will delay each new request after the first one such that,
41if requests were to continue at the same rate, only a certain fraction (set in the
42constructor as `ratelimit_pacefactor`) of the rate limit will be used up. The fraction can
43be (and by default, is) greater than one. If the rate limit is hit, "pace" behaves like
44"wait". This mode is probably the most advanced one and allows you to just poll in
45a loop without ever sleeping at all yourself. It is for applications that would rather
46just pretend there is no such thing as a rate limit and are fine with sometimes not
47being very interactive.
48
49In addition to the per-user limit, there is a per-IP limit of 7500 requests per 5
50minute time slot, and tighter limits on logins. Mastodon.py does not make any effort
51to respect these.
52
53If your application requires many hits to endpoints that are available without logging
54in, do consider using Mastodon.py without authenticating to get the full per-IP limit.
55
56Pagination
57----------
58Many of Mastodon's API endpoints are paginated. What this means is that if you request
59data from them, you might not get all the data at once - instead, you might only get the
60first few results.
61
62All endpoints that are paginated have four parameters: `since_id`, `max_id`, `min_id` and
63`limit`. `since_id` allows you to specify the smallest id you want in the returned data, but
64you will still always get the newest data, so if there are too many statuses between
65the newest one and `since_id`, some will not be returned. `min_id`, on the other hand, gives
66you statuses with that minimum id and newer, starting at the given id. `max_id`, similarly,
67allows you to specify the largest id you want. By specifying either min_id or `max_id`
68(generally, only one, not both, though specifying both is supported starting with Mastodon
69version 3.3.0) of them you can go through pages forwards and backwards.
70
71On Mastodon mainline, you can, pass datetime objects as IDs when fetching posts,
72since the IDs used are Snowflake IDs and dates can be approximately converted to those.
73This is guaranteed to work on mainline Mastodon servers and very likely to work on all
74forks, but will **not** work on other servers implementing the API, like Pleroma, Misskey
75or Gotosocial. You should not use this if you want your application to be universally
76compatible. It's also relatively coarse-grained.
77
78`limit` allows you to specify how many results you would like returned. Note that an
79instance may choose to return less results than you requested - by default, Mastodon
80will return no more than 40 statuses and no more than 80 accounts no matter how high
81you set the limit.
82
83The responses returned by paginated endpoints contain a "link" header that specifies
84which parameters to use to get the next and previous pages. Mastodon.py parses these
85and stores them (if present) in the first (for the previous page) and last (for the
86next page) item of the returned list as _pagination_prev and _pagination_next. They
87are accessible only via attribute-style access. Note that this means that if you
88want to persist pagination info with your data, you'll have to take care of that
89manually (or persist objects, not just dicts).
90
91There are convenience functions available for fetching the previous and next page of
92a paginated request as well as for fetching all pages starting from a first page.
93
94IDs and unpacking
95-----------------
96Mastodon's API uses IDs in several places: User IDs, Toot IDs, ...
97
98While debugging, it might be tempting to copy-paste IDs from the
99web interface into your code. This will not work, as the IDs on the web
100interface and in the URLs are not the same as the IDs used internally
101in the API, so don't do that.
102
103ID unpacking
104~~~~~~~~~~~~
105Wherever Mastodon.py expects an ID as a parameter, you can also pass a
106dict that contains an id - this means that, for example, instead of writing
107
108.. code-block:: python
109
110 mastodon.status_post("@somebody wow!", in_reply_to_id = toot["id"])
111
112you can also just write
113
114.. code-block:: python
115
116 mastodon.status_post("@somebody wow!", in_reply_to_id = toot)
117
118and everything will work as intended.
119
120A brief note on block lists
121---------------------------
122Mastodon.py used to block three instances because these were particularly notorious for
123harassing trans people and I don't feel like I have an obligation to let software I
124distribute help people who want my friends to die. I don't want to be associated with
125that, at all.
126
127Those instances are now all gone, any point that could have been has been made, and
128there is no list anymore.
129
130Trans rights are human rights.
Powered by cgit v1.2.3 (git 2.41.0)