geotribu_cli.social.mastodon_client module#

class geotribu_cli.social.mastodon_client.ExtendedMastodonClient(client_id=None, client_secret=None, access_token=None, api_base_url='https://mapstodon.space/', debug_requests=None, ratelimit_method='wait', ratelimit_pacefactor=1.1, request_timeout=60, mastodon_version=None, version_check_mode='created', session=None, feature_set='mainline', user_agent='GeotribuToolbelt/0.32.0', lang='fra')#

Bases : Mastodon

Extended Mastodon client with custom methods and attributes.

Lève:

MastodonError – if access token is not set

__init__(client_id=None, client_secret=None, access_token=None, api_base_url='https://mapstodon.space/', debug_requests=None, ratelimit_method='wait', ratelimit_pacefactor=1.1, request_timeout=60, mastodon_version=None, version_check_mode='created', session=None, feature_set='mainline', user_agent='GeotribuToolbelt/0.32.0', lang='fra')#

Create a new API wrapper instance based on the given client_secret and client_id on the instance given by api_base_url. If you give a client_id and it is not a file, you must also give a secret. If you specify an access_token then you don’t need to specify a client_id. It is allowed to specify neither - in this case, you will be restricted to only using endpoints that do not require authentication. If a file is given as client_id, client ID, secret and base url are read from that file.

You can also specify an access_token, directly or as a file (as written by log_in()). If a file is given, Mastodon.py also tries to load the base URL from this file, if present. A client id and secret are not required in this case.

Mastodon.py can try to respect rate limits in several ways, controlled by ratelimit_method. « throw » makes functions throw a MastodonRatelimitError when the rate limit is hit. « wait » mode will, once the limit is hit, wait and retry the request as soon as the rate limit resets, until it succeeds. « pace » works like throw, but tries to wait in between calls so that the limit is generally not hit (how hard it tries to avoid hitting the rate limit can be controlled by ratelimit_pacefactor). The default setting is « wait ». Note that even in « wait » and « pace » mode, requests can still fail due to network or other problems! Also note that « pace » and « wait » are NOT thread safe.

By default, a timeout of 300 seconds is used for all requests. If you wish to change this, pass the desired timeout (in seconds) as request_timeout.

For fine-tuned control over the requests object use session with a requests.Session.

The mastodon_version parameter can be used to specify the version of Mastodon that Mastodon.py will expect to be installed on the server. The function will throw an error if an unparseable Version is specified. If no version is specified, Mastodon.py will set mastodon_version to the detected version.

The version check mode can be set to « created » (the default behaviour), « changed » or « none ». If set to « created », Mastodon.py will throw an error if the version of Mastodon it is connected to is too old to have an endpoint. If it is set to « changed », it will throw an error if the endpoint’s behaviour has changed after the version of Mastodon that is connected has been released. If it is set to « none », version checking is disabled.

feature_set can be used to enable behaviour specific to non-mainline Mastodon API implementations. Details are documented in the functions that provide such functionality. Currently supported feature sets are mainline, fedibird and pleroma.

For some Mastodon instances a User-Agent header is needed. This can be set by parameter user_agent. Starting from Mastodon.py 1.5.2 create_app() stores the application name into the client secret file. If client_id points to this file, the app name will be used as User-Agent header as default. It is possible to modify old secret files and append a client app name to use it as a User-Agent name.

lang can be used to change the locale Mastodon will use to generate responses. Valid parameters are all ISO 639-1 (two letter) or for a language that has none, 639-3 (three letter) language codes. This affects some error messages (those related to validation) and trends. You can change the language using set_language().

If no other User-Agent is specified, « mastodonpy » will be used.

csv_accounts_columns_names = ['Account address', 'Show boosts', 'Notify on new posts', 'Languages']#
export_accounts(mastodon_accounts, dest_csv_path=PosixPath('/home/runner/.geotribu/mastodon/export/mastodon_comptes_suivis_geotribu.csv'), default_instance='mapstodon.space')#

Export Mastodon following accounts into CSV file as web UI.

