Mastodonplus.py/docs/index.rst

983 líneas
35 KiB
ReStructuredText
Original Vista normal Històric

2016-11-24 02:27:00 +01:00
Mastodon.py
===========
.. py:module:: mastodon
.. py:class: Mastodon
2018-08-17 15:09:25 +02:00
Register your app! This only needs to be done once. Uncomment the code and substitute in your information.
2016-11-24 00:58:14 +01:00
2016-11-24 03:03:52 +01:00
.. code-block:: python
from mastodon import Mastodon
'''
Mastodon.create_app(
2017-06-15 20:48:59 +02:00
'pytooterapp',
api_base_url = 'https://mastodon.social',
to_file = 'pytooter_clientcred.secret'
2016-11-24 03:03:52 +01:00
)
'''
2018-08-17 15:09:25 +02:00
Then login. This can be done every time, or use persisted.
.. code-block:: python
from mastodon import Mastodon
2017-06-15 23:28:53 +02:00
mastodon = Mastodon(
client_id = 'pytooter_clientcred.secret',
api_base_url = 'https://mastodon.social'
)
2016-11-24 03:03:52 +01:00
mastodon.log_in(
'my_login_email@example.com',
2017-06-15 20:48:59 +02:00
'incrediblygoodpassword',
to_file = 'pytooter_usercred.secret'
2016-11-24 03:03:52 +01:00
)
2018-08-17 15:09:25 +02:00
To post, create an actual API instance.
.. code-block:: python
from mastodon import Mastodon
2016-11-24 03:03:52 +01:00
mastodon = Mastodon(
2017-06-15 20:48:59 +02:00
access_token = 'pytooter_usercred.secret',
api_base_url = 'https://mastodon.social'
2016-11-24 03:03:52 +01:00
)
2018-08-17 15:09:25 +02:00
mastodon.toot('Tooting from python using #mastodonpy !')
2016-11-24 02:27:00 +01:00
2017-11-24 15:08:34 +01:00
`Mastodon`_ is an ActivityPub and OStatus based twitter-like federated social
2016-11-24 03:03:52 +01:00
network node. It has an API that allows you to interact with its
every aspect. This is a simple python wrapper for that api, provided
as a single python module. By default, it talks to the
`Mastodon flagship instance`_, but it can be set to talk to any
2017-12-14 00:33:22 +01:00
node running Mastodon by setting `api_base_url` when creating the
api object (or creating an app).
2016-11-24 02:27:00 +01:00
2017-06-16 01:31:18 +02:00
Mastodon.py aims to implement the complete public Mastodon API. As
2018-07-30 17:30:46 +02:00
of this time, it is feature complete for Mastodon version 2.4.3. Pleromas
Mastodon API layer, while not an official target, should also be basically
compatible.
2017-06-16 01:31:18 +02:00
2016-11-25 23:14:00 +01:00
A note about rate limits
------------------------
2017-12-15 21:16:06 +01:00
Mastodons API rate limits per user account. By default, the limit is 300 requests
per 5 minute time slot. This can differ from instance to instance and is subject to change.
2016-11-25 23:28:30 +01:00
Mastodon.py has three modes for dealing with rate limiting that you can pass to
the constructor, "throw", "wait" and "pace", "wait" being the default.
2016-11-25 23:14:00 +01:00
In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When
2017-12-14 00:27:34 +01:00
a request hits the rate limit, it simply throws a `MastodonRateLimitError`. This is
2016-11-25 23:14:00 +01:00
for applications that need to handle all rate limiting themselves (i.e. interactive apps),
or applications wanting to use Mastodon.py in a multi-threaded context ("wait" and "pace"
modes are not thread safe).
.. note::
Rate limit information is available on the `Mastodon` object for applications that
implement their own rate limit handling.
.. attribute:: Mastodon.ratelimit_remaining
Number of requests allowed until the next reset.
.. attribute:: Mastodon.ratelimit_reset
Time at which the rate limit will next be reset, as a POSIX timestamp.
.. attribute:: Mastodon.ratelimit_limit
Total number of requests allowed between resets. Typically 300.
.. attribute:: Mastodon.ratelimit_lastcall
Time at which these values have last been seen and updated, as a POSIX timestamp.
2016-11-25 23:14:00 +01:00
In "wait" mode, once a request hits the rate limit, Mastodon.py will wait until
the rate limit resets and then try again, until the request succeeds or an error
is encountered. This mode is for applications that would rather just not worry about rate limits
much, don't poll the api all that often, and are okay with a call sometimes just taking
a while.
In "pace" mode, Mastodon.py will delay each new request after the first one such that,
if requests were to continue at the same rate, only a certain fraction (set in the
2017-12-14 13:34:09 +01:00
constructor as `ratelimit_pacefactor`) of the rate limit will be used up. The fraction can
2016-11-25 23:14:00 +01:00
be (and by default, is) greater than one. If the rate limit is hit, "pace" behaves like
"wait". This mode is probably the most advanced one and allows you to just poll in
a loop without ever sleeping at all yourself. It is for applications that would rather
just pretend there is no such thing as a rate limit and are fine with sometimes not
being very interactive.
2017-12-15 21:16:06 +01:00
In addition to the per-user limit, there is a per-IP limit of 7500 requests per 5
minute time slot, and tighter limits on logins. Mastodon.py does not make any effort
to respect these.
If your application requires many hits to endpoints that are available without logging
in, do consider using Mastodon.py without authenticating to get the full per-IP limit.
2017-12-15 21:16:06 +01:00
2017-06-16 01:23:19 +02:00
A note about pagination
-----------------------
Many of Mastodons API endpoints are paginated. What this means is that if you request
data from them, you might not get all the data at once - instead, you might only get the
first few results.
All endpoints that are paginated have three parameters: since_id, max_id and limit.
since_id allows you to specify the smallest id you want in the returned data. max_id,
similarly, allows you to specify the largest. By specifying either one (generally,
only one, not both) of them you can go through pages forwards and backwards.
limit allows you to specify how many results you would like returned. Note that an
2018-06-04 16:55:37 +02:00
instance may choose to return less results than you requested - by default, Mastodon
will return no more than 40 statues and no more than 80 accounts no matter how high
you set the limit.
2017-06-16 01:23:19 +02:00
The responses returned by paginated endpoints contain a "link" header that specifies
which parameters to use to get the next and previous pages. Mastodon.py parses these
and stores them (if present) in the first (for the previous page) and last (for the
2018-06-04 16:48:20 +02:00
next page) item of the returned list as _pagination_prev and _pagination_next. They
2018-06-05 22:10:31 +02:00
are accessible only via attribute-style access. Note that this means that if you
want to persist pagination info with your data, you'll have to take care of that
2018-06-04 16:48:20 +02:00
manually (or persist objects, not just dicts).
2017-06-16 01:23:19 +02:00
There are convenience functions available for fetching the previous and next page of
a paginated request as well as for fetching all pages starting from a first page.
2017-12-11 16:23:22 +01:00
Two notes about IDs
-------------------
2016-11-25 20:34:45 +01:00
Mastodons API uses IDs in several places: User IDs, Toot IDs, ...
While debugging, it might be tempting to copy-paste in IDs from the
web interface into your code. This will not work, as the IDs on the web
interface and in the URLs are not the same as the IDs used internally
in the API, so don't do that.
2017-12-11 16:23:22 +01:00
ID unpacking
~~~~~~~~~~~~
Wherever Mastodon.py expects an ID as a parameter, you can also pass a
2017-12-14 13:29:42 +01:00
dict that contains an id - this means that, for example, instead of writing
2017-12-11 16:23:22 +01:00
.. code-block:: python
mastodon.status_post("@somebody wow!", in_reply_to_id = toot["id"])
you can also just write
.. code-block:: python
mastodon.status_post("@somebody wow!", in_reply_to_id = toot)
and everything will work as intended.
2017-09-08 16:27:28 +02:00
Error handling
--------------
When Mastodon.py encounters an error, it will raise an exception, generally with
some text included to tell you what went wrong.
2017-11-24 15:25:38 +01:00
The base class that all mastodon exceptions inherit from is `MastodonError`.
If you are only interested in the fact an error was raised somewhere in
Mastodon.py, and not the details, this is the exception you can catch.
2017-11-24 15:25:38 +01:00
`MastodonIllegalArgumentError` is generally a programming problem - you asked the
2017-12-13 17:11:45 +01:00
API to do something obviously invalid (i.e. specify a privacy option that does
2017-09-08 16:27:28 +02:00
not exist).
2017-11-24 15:25:38 +01:00
`MastodonFileNotFoundError` and `MastodonNetworkError` are IO errors - could be you
specified a wrong URL, could be the internet is down or your hard drive is
2018-06-04 15:00:25 +02:00
dying. They inherit from `MastodonIOError`, for easy catching. There is a sub-error
of `MastodonNetworkError`, `MastodonReadTimeout`, which is thrown when a streaming
API stream times out during reading.
2017-09-08 16:27:28 +02:00
2017-11-24 15:25:38 +01:00
`MastodonAPIError` is an error returned from the Mastodon instance - the server
2017-09-08 16:27:28 +02:00
has decided it can't fullfill your request (i.e. you requested info on a user that
2018-06-04 15:00:25 +02:00
does not exist). It is further split into `MastodonNotFoundError` (API returned 404)
and `MastodonUnauthorizedError` (API returned 401). Different error codes might exist,
but are not currently handled separately.
`MastodonMalformedEventError` is raised when a streaming API listener receives an
invalid event. There have been reports that this can sometimes happen after prolonged
operation due to an upstream problem in the requests/urllib libraries.
2017-09-08 16:27:28 +02:00
2017-11-24 15:25:38 +01:00
`MastodonRatelimitError` is raised when you hit an API rate limit. You should try
2017-09-08 16:27:28 +02:00
again after a while (see the rate limiting section above).
2019-04-27 18:46:20 +02:00
`MastodonServerError` is raised when the server throws an internal error, likely due
to server misconfiguration.
2017-12-14 00:27:34 +01:00
`MastodonVersionError` is raised when a version check for an API call fails.
2018-06-04 15:00:25 +02:00
Return values
-------------
2017-09-08 16:27:28 +02:00
Unless otherwise specified, all data is returned as python dictionaries, matching
the JSON format used by the API. Dates returned by the API are in ISO 8601 format
and are parsed into python datetime objects.
2016-11-24 03:03:52 +01:00
2017-12-14 13:29:42 +01:00
To make access easier, the dictionaries returned are wrapped by a class that adds
read-only attributes for all dict values - this means that, for example, instead of
writing
.. code-block:: python
description = mastodon.account_verify_credentials()["source"]["note"]
you can also just write
.. code-block:: python
description = mastodon.account_verify_credentials().source.note
2018-06-04 16:48:20 +02:00
and everything will work as intended. The class used for this is exposed as
`AttribAccessDict`.
2017-12-14 13:29:42 +01:00
User dicts
~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _user dict:
.. code-block:: python
2016-12-13 17:28:32 +01:00
mastodon.account(<numerical id>)
# Returns the following dictionary:
{
'id': # Same as <numerical id>
'username': # The username (what you @ them with)
'acct': # The user's account name as username@domain (@domain omitted for local users)
'display_name': # The user's display name
'locked': # Denotes whether the account can be followed without a follow request
2017-09-08 16:27:28 +02:00
'created_at': # Account creation time
'following_count': # How many people they follow
'followers_count': # How many followers they have
'statuses_count': # How many statuses they have
'note': # Their bio
'url': # Their URL; usually 'https://mastodon.social/users/<acct>'
2017-09-08 16:27:28 +02:00
'avatar': # URL for their avatar, can be animated
'header': # URL for their header image, can be animated
'avatar_static': # URL for their avatar, never animated
'header_static': # URL for their header image, never animated
2017-12-14 00:30:51 +01:00
'source': # Additional information - only present for user dict returned
# from account_verify_credentials()
2017-12-14 13:29:42 +01:00
'moved_to_account': # If set, an account dict of the account this user has
# set up as their moved-to address.
2018-06-04 17:58:11 +02:00
'bot': # Boolean indicating whether this account is automated.
2018-06-05 01:54:12 +02:00
'fields': # List of up to four dicts with free-form 'name' and 'value' profile info.
2016-12-13 17:28:32 +01:00
}
2017-12-13 17:11:45 +01:00
mastodon.account_verify_credentials()["source"]
# Returns the following dictionary:
{
'privacy': # The users default visibility setting ("private", "unlisted" or "public")
'sensitive': # Denotes whether user media should be marked sensitive by default
'note': # Plain text version of the users bio
}
Toot dicts
~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _toot dict:
.. code-block:: python
2016-12-13 17:28:32 +01:00
mastodon.toot("Hello from Python")
# Returns the following dictionary:
{
'id': # Numerical id of this toot
2017-04-26 12:19:41 +02:00
'uri': # Descriptor for the toot
# EG 'tag:mastodon.social,2016-11-25:objectId=<id>:objectType=Status'
'url': # URL of the toot
2017-11-24 14:04:43 +01:00
'account': # User dict for the account which posted the status
2017-04-26 12:19:41 +02:00
'in_reply_to_id': # Numerical id of the toot this toot is in response to
'in_reply_to_account_id': # Numerical id of the account this toot is in response to
2017-11-24 17:22:48 +01:00
'reblog': # Denotes whether the toot is a reblog. If so, set to the original toot dict.
2017-04-26 12:19:41 +02:00
'content': # Content of the toot, as HTML: '<p>Hello from Python</p>'
'created_at': # Creation time
'reblogs_count': # Number of reblogs
'favourites_count': # Number of favourites
'reblogged': # Denotes whether the logged in user has boosted this toot
2017-04-26 12:19:41 +02:00
'favourited': # Denotes whether the logged in user has favourited this toot
'sensitive': # Denotes whether media attachments to the toot are marked sensitive
'spoiler_text': # Warning text that should be displayed before the toot content
'visibility': # Toot visibility ('public', 'unlisted', 'private', or 'direct')
2017-09-08 16:27:28 +02:00
'mentions': # A list of users dicts mentioned in the toot, as Mention dicts
2017-11-21 14:22:58 +01:00
'media_attachments': # A list of media dicts of attached files
'emojis': # A list of custom emojis used in the toot, as Emoji dicts
2017-09-08 16:27:28 +02:00
'tags': # A list of hashtag used in the toot, as Hashtag dicts
2017-12-15 11:49:09 +01:00
'application': # Application dict for the client used to post the toot (Does not federate
# and is therefore always None for remote toots, can also be None for
# local toots for some legacy applications).
2018-07-30 15:22:11 +02:00
'language': # The language of the toot, if specified by the server,
2019-04-27 17:38:54 +02:00
# as ISO 639-1 (two-letter) language code.
2017-12-14 00:30:51 +01:00
'muted': # Boolean denoting whether the user has muted this status by
# way of conversation muting
2018-06-04 14:54:26 +02:00
'pinned': # Boolean denoting whether or not the status is currently pinned for the
# associated account.
2016-12-13 17:28:32 +01:00
}
2017-09-08 16:27:28 +02:00
Mention dicts
~~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _mention dict:
2017-09-08 16:27:28 +02:00
.. code-block:: python
2017-11-21 14:22:58 +01:00
2017-09-08 16:27:28 +02:00
{
'url': # Mentioned users profile URL (potentially remote)
'username': # Mentioned users user name (not including domain)
'acct': # Mentioned users account name (including domain)
'id': # Mentioned users (local) account ID
}
Hashtag dicts
~~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _hashtag dict:
2017-09-08 16:27:28 +02:00
.. code-block:: python
2017-11-21 14:22:58 +01:00
2017-09-08 16:27:28 +02:00
{
'name': # Hashtag name (not including the #)
'url': # Hashtag URL (can be remote)
2018-07-30 15:35:36 +02:00
'history': # List of usage history dicts for up to 7 days. Not present in statuses.
2017-09-08 16:27:28 +02:00
}
2018-07-30 15:35:36 +02:00
Hashtag usage history dicts
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _hashtag usage history dict:
.. code-block:: python
{
'day': # Date of the day this history dict is for
'uses': # Number of statuses using this hashtag on that day
'accounts': # Number of accounts using this hashtag in at least one status on that day
}
2017-11-21 14:22:58 +01:00
Emoji dicts
~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _emoji dict:
2017-11-21 14:22:58 +01:00
.. code-block:: python
{
2017-11-21 14:24:02 +01:00
'shortcode': # Emoji shortcode, without surrounding colons
2017-11-21 14:22:58 +01:00
'url': # URL for the emoji image, can be animated
'static_url': # URL for the emoji image, never animated
}
2017-12-14 10:51:55 +01:00
Application dicts
~~~~~~~~~~~~~~~~~
.. _application dict:
.. code-block:: python
{
'name': # The applications name
'website': # The applications website
}
Relationship dicts
~~~~~~~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _relationship dict:
.. code-block:: python
mastodon.account_follow(<numerical id>)
2016-11-25 20:46:20 +01:00
# Returns the following dictionary:
{
2017-04-26 12:19:41 +02:00
'id': # Numerical id (same one as <numerical id>)
2017-06-15 22:32:17 +02:00
'following': # Boolean denoting whether the logged-in user follows the specified user
'followed_by': # Boolean denoting whether the specified user follows the logged-in user
'blocking': # Boolean denoting whether the logged-in user has blocked the specified user
'muting': # Boolean denoting whether the logged-in user has muted the specified user
2018-07-30 15:16:46 +02:00
'muting_notifications': # Boolean denoting wheter the logged-in user has muted notifications
# related to the specified user
2017-12-14 00:30:51 +01:00
'requested': # Boolean denoting whether the logged-in user has sent the specified
# user a follow request
'domain_blocking': # Boolean denoting whether the logged-in user has blocked the
# specified users domain
2018-07-30 15:16:46 +02:00
'showing_reblogs': # Boolean denoting whether the specified users reblogs show up on the
# logged-in users Timeline
}
2018-07-30 17:29:37 +02:00
Filter dicts
~~~~~~~~~~~~
.. _filter dict:
.. code-block:: python
mastodon.account_follow(<numerical id>)
# Returns the following dictionary:
{
'id': # Numerical id of the filter
'phrase': # Filtered keyword or phrase
'context': # List of places where the filters are applied ('home', 'notifications', 'public', 'thread')
'expires_at': # Expiry date for the filter
'irreversible': # Boolean denoting if this filter is executed server-side
2018-07-30 20:25:25 +02:00
# or if it should be ran client-side.
2018-07-30 17:29:37 +02:00
'whole_word': # Boolean denoting whether this filter can match partial words
}
2016-11-25 20:46:20 +01:00
Notification dicts
~~~~~~~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _notification dict:
2016-11-25 20:46:20 +01:00
.. code-block:: python
2016-11-25 20:47:48 +01:00
mastodon.notifications()[0]
2016-11-25 20:46:20 +01:00
# Returns the following dictionary:
{
2017-12-13 21:16:02 +01:00
'id': # id of the notification
'type': # "mention", "reblog", "favourite" or "follow"
'created_at': # The time the notification was created
'account': # User dict of the user from whom the notification originates
'status': # In case of "mention", the mentioning status
# In case of reblog / favourite, the reblogged / favourited status
2016-11-25 20:46:20 +01:00
}
Context dicts
~~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _context dict:
.. code-block:: python
mastodon.status_context(<numerical id>)
2016-11-25 20:46:20 +01:00
# Returns the following dictionary:
{
2017-04-26 12:19:41 +02:00
'ancestors': # A list of toot dicts
'descendants': # A list of toot dicts
}
2017-12-13 21:16:02 +01:00
List dicts
~~~~~~~~~~
.. _list dict:
.. code-block:: python
mastodon.list(<numerical id>)
# Returns the following dictionary:
{
'id': # id of the list
'title': # title of the list
}
Media dicts
~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _media dict:
.. code-block:: python
mastodon.media_post("image.jpg", "image/jpeg")
2016-11-25 20:46:20 +01:00
# Returns the following dictionary:
{
'id': # The ID of the attachment.
2017-11-21 14:22:58 +01:00
'type': # Media type: 'image', 'video', 'gifv' or 'unknown'.
'url': # The URL for the image in the local cache
'remote_url': # The remote URL for the media (if the image is from a remote instance)
'preview_url': # The URL for the media preview
'text_url': # The display text for the media (what shows up in toots)
2017-12-14 00:30:51 +01:00
'meta': # Dictionary of two image metadata dicts (see below),
2018-04-17 17:49:08 +02:00
# 'original' and 'small' (preview). Either may be empty.
# May additionally contain an "fps" field giving a videos frames per second (possibly
# rounded), and a "length" field giving a videos length in a human-readable format.
# Note that a video may have an image as preview.
2018-06-04 21:20:43 +02:00
# May also contain a 'focus' dict.
}
2017-06-15 22:32:17 +02:00
2018-04-17 17:49:08 +02:00
# Metadata dicts (image) - all fields are optional:
2017-06-15 22:32:17 +02:00
{
'width': # Width of the image in pixels
'height': # Height of the image in pixels
'aspect': # Aspect ratio of the image as a floating point number
'size': # Textual representation of the image size in pixels, e.g. '800x600'
}
2018-04-17 17:49:08 +02:00
# Metadata dicts (video, gifv) - all fields are optional:
{
'width': # Width of the video in pixels
'heigh': # Height of the video in pixels
2018-05-06 16:02:26 +02:00
'frame_rate': # Exact frame rate of the video in frames per second.
# Can be an integer fraction (i.e. "20/7")
2018-04-17 17:49:08 +02:00
'duration': # Duration of the video in seconds
'bitrate': # Average bit-rate of the video in bytes per second
}
2018-05-06 16:02:26 +02:00
2018-06-04 21:20:43 +02:00
# Focus Metadata dict:
{
'x': Focus point x coordinate (between -1 and 1)
'y': Focus point x coordinate (between -1 and 1)
}
Card dicts
~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _card dict:
2017-04-27 10:43:10 +02:00
.. code-block:: python
mastodon.status_card(<numerical id>):
2017-12-14 00:33:22 +01:00
# Returns the following dictionary
{
2017-04-27 10:43:10 +02:00
'url': # The URL of the card.
'title': # The title of the card.
'description': # The description of the card.
2017-06-15 22:32:17 +02:00
'type': # Embed type: 'link', 'photo', 'video', or 'rich'
2017-04-27 10:43:10 +02:00
'image': # (optional) The image associated with the card.
2017-06-15 22:32:17 +02:00
# OEmbed data (all optional):
'author_name': # Name of the embedded contents author
'author_url': # URL pointing to the embedded contents author
'description': # Description of the embedded content
'width': # Width of the embedded object
'height': # Height of the embedded object
'html': # HTML string of the embed
'provider_name': # Name of the provider from which the embed originates
'provider_url': # URL pointing to the embeds provider
}
2017-12-13 17:55:14 +01:00
Search result dicts
~~~~~~~~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _search result dict:
2017-12-13 17:55:14 +01:00
.. code-block:: python
2017-12-13 18:40:17 +01:00
mastodon.search("<query>")
2017-12-14 00:33:22 +01:00
# Returns the following dictionary
2017-12-13 17:55:14 +01:00
{
'accounts': # List of account dicts resulting from the query
'hashtags': # List of hashtag dicts resulting from the query
'statuses': # List of toot dicts resulting from the query
}
2017-06-15 22:48:23 +02:00
Instance dicts
~~~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _instance dict:
2017-06-15 22:48:23 +02:00
.. code-block:: python
mastodon.instance()
2017-12-14 00:33:22 +01:00
# Returns the following dictionary
2017-06-15 22:48:23 +02:00
{
'description': # A brief instance description set by the admin
'email': # The admin contact e-mail
'title': # The instances title
'uri': # The instances URL
'version': # The instances mastodon version
2017-12-14 00:30:51 +01:00
'urls': # Additional URLs dict, presently only 'streaming_api' with the
# stream websocket address.
2018-04-17 17:49:08 +02:00
'contact_account': # Account dict of the primary contact for the instance.
2019-04-27 17:38:54 +02:00
'languages': # Array of ISO 639-1 (two-letter) language codes the instance
# has chosen to advertise.
2017-06-15 22:48:23 +02:00
}
2018-01-29 14:14:58 +01:00
Activity dicts
~~~~~~~~~~~~~~
.. _activity dict:
.. code-block:: python
mastodon.instance_activity()[0]
# Returns the following dictionary
{
'week': # Date of the first day of the week the stats were collected for
'logins': # Number of users that logged in that week
'registrations': # Number of new users that week
'statuses': # Number of statuses posted that week
}
2017-11-21 14:33:04 +01:00
Report dicts
~~~~~~~~~~~~
2017-12-13 18:40:17 +01:00
.. _report dict:
2017-11-21 14:33:04 +01:00
.. code-block:: python
mastodon.reports()[0]
2017-12-14 00:33:22 +01:00
# Returns the following dictionary
2017-11-21 14:33:04 +01:00
{
'id': # Numerical id of the report
'action_taken': # True if a moderator or admin has processed the
2017-12-14 00:30:51 +01:00
# report, False otherwise. Note that no indication as to
# what action was taken is given and that an admin simply
# marking the report as processed and not doing anything else
2017-11-21 14:33:04 +01:00
# will set this field to True.
}
2018-06-05 14:10:53 +02:00
Push subscription dicts
~~~~~~~~~~~~~~~~~~~~~~~
.. _push subscription dict:
.. code-block:: python
mastodon.push_subscription()
# Returns the following dictionary
{
'id': # Numerical id of the push subscription
'endpoint': # Endpoint URL for the subscription
'server_key': # Server pubkey used for signature verification
'alerts': # Subscribed events - dict that may contain keys 'follow',
# 'favourite', 'reblog' and 'mention', with value True
# if webpushes have been requested for those events.
}
2018-06-05 17:19:15 +02:00
Push notification dicts
~~~~~~~~~~~~~~~~~~~~~~~
.. _push notification dict:
.. code-block:: python
mastodon.push_subscription_decrypt_push(...)
# Returns the following dictionary
{
'access_token': # Access token that can be used to access the API as the
# notified user
'body': # Text body of the notification
'icon': # URL to an icon for the notification
'notification_id': # ID that can be passed to notification() to get the full
# notification object,
'notification_type': # 'mention', 'reblog', 'follow' or 'favourite'
'preferred_locale': # The users preferred locale
'title': # Title for the notification
}
2016-11-24 03:03:52 +01:00
App registration and user authentication
----------------------------------------
Before you can use the mastodon API, you have to register your
application (which gets you a client key and client secret)
and then log in (which gets you an access token). These functions
allow you to do those things.
For convenience, once you have a client id, secret and access token,
you can simply pass them to the constructor of the class, too!
Note that while it is perfectly reasonable to log back in whenever
your app starts, registering a new application on every
startup is not, so don't do that - instead, register an application
2017-06-16 01:23:19 +02:00
once, and then persist your client id and secret. A convenient method
for this is provided by the functions dealing with registering the app,
logging in and the Mastodon classes constructor.
2016-11-24 02:27:00 +01:00
To talk to an instance different from the flagship instance, specify
the api_base_url (usually, just the URL of the instance, i.e.
https://mastodon.social/ for the flagship instance). If no protocol
is specified, Mastodon.py defaults to https.
2016-11-24 03:03:52 +01:00
.. automethod:: Mastodon.create_app
.. automethod:: Mastodon.__init__
2017-12-13 18:40:17 +01:00
.. _log_in():
2016-11-24 03:03:52 +01:00
.. automethod:: Mastodon.log_in
2017-04-26 12:19:41 +02:00
.. automethod:: Mastodon.auth_request_url
2016-11-24 03:03:52 +01:00
2017-12-11 14:41:15 +01:00
Versioning
----------
Mastodon.py will check if a certain endpoint is available before doing API
calls. By default, it checks against the version of Mastodon retrieved on
2017-12-14 00:27:34 +01:00
init(), or the version you specified. Mastodon.py can be set (in the
2017-12-14 13:29:42 +01:00
constructor) to either check if an endpoint is available at all (this is the
default) or to check if the endpoint is available and behaves as in the newest
Mastodon version (with regards to parameters as well as return values).
Version checking can also be disabled altogether. If a version check fails,
Mastodon.py throws a `MastodonVersionError`.
2017-12-14 00:27:34 +01:00
With the following functions, you can make Mastodon.py re-check the server
version or explicitly determine if a specific minimum Version is available.
2017-12-11 16:23:22 +01:00
2017-12-11 14:41:15 +01:00
.. automethod:: Mastodon.retrieve_mastodon_version
.. automethod:: Mastodon.verify_minimum_version
2017-06-15 22:48:23 +02:00
Reading data: Instances
-----------------------
2018-01-29 14:14:58 +01:00
These functions allow you to fetch information associated with the
current instance.
.. automethod:: Mastodon.instance
2018-01-29 14:14:58 +01:00
.. automethod:: Mastodon.instance_activity
.. automethod:: Mastodon.instance_peers
2016-11-24 03:03:52 +01:00
Reading data: Timelines
-----------------------
This function allows you to access the timelines a logged in
2019-04-27 17:44:05 +02:00
user could see, as well as hashtag timelines and the public (federated)
and local timelines.
2016-11-24 03:03:52 +01:00
2017-12-13 18:40:17 +01:00
.. _timeline():
2016-11-24 03:03:52 +01:00
.. automethod:: Mastodon.timeline
.. automethod:: Mastodon.timeline_home
2017-04-02 19:35:42 +02:00
.. automethod:: Mastodon.timeline_local
.. automethod:: Mastodon.timeline_public
2017-12-13 18:40:17 +01:00
.. _timeline_hashtag():
.. automethod:: Mastodon.timeline_hashtag
2017-12-13 21:16:02 +01:00
.. automethod:: Mastodon.timeline_list
2016-11-24 03:03:52 +01:00
Reading data: Statuses
----------------------
These functions allow you to get information about single statuses.
.. automethod:: Mastodon.status
.. automethod:: Mastodon.status_context
.. automethod:: Mastodon.status_reblogged_by
.. automethod:: Mastodon.status_favourited_by
.. automethod:: Mastodon.status_card
2016-11-24 03:03:52 +01:00
Reading data: Notifications
---------------------------
This function allows you to get information about a users notifications.
.. automethod:: Mastodon.notifications
2016-11-24 03:03:52 +01:00
Reading data: Accounts
----------------------
These functions allow you to get information about accounts and
their relationships.
.. automethod:: Mastodon.account
2016-11-24 19:31:46 +01:00
.. automethod:: Mastodon.account_verify_credentials
2016-11-24 03:03:52 +01:00
.. automethod:: Mastodon.account_statuses
.. automethod:: Mastodon.account_following
.. automethod:: Mastodon.account_followers
.. automethod:: Mastodon.account_relationships
.. automethod:: Mastodon.account_search
2018-07-30 18:03:11 +02:00
Reading data: Keyword filters
2018-07-30 17:29:37 +02:00
-----------------------------
These functions allow you to get information about keyword filters.
.. automethod:: Mastodon.filters
.. automethod:: Mastodon.filter
2018-07-30 22:09:14 +02:00
.. automethod:: Mastodon.filters_apply
2018-07-30 17:29:37 +02:00
2018-07-30 16:20:56 +02:00
Reading data: Follow suggestions
--------------------------------
.. automethod:: Mastodon.suggestions
2017-12-13 22:33:29 +01:00
Reading data: Lists
-------------------
These functions allow you to view information about lists.
.. automethod:: Mastodon.lists
.. automethod:: Mastodon.list
.. automethod:: Mastodon.list_accounts
Reading data: Follows
---------------------
.. automethod:: Mastodon.follows
2017-06-15 22:48:23 +02:00
Reading data: Favourites
------------------------
.. automethod:: Mastodon.favourites
Reading data: Follow requests
-----------------------------
.. automethod:: Mastodon.follow_requests
2017-04-26 12:19:41 +02:00
Reading data: Searching
-----------------------
.. automethod:: Mastodon.search
2018-07-30 15:39:46 +02:00
.. automethod:: Mastodon.search_v2
2017-04-26 12:19:41 +02:00
2018-07-30 15:54:17 +02:00
Reading data: Trends
--------------------
.. automethod:: Mastodon.trends
Reading data: Mutes and blocks
------------------------------
These functions allow you to get information about accounts that are
muted or blocked by the logged in user.
.. automethod:: Mastodon.mutes
.. automethod:: Mastodon.blocks
Reading data: Reports
------------------------------
.. automethod:: Mastodon.reports
2017-04-03 05:53:32 +02:00
2017-06-15 23:13:34 +02:00
Reading data: Domain blocks
---------------------------
.. automethod:: Mastodon.domain_blocks
2017-12-13 17:17:42 +01:00
Reading data: Emoji
-------------------
.. automethod:: Mastodon.custom_emojis
2016-11-24 03:03:52 +01:00
Writing data: Statuses
----------------------
These functions allow you to post statuses to Mastodon and to
interact with already posted statuses.
2017-12-13 18:40:17 +01:00
.. _status_post():
2016-11-24 03:03:52 +01:00
.. automethod:: Mastodon.status_post
2018-07-30 14:37:20 +02:00
.. automethod:: Mastodon.status_reply
2016-11-24 03:03:52 +01:00
.. automethod:: Mastodon.toot
.. automethod:: Mastodon.status_reblog
.. automethod:: Mastodon.status_unreblog
.. automethod:: Mastodon.status_favourite
.. automethod:: Mastodon.status_unfavourite
2017-09-08 16:34:11 +02:00
.. automethod:: Mastodon.status_mute
.. automethod:: Mastodon.status_unmute
2018-06-04 14:54:26 +02:00
.. automethod:: Mastodon.status_pin
.. automethod:: Mastodon.status_unpin
2016-11-25 18:52:55 +01:00
.. automethod:: Mastodon.status_delete
2017-09-08 15:26:05 +02:00
Writing data: Notifications
---------------------------
These functions allow you to clear all or some notifications.
.. automethod:: Mastodon.notifications_clear
.. automethod:: Mastodon.notifications_dismiss
2016-11-24 03:03:52 +01:00
Writing data: Accounts
----------------------
These functions allow you to interact with other accounts: To (un)follow and
(un)block.
2016-11-24 02:27:00 +01:00
2017-04-26 13:24:27 +02:00
.. automethod:: Mastodon.account_follow
.. automethod:: Mastodon.follows
2016-11-24 03:03:52 +01:00
.. automethod:: Mastodon.account_unfollow
.. automethod:: Mastodon.account_block
.. automethod:: Mastodon.account_unblock
.. automethod:: Mastodon.account_mute
.. automethod:: Mastodon.account_unmute
.. automethod:: Mastodon.account_update_credentials
2018-07-30 17:29:37 +02:00
Writing data: Keyword filters
-----------------------------
These functions allow you to manipulate keyword filters.
.. automethod:: Mastodon.filter_create
.. automethod:: Mastodon.filter_update
.. automethod:: Mastodon.filter_delete
2018-07-30 16:20:56 +02:00
Writing data: Follow suggestions
--------------------------------
.. automethod:: Mastodon.suggestion_delete
2017-12-13 22:33:29 +01:00
Writing data: Lists
-------------------
These functions allow you to create, maintain and delete lists.
When creating lists, note that a user can only
2017-12-13 22:33:29 +01:00
have a maximum of 50 lists.
.. automethod:: Mastodon.list_create
.. automethod:: Mastodon.list_update
.. automethod:: Mastodon.list_delete
.. automethod:: Mastodon.list_accounts_add
.. automethod:: Mastodon.list_accounts_delete
2017-04-03 05:53:32 +02:00
Writing data: Follow requests
-----------------------------
These functions allow you to accept or reject incoming follow requests.
.. automethod:: Mastodon.follow_request_authorize
.. automethod:: Mastodon.follow_request_reject
2016-11-24 03:03:52 +01:00
Writing data: Media
-------------------
2016-11-24 13:18:13 +01:00
This function allows you to upload media to Mastodon. The returned
media IDs (Up to 4 at the same time) can then be used with post_status
to attach media to statuses.
2016-11-24 03:07:07 +01:00
2017-12-13 18:40:17 +01:00
.. _media_post():
2016-11-24 03:07:07 +01:00
.. automethod:: Mastodon.media_post
2018-06-04 14:54:26 +02:00
.. automethod:: Mastodon.media_update
2017-06-15 22:48:23 +02:00
Writing data: Reports
---------------------
.. automethod:: Mastodon.report
2017-06-15 23:13:34 +02:00
Writing data: Domain blocks
---------------------------
2017-06-16 01:23:19 +02:00
These functions allow you to block and unblock all statuses from a domain
2017-06-15 23:13:34 +02:00
for the logged-in user.
.. automethod:: Mastodon.domain_block
.. automethod:: Mastodon.domain_unblock
2017-06-16 01:23:19 +02:00
Pagination
----------
These functions allow for convenient retrieval of paginated data.
.. automethod:: Mastodon.fetch_next
.. automethod:: Mastodon.fetch_previous
.. automethod:: Mastodon.fetch_remaining
2017-04-26 12:19:41 +02:00
Streaming
---------
These functions allow access to the streaming API.
2018-04-17 15:22:07 +02:00
If `async` is False, these methods block forever (or until an error is encountered).
2017-11-24 15:08:34 +01:00
2018-04-17 15:10:40 +02:00
If `async` is True, the listener will listen on another thread and these methods
will return a handle corresponding to the open connection. If, in addition, `async_reconnect` is True,
the thread will attempt to reconnect to the streaming API if any errors are encountered, waiting
`async_reconnect_wait_sec` seconds between reconnection attempts. Note that no effort is made
2018-04-17 16:15:54 +02:00
to "catch up" - events created while the connection is broken will not be received. If you need to make
2018-06-05 17:37:11 +02:00
sure to get absolutely all notifications / deletes / toots, you will have to do that manually, e.g.
using the `on_abort` handler to fill in events since the last received one and then reconnecting.
2018-04-17 15:10:40 +02:00
The connection may be closed at any time by calling the handles close() method. The
current status of the handler thread can be checked with the handles is_alive() function,
and the streaming status can be checked by calling is_receiving().
2017-11-24 15:08:34 +01:00
2017-12-13 17:26:44 +01:00
The streaming functions take instances of `StreamListener` as the `listener` parameter.
2017-11-24 15:25:38 +01:00
A `CallbackStreamListener` class that allows you to specify function callbacks
directly is included for convenience.
2018-04-17 15:22:07 +02:00
When in not-async mode or async mode without async_reconnect, the stream functions may raise
various exceptions: `MastodonMalformedEventError` if a received event cannot be parsed and
`MastodonNetworkError` if any connection problems occur.
2017-11-24 15:25:38 +01:00
.. automethod:: Mastodon.stream_user
.. automethod:: Mastodon.stream_public
.. automethod:: Mastodon.stream_local
.. automethod:: Mastodon.stream_hashtag
2017-12-13 17:26:44 +01:00
.. automethod:: Mastodon.stream_list
2017-11-24 15:25:38 +01:00
StreamListener
~~~~~~~~~~~~~~
.. autoclass:: StreamListener
.. automethod:: StreamListener.on_update
.. automethod:: StreamListener.on_notification
.. automethod:: StreamListener.on_delete
2018-06-05 14:10:53 +02:00
.. automethod:: StreamListener.on_abort
2017-11-24 15:25:38 +01:00
.. automethod:: StreamListener.handle_heartbeat
CallbackStreamListener
~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: CallbackStreamListener
2017-04-26 12:19:41 +02:00
2018-06-05 14:10:53 +02:00
Push subscriptions
------------------
These functions allow you to manage webpush subscriptions and to decrypt received
pushes. Note that the intended setup is not mastodon pushing directly to a users client -
the push endpoint should usually be a relay server that then takes care of delivering the
(encrypted) push to the end user via some mechanism, where it can then be decrypted and
displayed.
Mastodon allows an application to have one webpush subscription per user at a time.
.. automethod:: Mastodon.push_subscription
.. automethod:: Mastodon.push_subscription_set
.. automethod:: Mastodon.push_subscription_update
2018-06-05 14:30:47 +02:00
.. _push_subscription_generate_keys():
2018-06-05 14:10:53 +02:00
.. automethod:: Mastodon.push_subscription_generate_keys
.. automethod:: Mastodon.push_subscription_decrypt_push
2017-12-14 13:42:13 +01:00
Acknowledgements
----------------
Mastodon.py contains work by a large amount of contributors, many of which have
put significant work into making it a better library. You can find some information
about who helped with which particular feature or fix in the changelog.
2018-06-05 14:10:53 +02:00
.. _Mastodon: https://github.com/tootsuite/mastodon
.. _Mastodon flagship instance: http://mastodon.social/
2018-07-30 20:25:25 +02:00
.. _Mastodon api docs: https://github.com/tootsuite/documentation/