From c06baa13b30fc5ce058940e681305f6c03dc0048 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 17:11:45 +0100 Subject: [PATCH 01/11] Update verify_credentials docs --- docs/index.rst | 11 ++++++++++- mastodon/Mastodon.py | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index dfd91ab..1d8f138 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -134,7 +134,7 @@ 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 +API to do something obviously invalid (i.e. specify a privacy option that does not exist). `MastodonFileNotFoundError` and `MastodonNetworkError` are IO errors - could be you @@ -176,8 +176,17 @@ User dicts '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 + 'source': # Additional information - only present for user dict returned from account_verify_credentials() } + 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 ~~~~~~~~~~ .. code-block:: python diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 641b742..1c7b2ee 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -510,7 +510,7 @@ class Mastodon: """ Fetch authenticated user's account information. - Returns a user dict. + Returns a user dict (Starting from 2.1.0, with an additional "source" field). """ return self.__api_request('GET', '/api/v1/accounts/verify_credentials') From 75aebac744717216118afcdb89c96cfe8856e9ef Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 17:17:42 +0100 Subject: [PATCH 02/11] Add custom_emojis --- docs/index.rst | 5 +++++ mastodon/Mastodon.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 1d8f138..3dff97a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -496,6 +496,11 @@ Reading data: Domain blocks .. automethod:: Mastodon.domain_blocks +Reading data: Emoji +------------------- + +.. automethod:: Mastodon.custom_emojis + Writing data: Statuses ---------------------- These functions allow you to post statuses to Mastodon and to diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 1c7b2ee..8130b2c 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -713,6 +713,19 @@ class Mastodon: params = self.__generate_params(locals()) return self.__api_request('GET', '/api/v1/domain_blocks', params) + ### + # Reading data: Emoji + ### + @api_version("2.1.0") + def custom_emojis(self): + """ + Fetch the list of custom emoji the instance has installed. + + Returns a list of emoji dicts. + + """ + return self.__api_request('GET', '/api/v1/custom_emojis') + ### # Writing data: Statuses ### From 7ed769b37a9fc27e36023370ad265380cbc50c52 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 17:26:44 +0100 Subject: [PATCH 03/11] Add list streaming --- docs/index.rst | 3 ++- mastodon/Mastodon.py | 22 +++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 3dff97a..fe92636 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -585,7 +585,7 @@ will return a handle corresponding to the open connection. The connection may be closed at any time by calling the handles close() method, and the status of the connection can be verified calling is_alive() on the handle. -The streaming functions take instances of `StreamListener` as a parameter. +The streaming functions take instances of `StreamListener` as the `listener` parameter. A `CallbackStreamListener` class that allows you to specify function callbacks directly is included for convenience. @@ -593,6 +593,7 @@ directly is included for convenience. .. automethod:: Mastodon.stream_public .. automethod:: Mastodon.stream_local .. automethod:: Mastodon.stream_hashtag +.. automethod:: Mastodon.stream_list StreamListener ~~~~~~~~~~~~~~ diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 8130b2c..d8410b3 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1165,25 +1165,21 @@ class Mastodon: def stream_user(self, listener, async=False): """ Streams events that are relevant to the authorized user, i.e. home - timeline and notifications. 'listener' should be a subclass of - StreamListener which will receive callbacks for incoming events. + timeline and notifications. """ return self.__stream('/api/v1/streaming/user', listener, async=async) @api_version("1.1.0") def stream_public(self, listener, async=False): """ - Streams public events. 'listener' should be a subclass of StreamListener - which will receive callbacks for incoming events. + Streams public events. """ return self.__stream('/api/v1/streaming/public', listener, async=async) @api_version("1.1.0") def stream_local(self, listener, async=False): """ - Streams local events. 'listener' should be a subclass of StreamListener - which will receive callbacks for incoming events. - + Streams local public events. """ return self.__stream('/api/v1/streaming/public/local', listener, async=async) @@ -1191,13 +1187,21 @@ class Mastodon: def stream_hashtag(self, tag, listener, async=False): """ Stream for all public statuses for the hashtag 'tag' seen by the connected - instance. 'listener' should be a subclass of StreamListener which will receive - callbacks for incoming events. + instance. """ if tag.startswith("#"): raise MastodonIllegalArgumentError("Tag parameter should omit leading #") return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener) + @api_version("2.1.0") + def stream_list(self, id, listener, async=False): + """ + Stream events for the current user, restricted to accounts on the given + list. + """ + id = self.__unpack_id(id) + return self.__stream("/api/v1/streaming/list?list={}".format(id), listener) + ### # Internal helpers, dragons probably ### From 48d54effc8444b7f119ded786a6e750accf22027 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 17:47:50 +0100 Subject: [PATCH 04/11] Add lists endpoint --- mastodon/Mastodon.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index d8410b3..e8d68a8 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -606,6 +606,18 @@ class Mastodon: params = self.__generate_params(locals()) return self.__api_request('GET', '/api/v1/search', params) + ### + # Reading data: Lists + ### + @api_version("2.1.0") + def lists(self): + """ + Fetch a list of all the Lists by the logged-in user. + + Returns a list of list dicts. + """ + return self.__api_request('GET', '/api/v1/lists') + ### # Reading data: Mutes and Blocks ### From d7d8f153a31b76493daf68a44e01d4c4e11b180d Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 17:55:14 +0100 Subject: [PATCH 05/11] Update docs for search function --- docs/index.rst | 12 ++++++++++++ mastodon/Mastodon.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index fe92636..da670b1 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -340,6 +340,18 @@ Card dicts 'provider_url': # URL pointing to the embeds provider } +Search result dicts +~~~~~~~~~~~~~~~~~~~ +.. code-block:: python + + mastodon.search(...) + # Returns the folowing dictionary + { + '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 + } + Instance dicts ~~~~~~~~~~~~~~ .. code-block:: python diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index e8d68a8..2e3935c 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -601,7 +601,7 @@ class Mastodon: Fetch matching hashtags, accounts and statuses. Will search federated instances if resolve is True. - Returns a dict of lists. + Returns a search result dict. """ params = self.__generate_params(locals()) return self.__api_request('GET', '/api/v1/search', params) From 7d2f2441269fda71b72322825b5813a2aec4c7bd Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 18:40:17 +0100 Subject: [PATCH 06/11] Add internal links in docs --- docs/index.rst | 34 ++++++- mastodon/Mastodon.py | 209 +++++++++++++++++++++---------------------- 2 files changed, 137 insertions(+), 106 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index da670b1..0c05321 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -156,6 +156,8 @@ and are parsed into python datetime objects. User dicts ~~~~~~~~~~ +.. _user dict: + .. code-block:: python mastodon.account() @@ -189,6 +191,8 @@ User dicts Toot dicts ~~~~~~~~~~ +.. _toot dict: + .. code-block:: python mastodon.toot("Hello from Python") @@ -222,6 +226,8 @@ Toot dicts Mention dicts ~~~~~~~~~~~~~ +.. _mention dict: + .. code-block:: python { @@ -233,6 +239,8 @@ Mention dicts Hashtag dicts ~~~~~~~~~~~~~ +.. _hashtag dict: + .. code-block:: python { @@ -242,6 +250,8 @@ Hashtag dicts Emoji dicts ~~~~~~~~~~~ +.. _emoji dict: + .. code-block:: python { @@ -252,6 +262,8 @@ Emoji dicts Relationship dicts ~~~~~~~~~~~~~~~~~~ +.. _relationship dict: + .. code-block:: python mastodon.account_follow() @@ -268,6 +280,8 @@ Relationship dicts Notification dicts ~~~~~~~~~~~~~~~~~~ +.. _notification dict: + .. code-block:: python mastodon.notifications()[0] @@ -283,6 +297,8 @@ Notification dicts Context dicts ~~~~~~~~~~~~~ +.. _context dict: + .. code-block:: python mastodon.status_context() @@ -294,6 +310,8 @@ Context dicts Media dicts ~~~~~~~~~~~ +.. _media dict: + .. code-block:: python mastodon.media_post("image.jpg", "image/jpeg") @@ -318,6 +336,8 @@ Media dicts Card dicts ~~~~~~~~~~ +.. _card dict: + .. code-block:: python mastodon.status_card(): @@ -342,9 +362,11 @@ Card dicts Search result dicts ~~~~~~~~~~~~~~~~~~~ +.. _search result dict: + .. code-block:: python - mastodon.search(...) + mastodon.search("") # Returns the folowing dictionary { 'accounts': # List of account dicts resulting from the query @@ -354,6 +376,8 @@ Search result dicts Instance dicts ~~~~~~~~~~~~~~ +.. _instance dict: + .. code-block:: python mastodon.instance() @@ -369,6 +393,8 @@ Instance dicts Report dicts ~~~~~~~~~~~~ +.. _report dict: + .. code-block:: python mastodon.reports()[0] @@ -404,6 +430,7 @@ is specified, Mastodon.py defaults to https. .. automethod:: Mastodon.create_app .. automethod:: Mastodon.__init__ +.. _log_in(): .. automethod:: Mastodon.log_in .. automethod:: Mastodon.auth_request_url @@ -434,10 +461,12 @@ Reading data: Timelines This function allows you to access the timelines a logged in user could see, as well as hashtag timelines and the public timeline. +.. _timeline(): .. automethod:: Mastodon.timeline .. automethod:: Mastodon.timeline_home .. automethod:: Mastodon.timeline_local .. automethod:: Mastodon.timeline_public +.. _timeline_hashtag(): .. automethod:: Mastodon.timeline_hashtag Reading data: Statuses @@ -518,6 +547,7 @@ Writing data: Statuses These functions allow you to post statuses to Mastodon and to interact with already posted statuses. +.. _status_post(): .. automethod:: Mastodon.status_post .. automethod:: Mastodon.toot .. automethod:: Mastodon.status_reblog @@ -562,6 +592,8 @@ 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. +.. _media_post(): + .. automethod:: Mastodon.media_post Writing data: Reports diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 2e3935c..af450ec 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -72,16 +72,16 @@ class Mastodon: def create_app(client_name, scopes=['read', 'write', 'follow'], redirect_uris=None, website=None, to_file=None, api_base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT): """ - Create a new app with given client_name and scopes (read, write, follow) + Create a new app with given `client_name` and `scopes` (read, write, follow) - Specify redirect_uris if you want users to be redirected to a certain page after authenticating. - Specify to_file to persist your apps info to a file so you can use them in the constructor. - Specify api_base_url if you want to register an app on an instance different from the flagship one. + Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating. + Specify `to_file` to persist your apps info to a file so you can use them in the constructor. + Specify `api_base_url` if you want to register an app on an instance different from the flagship one. Presently, app registration is open by default, but this is not guaranteed to be the case for all future mastodon instances or even the flagship instance in the future. - Returns client_id and client_secret. + Returns `client_id` and `client_secret`, both as strings. """ api_base_url = Mastodon.__protocolize(api_base_url) @@ -118,13 +118,13 @@ class Mastodon: ratelimit_method="wait", ratelimit_pacefactor=1.1, request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None): """ - Create a new API wrapper instance based on the given client_secret and client_id. If you - give a client_id and it is not a file, you must also give a secret. + Create a new API wrapper instance based on the given `client_secret` and `client_id`. If you + give a `client_id` and it is not a file, you must also give a secret. - You can also specify an access_token, directly or as a file (as written by log_in). + You can also specify an `access_token`, directly or as a file (as written by `log_in()`_). - Mastodon.py can try to respect rate limits in several ways, controlled by ratelimit_method. - "throw" makes functions throw a MastodonRatelimitError when the rate + 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 not hit the rate @@ -132,15 +132,15 @@ class Mastodon: 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. - Specify api_base_url if you wish to talk to an instance other than the flagship one. - If a file is given as client_id, read client ID and secret from that file. + Specify `api_base_url` if you wish to talk to an instance other than the flagship one. + If a file is given as `client_id`, client ID and secret are read from that file. 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. + pass the desired timeout (in seconds) as `request_timeout`. - The mastodon_version parameter can be used to specify the version of Mastodon that Mastodon.py will + 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 + Version is specified. If no version is specified, Mastodon.py will set `mastodon_version` to the detected version. """ self.api_base_url = Mastodon.__protocolize(api_base_url) @@ -240,21 +240,21 @@ class Mastodon: code=None, redirect_uri="urn:ietf:wg:oauth:2.0:oob", refresh_token=None, scopes=['read', 'write', 'follow'], to_file=None): """ - Your username is the e-mail you use to log in into mastodon. + Get the access token for a user. + + The username is the e-mail used to log in into mastodon. - Can persist access token to file, to be used in the constructor. + Can persist access token to file `to_file`, to be used in the constructor. - Supports refresh_token but Mastodon.social doesn't implement it at the moment. - - Handles password, authorization_code, and refresh_token authentication. - - Will throw a MastodonIllegalArgumentError if username / password + Handles password and OAuth-based authorization. + + Will throw a `MastodonIllegalArgumentError` if username / password are wrong, scopes are not valid or granted scopes differ from requested. For OAuth2 documentation, compare https://github.com/doorkeeper-gem/doorkeeper/wiki/Interacting-as-an-OAuth-client-with-Doorkeeper - Returns the access token. + Returns the access token as a string. """ if username is not None and password is not None: params = self.__generate_params(locals(), ['scopes', 'to_file', 'code', 'refresh_token']) @@ -306,7 +306,7 @@ class Mastodon: """ Retrieve basic information about the instance, including the URI and administrative contact email. - Returns an instance dict. + Returns an `instance dict`_. """ return self.__instance() @@ -322,13 +322,13 @@ class Mastodon: @api_version("1.0.0") def timeline(self, timeline="home", max_id=None, since_id=None, limit=None): """ - Fetch statuses, most recent ones first. Timeline can be 'home', 'local', 'public', + Fetch statuses, most recent ones first. `timeline` can be 'home', 'local', 'public', or 'tag/hashtag'. See the following functions documentation for what those do. - Local hashtag timelines are supported via the timeline_hashtag() function. + Local hashtag timelines are supported via the `timeline_hashtag()`_ function. The default timeline is the "home" timeline. - Returns a list of toot dicts. + Returns a list of `toot dicts`_. """ if max_id != None: max_id = self.__unpack_id(max_id) @@ -349,9 +349,9 @@ class Mastodon: @api_version("1.0.0") def timeline_home(self, max_id=None, since_id=None, limit=None): """ - Fetch the authenticated users home timeline (i.e. followed users and self). + Fetch the logged-in users home timeline (i.e. followed users and self). - Returns a list of toot dicts. + Returns a list of `toot dicts`_. """ return self.timeline('home', max_id=max_id, since_id=since_id, limit=limit) @@ -361,7 +361,7 @@ class Mastodon: """ Fetches the local / instance-wide timeline, not including replies. - Returns a list of toot dicts. + Returns a list of `toot dicts`_. """ return self.timeline('local', max_id=max_id, since_id=since_id, limit=limit) @@ -371,7 +371,7 @@ class Mastodon: """ Fetches the public / visible-network timeline, not including replies. - Returns a list of toot dicts. + Returns a list of `toot dicts`_. """ return self.timeline('public', max_id=max_id, since_id=since_id, limit=limit) @@ -382,9 +382,9 @@ class Mastodon: Fetch a timeline of toots with a given hashtag. The hashtag parameter should not contain the leading #. - Set "local" to True to retrieve only instance-local tagged posts. + Set `local` to True to retrieve only instance-local tagged posts. - Returns a list of toot dicts. + Returns a list of `toot dicts`_. """ if hashtag.startswith("#"): raise MastodonIllegalArgumentError("Hashtag parameter should omit leading #") @@ -413,7 +413,7 @@ class Mastodon: """ Fetch information about a single toot. - Returns a toot dict. + Returns a `toot dict`_. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}'.format(str(id)) @@ -425,7 +425,7 @@ class Mastodon: Fetch a card associated with a status. A card describes an object (such as an external video or link) embedded into a status. - Returns a card dict. + Returns a `card dict`_. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/card'.format(str(id)) @@ -436,7 +436,7 @@ class Mastodon: """ Fetch information about ancestors and descendants of a toot. - Returns a context dict. + Returns a `context dict`_. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/context'.format(str(id)) @@ -447,7 +447,7 @@ class Mastodon: """ Fetch a list of users that have reblogged a status. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/reblogged_by'.format(str(id)) @@ -458,7 +458,7 @@ class Mastodon: """ Fetch a list of users that have favourited a status. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/favourited_by'.format(str(id)) @@ -470,12 +470,12 @@ class Mastodon: @api_version("1.0.0") def notifications(self, id=None, max_id=None, since_id=None, limit=None): """ - Fetch notifications (mentions, favourites, reblogs, follows) for the authenticated + Fetch notifications (mentions, favourites, reblogs, follows) for the logged-in user. - Can be passed an id to fetch a single notification. + Can be passed an `id` to fetch a single notification. - Returns a list of notification dicts. + Returns a list of `notification dicts`_. """ if max_id != None: max_id = self.__unpack_id(max_id) @@ -497,9 +497,9 @@ class Mastodon: @api_version("1.0.0") def account(self, id): """ - Fetch account information by user id. + Fetch account information by user `id`. - Returns a user dict. + Returns a `user dict`_. """ id = self.__unpack_id(id) url = '/api/v1/accounts/{0}'.format(str(id)) @@ -508,18 +508,21 @@ class Mastodon: @api_version("1.0.0") def account_verify_credentials(self): """ - Fetch authenticated user's account information. + Fetch logged-in user's account information. - Returns a user dict (Starting from 2.1.0, with an additional "source" field). + Returns a `user dict`_ (Starting from 2.1.0, with an additional "source" field). """ return self.__api_request('GET', '/api/v1/accounts/verify_credentials') @api_version("1.0.0") def account_statuses(self, id, max_id=None, since_id=None, limit=None): """ - Fetch statuses by user id. Same options as timeline are permitted. + Fetch statuses by user `id`. Same options as `timeline()`_ are permitted. + Returned toots are from the perspective of the logged-in user, i.e. + all statuses visible to the logged-in user (including DMs) are + included. - Returns a list of toot dicts. + Returns a list of `toot dicts`_. """ id = self.__unpack_id(id) if max_id != None: @@ -537,7 +540,7 @@ class Mastodon: """ Fetch users the given user is following. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ id = self.__unpack_id(id) if max_id != None: @@ -555,7 +558,7 @@ class Mastodon: """ Fetch users the given user is followed by. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ id = self.__unpack_id(id) if max_id != None: @@ -572,9 +575,9 @@ class Mastodon: def account_relationships(self, id): """ Fetch relationship (following, followed_by, blocking, follow requested) of - the logged in user to a given account. id can be a list. + the logged in user to a given account. `id` can be a list. - Returns a list of relationship dicts. + Returns a list of `relationship dicts`_. """ id = self.__unpack_id(id) params = self.__generate_params(locals()) @@ -587,7 +590,7 @@ class Mastodon: Fetch matching accounts. Will lookup an account remotely if the search term is in the username@domain format and not yet in the database. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ params = self.__generate_params(locals()) return self.__api_request('GET', '/api/v1/accounts/search', params) @@ -601,7 +604,7 @@ class Mastodon: Fetch matching hashtags, accounts and statuses. Will search federated instances if resolve is True. - Returns a search result dict. + Returns a `search result dict`_. """ params = self.__generate_params(locals()) return self.__api_request('GET', '/api/v1/search', params) @@ -614,7 +617,7 @@ class Mastodon: """ Fetch a list of all the Lists by the logged-in user. - Returns a list of list dicts. + Returns a list of `list dicts`_. """ return self.__api_request('GET', '/api/v1/lists') @@ -624,9 +627,9 @@ class Mastodon: @api_version("1.1.0") def mutes(self, max_id=None, since_id=None, limit=None): """ - Fetch a list of users muted by the authenticated user. + Fetch a list of users muted by the logged-in user. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ if max_id != None: max_id = self.__unpack_id(max_id) @@ -640,9 +643,9 @@ class Mastodon: @api_version("1.0.0") def blocks(self, max_id=None, since_id=None, limit=None): """ - Fetch a list of users blocked by the authenticated user. + Fetch a list of users blocked by the logged-in user. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ if max_id != None: max_id = self.__unpack_id(max_id) @@ -659,12 +662,12 @@ class Mastodon: @api_version("1.1.0") def reports(self): """ - Fetch a list of reports made by the authenticated user. + Fetch a list of reports made by the logged-in user. - Returns a list of report dicts. + Returns a list of `report dicts`_. Warning: According to the official API documentation, this - method is to be treated as not finalized as of Mastodon 2.0.0. + method is to be treated as not finalized as of Mastodon 2.1.0. """ return self.__api_request('GET', '/api/v1/reports') @@ -674,9 +677,9 @@ class Mastodon: @api_version("1.0.0") def favourites(self, max_id=None, since_id=None, limit=None): """ - Fetch the authenticated user's favourited statuses. + Fetch the logged-in user's favourited statuses. - Returns a list of toot dicts. + Returns a list of `toot dicts`_. """ if max_id != None: max_id = self.__unpack_id(max_id) @@ -693,9 +696,9 @@ class Mastodon: @api_version("1.0.0") def follow_requests(self, max_id=None, since_id=None, limit=None): """ - Fetch the authenticated user's incoming follow requests. + Fetch the logged-in user's incoming follow requests. - Returns a list of user dicts. + Returns a list of `user dicts`_. """ if max_id != None: max_id = self.__unpack_id(max_id) @@ -712,7 +715,7 @@ class Mastodon: @api_version("1.4.0") def domain_blocks(self, max_id=None, since_id=None, limit=None): """ - Fetch the authenticated user's blocked domains. + Fetch the logged-in user's blocked domains. Returns a list of blocked domain URLs (as strings, without protocol specifier). """ @@ -733,7 +736,7 @@ class Mastodon: """ Fetch the list of custom emoji the instance has installed. - Returns a list of emoji dicts. + Returns a list of `emoji dicts`_. """ return self.__api_request('GET', '/api/v1/custom_emojis') @@ -746,11 +749,11 @@ class Mastodon: sensitive=False, visibility='', spoiler_text=None): """ Post a status. Can optionally be in reply to another status and contain - up to four pieces of media (Uploaded via media_post()). media_ids can - also be the media dicts returned by media_post - they are unpacked + up to four pieces of media (Uploaded via `media_post()`_). media_ids can + also be the `media dicts`_ returned by `media_post()`_ - they are unpacked automatically. - The 'sensitive' boolean decides whether or not media attached to the post + The `sensitive` boolean decides whether or not media attached to the post should be marked as sensitive, which hides it by default on the Mastodon web front-end. @@ -766,11 +769,11 @@ class Mastodon: locked setting - private if the account is locked, public otherwise (for Mastodon versions lower than 1.6). - The spoiler_text parameter is a string to be shown as a warning before + The `spoiler_text` parameter is a string to be shown as a warning before the text of the status. If no text is passed in, no warning will be displayed. - Returns a toot dict with the new status. + Returns a `toot dict`_ with the new status. """ if in_reply_to_id != None: in_reply_to_id = self.__unpack_id(in_reply_to_id) @@ -806,11 +809,11 @@ class Mastodon: @api_version("1.0.0") def toot(self, status): """ - Synonym for status_post that only takes the status text as input. + Synonym for `status_post()`_ that only takes the status text as input. Usage in production code is not recommended. - Returns a toot dict with the new status. + Returns a `toot dict`_ with the new status. """ return self.status_post(status) @@ -818,19 +821,17 @@ class Mastodon: def status_delete(self, id): """ Delete a status - - Returns an empty dict for good measure. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}'.format(str(id)) - return self.__api_request('DELETE', url) + self.__api_request('DELETE', url) @api_version("1.0.0") def status_reblog(self, id): """ Reblog a status. - Returns a toot dict with a new status that wraps around the reblogged one. + Returns a `toot dict`_ with a new status that wraps around the reblogged one. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/reblog'.format(str(id)) @@ -841,7 +842,7 @@ class Mastodon: """ Un-reblog a status. - Returns a toot dict with the status that used to be reblogged. + Returns a `toot dict`_ with the status that used to be reblogged. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/unreblog'.format(str(id)) @@ -852,7 +853,7 @@ class Mastodon: """ Favourite a status. - Returns a toot dict with the favourited status. + Returns a `toot dict`_ with the favourited status. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/favourite'.format(str(id)) @@ -863,7 +864,7 @@ class Mastodon: """ Un-favourite a status. - Returns a toot dict with the un-favourited status. + Returns a `toot dict`_ with the un-favourited status. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/unfavourite'.format(str(id)) @@ -874,7 +875,7 @@ class Mastodon: """ Mute notifications for a status. - Returns a toot dict with the now muted status + Returns a `toot dict`_ with the now muted status """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/mute'.format(str(id)) @@ -885,7 +886,7 @@ class Mastodon: """ Unmute notifications for a status. - Returns a toot dict with the status that used to be muted. + Returns a `toot dict`_ with the status that used to be muted. """ id = self.__unpack_id(id) url = '/api/v1/statuses/{0}/unmute'.format(str(id)) @@ -899,7 +900,7 @@ class Mastodon: """ Clear out a users notifications """ - return self.__api_request('POST', '/api/v1/notifications/clear') + self.__api_request('POST', '/api/v1/notifications/clear') @api_version("1.3.0") @@ -909,7 +910,7 @@ class Mastodon: """ id = self.__unpack_id(id) params = self.__generate_params(locals()) - return self.__api_request('POST', '/api/v1/notifications/dismiss', params) + self.__api_request('POST', '/api/v1/notifications/dismiss', params) ### # Writing data: Accounts @@ -919,7 +920,7 @@ class Mastodon: """ Follow a user. - Returns a relationship dict containing the updated relationship to the user. + Returns a `relationship dict`_ containing the updated relationship to the user. """ id = self.__unpack_id(id) url = '/api/v1/accounts/{0}/follow'.format(str(id)) @@ -930,7 +931,7 @@ class Mastodon: """ Follow a remote user by uri (username@domain). - Returns a user dict. + Returns a `user dict`_. """ params = self.__generate_params(locals()) return self.__api_request('POST', '/api/v1/follows', params) @@ -940,7 +941,7 @@ class Mastodon: """ Unfollow a user. - Returns a relationship dict containing the updated relationship to the user. + Returns a `relationship dict`_ containing the updated relationship to the user. """ id = self.__unpack_id(id) url = '/api/v1/accounts/{0}/unfollow'.format(str(id)) @@ -951,7 +952,7 @@ class Mastodon: """ Block a user. - Returns a relationship dict containing the updated relationship to the user. + Returns a `relationship dict`_ containing the updated relationship to the user. """ id = self.__unpack_id(id) url = '/api/v1/accounts/{0}/block'.format(str(id)) @@ -962,7 +963,7 @@ class Mastodon: """ Unblock a user. - Returns a relationship dict containing the updated relationship to the user. + Returns a `relationship dict`_ containing the updated relationship to the user. """ id = self.__unpack_id(id) url = '/api/v1/accounts/{0}/unblock'.format(str(id)) @@ -973,7 +974,7 @@ class Mastodon: """ Mute a user. - Returns a relationship dict containing the updated relationship to the user. + Returns a `relationship dict`_ containing the updated relationship to the user. """ id = self.__unpack_id(id) url = '/api/v1/accounts/{0}/mute'.format(str(id)) @@ -984,7 +985,7 @@ class Mastodon: """ Unmute a user. - Returns a relationship dict containing the updated relationship to the user. + Returns a `relationship dict`_ containing the updated relationship to the user. """ id = self.__unpack_id(id) url = '/api/v1/accounts/{0}/unmute'.format(str(id)) @@ -994,12 +995,14 @@ class Mastodon: def account_update_credentials(self, display_name=None, note=None, avatar=None, header=None): """ - Update the profile for the currently authenticated user. + Update the profile for the currently logged-in user. 'note' is the user's bio. 'avatar' and 'header' are images encoded in base64, prepended by a content-type (for example: 'data:image/png;base64,iVBORw0KGgoAAAA[...]') + + Returns the updated `user dict` of the logged-in user. """ params = self.__generate_params(locals()) return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params) @@ -1014,7 +1017,7 @@ class Mastodon: Accepts a list of toot IDs associated with the report, and a comment. - Returns a report dict. + Returns a `report dict`_. """ account_id = self.__unpack_id(account_id) status_ids = map(lambda x: self.__unpack_id(x), status_ids) @@ -1028,23 +1031,19 @@ class Mastodon: def follow_request_authorize(self, id): """ Accept an incoming follow request. - - Returns an empty dict. """ id = self.__unpack_id(id) url = '/api/v1/follow_requests/{0}/authorize'.format(str(id)) - return self.__api_request('POST', url) + self.__api_request('POST', url) @api_version("1.0.0") def follow_request_reject(self, id): """ Reject an incoming follow request. - - Returns an empty dict. """ id = self.__unpack_id(id) url = '/api/v1/follow_requests/{0}/reject'.format(str(id)) - return self.__api_request('POST', url) + self.__api_request('POST', url) ### # Writing data: Media @@ -1052,15 +1051,15 @@ class Mastodon: @api_version("1.0.0") def media_post(self, media_file, mime_type=None, description=None): """ - Post an image. media_file can either be image data or + Post an image. `media_file` can either be image data or a file name. If image data is passed directly, the mime type has to be specified manually, otherwise, it is determined from the file name. - Throws a MastodonIllegalArgumentError if the mime type of the + Throws a `MastodonIllegalArgumentError` if the mime type of the passed data or file can not be determined properly. - Returns a media dict. This contains the id that can be used in + Returns a `media dict`_. This contains the id that can be used in status_post to attach the media file to a toot. """ if mime_type is None and os.path.isfile(media_file): @@ -1090,7 +1089,7 @@ class Mastodon: Add a block for all statuses originating from the specified domain for the logged-in user. """ params = self.__generate_params(locals()) - return self.__api_request('POST', '/api/v1/domain_blocks', params) + self.__api_request('POST', '/api/v1/domain_blocks', params) @api_version("1.4.0") def domain_unblock(self, domain=None): @@ -1098,7 +1097,7 @@ class Mastodon: Remove a domain block for the logged-in user. """ params = self.__generate_params(locals()) - return self.__api_request('DELETE', '/api/v1/domain_blocks', params) + self.__api_request('DELETE', '/api/v1/domain_blocks', params) ### # Pagination From c9cc8737142cf71ce5b6e2435b3bf4c00272cd80 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 18:45:28 +0100 Subject: [PATCH 07/11] Add information about unauthenticated methods --- mastodon/Mastodon.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index af450ec..35640fd 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -306,6 +306,8 @@ class Mastodon: """ Retrieve basic information about the instance, including the URI and administrative contact email. + Does not require authentication. + Returns an `instance dict`_. """ return self.__instance() @@ -413,6 +415,8 @@ class Mastodon: """ Fetch information about a single toot. + Does not require authentication for publicly visible statuses. + Returns a `toot dict`_. """ id = self.__unpack_id(id) @@ -425,6 +429,8 @@ class Mastodon: Fetch a card associated with a status. A card describes an object (such as an external video or link) embedded into a status. + Does not require authentication for publicly visible statuses. + Returns a `card dict`_. """ id = self.__unpack_id(id) @@ -436,6 +442,8 @@ class Mastodon: """ Fetch information about ancestors and descendants of a toot. + Does not require authentication for publicly visible statuses. + Returns a `context dict`_. """ id = self.__unpack_id(id) @@ -447,6 +455,8 @@ class Mastodon: """ Fetch a list of users that have reblogged a status. + Does not require authentication for publicly visible statuses. + Returns a list of `user dicts`_. """ id = self.__unpack_id(id) @@ -458,6 +468,8 @@ class Mastodon: """ Fetch a list of users that have favourited a status. + Does not require authentication for publicly visible statuses. + Returns a list of `user dicts`_. """ id = self.__unpack_id(id) @@ -736,6 +748,8 @@ class Mastodon: """ Fetch the list of custom emoji the instance has installed. + Does not require authentication. + Returns a list of `emoji dicts`_. """ From 7cf813b9744342d0be82330cf5fb81e85fea5642 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 18:59:32 +0100 Subject: [PATCH 08/11] Added account_lists(broken) --- mastodon/Mastodon.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 35640fd..0df1217 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -607,6 +607,20 @@ class Mastodon: params = self.__generate_params(locals()) return self.__api_request('GET', '/api/v1/accounts/search', params) + @api_version("2.1.0") + def account_lists(self, id): + """ + Get all of the logged in users lists which the specified user is + a member of. + + TODO: This doesn't work. + + Returns a list of `list dicts`_. + """ + params = self.__generate_params(locals(), ['id']) + url = '/api/v1/accounts/{0}/lists'.format(str(id)) + return self.__api_request('GET', url, params) + ### # Reading data: Searching ### From b840766ed77ea0cac84ada7cb27a9b23ffda0a09 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 21:16:02 +0100 Subject: [PATCH 09/11] Add more list endpoints --- docs/index.rst | 26 +++++++++++++++----- mastodon/Mastodon.py | 57 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 0c05321..9496da6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -287,12 +287,12 @@ Notification dicts mastodon.notifications()[0] # Returns the following dictionary: { - '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. + '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 } Context dicts @@ -308,6 +308,19 @@ Context dicts 'descendants': # A list of toot dicts } +List dicts +~~~~~~~~~~ +.. _list dict: + +.. code-block:: python + + mastodon.list() + # Returns the following dictionary: + { + 'id': # id of the list + 'title': # title of the list + } + Media dicts ~~~~~~~~~~~ .. _media dict: @@ -468,6 +481,7 @@ user could see, as well as hashtag timelines and the public timeline. .. automethod:: Mastodon.timeline_public .. _timeline_hashtag(): .. automethod:: Mastodon.timeline_hashtag +.. automethod:: Mastodon.timeline_list Reading data: Statuses ---------------------- diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 0df1217..44e0538 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -325,7 +325,7 @@ class Mastodon: def timeline(self, timeline="home", max_id=None, since_id=None, limit=None): """ Fetch statuses, most recent ones first. `timeline` can be 'home', 'local', 'public', - or 'tag/hashtag'. See the following functions documentation for what those do. + 'tag/hashtag' or 'list/id'. See the following functions documentation for what those do. Local hashtag timelines are supported via the `timeline_hashtag()`_ function. The default timeline is the "home" timeline. @@ -407,6 +407,17 @@ class Mastodon: return self.__api_request('GET', url, params) + @api_version("2.1.0") + def timeline_list(self, id, max_id=None, since_id=None, limit=None): + """ + Fetches a timeline containing all the toots by users in a given list. + + Returns a list of `toot dicts`_. + """ + id = self.__unpack_id(id) + return self.timeline('list/{0}'.format(id), max_id=max_id, + since_id=since_id, limit=limit) + ### # Reading data: Statuses ### @@ -613,8 +624,6 @@ class Mastodon: Get all of the logged in users lists which the specified user is a member of. - TODO: This doesn't work. - Returns a list of `list dicts`_. """ params = self.__generate_params(locals(), ['id']) @@ -647,6 +656,16 @@ class Mastodon: """ return self.__api_request('GET', '/api/v1/lists') + @api_version("2.1.0") + def list(self, id): + """ + Fetch info about a specific list. + + Returns a `list dict`_. + """ + id = self.__unpack_id(id) + return self.__api_request('GET', '/api/v1/lists/{0}'.format(id)) + ### # Reading data: Mutes and Blocks ### @@ -1035,6 +1054,38 @@ class Mastodon: params = self.__generate_params(locals()) return self.__api_request('PATCH', '/api/v1/accounts/update_credentials', params) + ### + # Writing data: Lists + ### + @api_version("2.1.0") + def list_create(self, title): + """ + Create a new list with the given `title`. + + Returns the `list dict`_ of the created list. + """ + params = self.__generate_params(locals()) + return self.__api_request('POST', '/api/v1/lists', params) + + @api_version("2.1.0") + def list_update(self, id, title): + """ + Update info about a list, where "info" is really the lists `title`. + + Returns the `list dict`_ of the modified list. + """ + id = self.__unpack_id(id) + params = self.__generate_params(locals(), ['id']) + return self.__api_request('PUT', '/api/v1/lists/{0}'.format(id), params) + + @api_version("2.1.0") + def list_delete(self, id): + """ + Delete a list. + """ + id = self.__unpack_id(id) + self.__api_request('DELETE', '/api/v1/lists/{0}'.format(id)) + ### # Writing data: Reports ### From 0b5c0ae5ccb61a42ed19ee9c5b46de65979b9060 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Wed, 13 Dec 2017 22:16:10 +0100 Subject: [PATCH 10/11] Add remaining list endpoints --- mastodon/.Mastodon.py.kate-swp | Bin 0 -> 106 bytes mastodon/Mastodon.py | 61 ++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 mastodon/.Mastodon.py.kate-swp diff --git a/mastodon/.Mastodon.py.kate-swp b/mastodon/.Mastodon.py.kate-swp new file mode 100644 index 0000000000000000000000000000000000000000..60f8d57fa41a22907eada23cf732266027171e5e GIT binary patch literal 106 zcmZQzU=Z?7EJ;-eE>A2_aLdd|RWQ;sU|?Vn5&ZRS%Jbdp_rG4EF!Au8)xW(;Zz~4} aF)*;C17$gYn9mi3%L(C@qRPU!oUQ Date: Wed, 13 Dec 2017 22:33:29 +0100 Subject: [PATCH 11/11] Document list endpoints --- docs/index.rst | 22 +++++++++++++++++++++- mastodon/Mastodon.py | 12 ++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 9496da6..4dd6403 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -512,6 +512,14 @@ their relationships. .. automethod:: Mastodon.account_relationships .. automethod:: Mastodon.account_search +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 --------------------- @@ -532,7 +540,6 @@ Reading data: Searching .. automethod:: Mastodon.search - Reading data: Mutes and blocks ------------------------------ These functions allow you to get information about accounts that are @@ -593,6 +600,19 @@ These functions allow you to interact with other accounts: To (un)follow and .. automethod:: Mastodon.account_unmute .. automethod:: Mastodon.account_update_credentials +Writing data: Lists +------------------- +These functions allow you to create, maintain and delete lists. + +When creating lists, note that (As of Mastodon 2.1.0), a user can only +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 + Writing data: Follow requests ----------------------------- These functions allow you to accept or reject incoming follow requests. diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 3b02881..f15ee23 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1105,14 +1105,6 @@ class Mastodon: id = self.__unpack_id(id) self.__api_request('DELETE', '/api/v1/lists/{0}'.format(id)) - @api_version("2.1.0") - def list_delete(self, id): - """ - Delete a list. - """ - id = self.__unpack_id(id) - self.__api_request('DELETE', '/api/v1/lists/{0}'.format(id)) - @api_version("2.1.0") def list_accounts_add(self, id, account_ids): """ @@ -1125,7 +1117,7 @@ class Mastodon: account_ids = list(map(lambda x: self.__unpack_id(x), account_ids)) params = self.__generate_params(locals(), ['id']) - return self.__api_request('POST', '/api/v1/lists/{0}/accounts'.format(id), params) + self.__api_request('POST', '/api/v1/lists/{0}/accounts'.format(id), params) @api_version("2.1.0") def list_accounts_delete(self, id, account_ids): @@ -1139,7 +1131,7 @@ class Mastodon: account_ids = list(map(lambda x: self.__unpack_id(x), account_ids)) params = self.__generate_params(locals(), ['id']) - return self.__api_request('DELETE', '/api/v1/lists/{0}/accounts'.format(id), params) + self.__api_request('DELETE', '/api/v1/lists/{0}/accounts'.format(id), params) ### # Writing data: Reports