Paramètres:
  • mastodon_accounts (list[dict]) – list of accounts

  • dest_csv_path (Path) – path to the CSV file to write to. Defaults to default_dest_path_following_accounts.

  • default_instance (str) – default instance domain when account is on the same. Defaults to mapstodon.space.

Type renvoyé:

Path

Renvoie:

path to the CSV file

export_data(dest_path_following_accounts=PosixPath('/home/runner/.geotribu/mastodon/export/mastodon_comptes_suivis_geotribu.csv'), dest_path_lists=PosixPath('/home/runner/.geotribu/mastodon/export/mastodon_listes_geotribu.csv'), dest_path_lists_only_accounts=PosixPath('/home/runner/.geotribu/mastodon/export/mastodon_comptes_des_listes_geotribu'))#

Export account data.

Paramètres:
  • dest_path_following_accounts (Optional[Path]) – path to the CSV file for following accounts export. Defaults to default_dest_path_following_accounts.

  • dest_path_lists (Optional[Path]) – path to the CSV file for lists export. Defaults to default_dest_path_lists.

  • dest_path_lists_only_accounts (Optional[Path]) – path to the CSV file for only accounts from lists export. Defaults to default_dest_path_lists_only_accounts.

Lève:

MastodonAPIError – when it’s impossible to perform API request for profile information.

Type renvoyé:

tuple[Optional[Path], Optional[Path], Optional[Path]]

Renvoie:

tuple of paths of exported files

export_lists(mastodon_lists, dest_csv_path=PosixPath('/home/runner/.geotribu/mastodon/export/mastodon_listes_geotribu.csv'), default_instance='mapstodon.space')#

Export lists.

Paramètres:
  • mastodon_lists (dict[str, list[dict]]) – _description_

  • dest_csv_path (Path) – path to the CSV file to write to. Defaults to default_dest_path_following_accounts.

  • default_instance (str) – default instance domain when account is on the same. Defaults to mapstodon.space.

Type renvoyé:

Path

Renvoie:

path to the CSV file

classmethod full_account_with_instance(account, default_instance='mapstodon.space')#

Make sure the account contains instance domain.

Paramètres:
  • default_instance (str) – default instance domain. Defaults to mapstodon.space.

  • account (dict) – account dictionary

Renvoie:

account with default instance if not present

Example:


>>> print(ExtendedMastodonClient.full_account_with_instance(account={"acct": "datagouvfr@social.numerique.gouv.fr"}))
datagouvfr@social.numerique.gouv.fr
>>> print(ExtendedMastodonClient.full_account_with_instance(account={"acct": "leaflet"}))
leaflet@mapstodon.space
>>> print(ExtendedMastodonClient.full_account_with_instance(account={"acct": "opengisch"}, default_instance="fosstodon.org))
opengisch@fosstodon.org
classmethod url_to_instance_domain(url)#

Extract instance domain from URL.

Paramètres:

url (str) – input URL

Type renvoyé:

str

Renvoie:

instance domain

Example:


>>> print(ExtendedMastodonClient.url_to_instance_domain(url="https://mapstodon.space/@geotribu"))
mapstodon.space
geotribu_cli.social.mastodon_client.broadcast_to_mastodon(in_comment, public=True)#

Post the latest comment to Mastodon.

Paramètres:
  • in_comment (Comment) – comment to broadcast

  • public (bool) – if not, the comment is sent as direct message, so it’s not public.

Type renvoyé:

dict

Renvoie:

URL to posted status

geotribu_cli.social.mastodon_client.comment_already_broadcasted(comment_id, media='mastodon')#

Check if comment has already been broadcasted on the media.

Paramètres:
  • comment_id (int) – id of the comment to check

  • media (str) – name of the targetted media

Type renvoyé:

dict

Renvoie:

post on media if it has been already published

geotribu_cli.social.mastodon_client.comment_to_media(in_comment, media)#

Format comment to fit media size and publication rules.

Paramètres:
  • in_comment (Comment) – comment to format

  • media (str) – name of the targetted media

Type renvoyé:

str

Renvoie:

formatted comment