<commit_before>Mastodon.py
===========
.. py:module:: mastodon
.. py:class: Mastodon

.. code-block:: python

   from mastodon import Mastodon

   # Register app - only once!
   '''
   Mastodon.create_app(
        'pytooterapp',
        api_base_url = 'https://mastodon.social',
        to_file = 'pytooter_clientcred.secret'
   )
   '''

   # Log in - either every time, or use persisted
   '''
   mastodon = Mastodon(
       client_id = 'pytooter_clientcred.secret',
       api_base_url = 'https://mastodon.social'
   )
   mastodon.log_in(
       '<EMAIL>',
       '<PASSWORD>',
       to_file = 'pytooter_usercred.secret'
   )
   '''

   # Create actual API instance
   mastodon = Mastodon(
       client_id = 'pytooter_clientcred.secret', 
       access_token = '<PASSWORD>',
       api_base_url = 'https://mastodon.social'
   )
   mastodon.toot('Tooting from python using #mastodonpy !')

`Mastodon`_ is an ostatus based twitter-like federated social 
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 
node running Mastodon by setting api_base_url when creating the
api object (or creating an app).

Mastodon.py aims to implement the complete public Mastodon API. As
of this time, it is feature complete for Mastodon version 2.0.0.

A note about rate limits
------------------------
Mastodons API rate limits per IP. By default, the limit is 300 requests per 5 minute 
time slot. This can differ from instance to instance and is subject to change.
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.

In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When
a request hits the rate limit, it simply throws a MastodonRateLimitError. This is
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).

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
constructor as ratelimit_pacefactor) of the rate limit will be used up. The fraction can
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.

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
instance may choose to return less results than you requested.

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 
next page) item of the returned list as _pagination_prev and _pagination_next.

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.

A note about IDs
----------------
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.

Error handling
--------------
When Mastodon.py encounters an error, it will raise an exception, generally with
some text included to tell you what went wrong. 

The base class that all mastodon exceptions inherit from is the MastodonError
class. 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.

MastodonIllegalArgumentError is generally a programming problem - you asked the 
API to do something obviously invalid (i.e. specify a privacy scope that does
not exist).

MastodonFileNotFoundError and MastodonNetworkError are IO errors - could be you
specified a wrong URL, could be the internet is down or your hard drive is
dying. They inherit from MastodonIOError, for easy catching.

MastodonAPIError is an error returned from the Mastodon instance - the server
has decided it can't fullfill your request (i.e. you requested info on a user that
does not exist).

MastodonRatelimitError is raised when you hit an API rate limit. You should try 
again after a while (see the rate limiting section above).

Return values
-------------
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.

User dicts
~~~~~~~~~~
.. code-block:: python

    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
        '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>'
        '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
    }

Toot dicts
~~~~~~~~~~
.. code-block:: python

    mastodon.toot("Hello from Python")
    # Returns the following dictionary:
    {
        'id': # Numerical id of this toot
        'uri': # Descriptor for the toot
            # EG 'tag:mastodon.social,2016-11-25:objectId=<id>:objectType=Status'
        'url': # URL of the toot
        'account': # Account dict for the account which posted the status
        '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
        'reblog': # Denotes whether the toot is a reblog<commit_msg>Fix doc inaccuracy<commit_after>Mastodon.py
===========
.. py:module:: mastodon
.. py:class: Mastodon

.. code-block:: python

   from mastodon import Mastodon

   # Register app - only once!
   '''
   Mastodon.create_app(
        'pytooterapp',
        api_base_url = 'https://mastodon.social',
        to_file = 'pytooter_clientcred.secret'
   )
   '''

   # Log in - either every time, or use persisted
   '''
   mastodon = Mastodon(
       client_id = 'pytooter_clientcred.secret',
       api_base_url = 'https://mastodon.social'
   )
   mastodon.log_in(
       '<EMAIL>',
       '<PASSWORD>',
       to_file = 'pytooter_usercred.secret'
   )
   '''

   # Create actual API instance
   mastodon = Mastodon(
       client_id = 'pytooter_clientcred.secret', 
       access_token = '<PASSWORD>',
       api_base_url = 'https://mastodon.social'
   )
   mastodon.toot('Tooting from python using #mastodonpy !')

`Mastodon`_ is an ostatus based twitter-like federated social 
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 
node running Mastodon by setting api_base_url when creating the
api object (or creating an app).

Mastodon.py aims to implement the complete public Mastodon API. As
of this time, it is feature complete for Mastodon version 2.0.0.

A note about rate limits
------------------------
Mastodons API rate limits per IP. By default, the limit is 300 requests per 5 minute 
time slot. This can differ from instance to instance and is subject to change.
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.

In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When
a request hits the rate limit, it simply throws a MastodonRateLimitError. This is
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).

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
constructor as ratelimit_pacefactor) of the rate limit will be used up. The fraction can
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.

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
instance may choose to return less results than you requested.

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 
next page) item of the returned list as _pagination_prev and _pagination_next.

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.

A note about IDs
----------------
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.

Error handling
--------------
When Mastodon.py encounters an error, it will raise an exception, generally with
some text included to tell you what went wrong. 

The base class that all mastodon exceptions inherit from is the MastodonError
class. 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.

MastodonIllegalArgumentError is generally a programming problem - you asked the 
API to do something obviously invalid (i.e. specify a privacy scope that does
not exist).

MastodonFileNotFoundError and MastodonNetworkError are IO errors - could be you
specified a wrong URL, could be the internet is down or your hard drive is
dying. They inherit from MastodonIOError, for easy catching.

MastodonAPIError is an error returned from the Mastodon instance - the server
has decided it can't fullfill your request (i.e. you requested info on a user that
does not exist).

MastodonRatelimitError is raised when you hit an API rate limit. You should try 
again after a while (see the rate limiting section above).

Return values
-------------
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.

User dicts
~~~~~~~~~~
.. code-block:: python

    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
        '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>'
        '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
    }

Toot dicts
~~~~~~~~~~
.. code-block:: python

    mastodon.toot("Hello from Python")
    # Returns the following dictionary:
    {
        'id': # Numerical id of this toot
        'uri': # Descriptor for the toot
            # EG 'tag:mastodon.social,2016-11-25:objectId=<id>:objectType=Status'
        'url': # URL of the toot
        'account': # User dict for the account which posted the status
        '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
        'reblog': # Denotes whether the toot is a reblog
{'fraction_non_alphanumeric': 0.07219285806368442, 'fraction_numerical': 0.0024494005414464354, 'mean_word_length': 3.4064186310707187, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2917823', 'n_tokens_mistral': 4291, 'n_tokens_neox': 4093, 'n_words': 2191}