From 5342fd8f7ac012ce3a432586bd164ddc1936298f Mon Sep 17 00:00:00 2001 From: Alex Camilleri Date: Tue, 22 Oct 2024 23:58:14 +0200 Subject: [PATCH 1/5] Get multiple accounts --- docs/06_accounts.rst | 1 + docs/15_everything.rst | 15 ++++++++------- mastodon/accounts.py | 14 ++++++++++++-- tests/test_account.py | 9 +++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/docs/06_accounts.rst b/docs/06_accounts.rst index b188684..53ec717 100644 --- a/docs/06_accounts.rst +++ b/docs/06_accounts.rst @@ -15,6 +15,7 @@ Reading .. automethod:: Mastodon.account .. automethod:: Mastodon.account_search .. automethod:: Mastodon.account_lookup +.. automethod:: Mastodon.accounts .. automethod:: Mastodon.featured_tags .. automethod:: Mastodon.featured_tag_suggestions diff --git a/docs/15_everything.rst b/docs/15_everything.rst index 84ddbbe..b6711cf 100644 --- a/docs/15_everything.rst +++ b/docs/15_everything.rst @@ -1,8 +1,8 @@ -Every function on a huge CTRL-F-able page -========================================= -.. py:module:: mastodon -.. py:class: Mastodon - +Every function on a huge CTRL-F-able page +========================================= +.. py:module:: mastodon +.. py:class: Mastodon + .. automethod:: Mastodon.retrieve_mastodon_version .. automethod:: Mastodon.verify_minimum_version .. automethod:: Mastodon.create_app @@ -51,6 +51,7 @@ Every function on a huge CTRL-F-able page .. automethod:: Mastodon.account .. automethod:: Mastodon.account_search .. automethod:: Mastodon.account_lookup +.. automethod:: Mastodon.accounts .. automethod:: Mastodon.featured_tags .. automethod:: Mastodon.featured_tag_suggestions .. automethod:: Mastodon.account_featured_tags @@ -184,5 +185,5 @@ Every function on a huge CTRL-F-able page .. automethod:: Mastodon.admin_delete_domain_block .. automethod:: Mastodon.admin_measures .. automethod:: Mastodon.admin_dimensions -.. automethod:: Mastodon.admin_retention - +.. automethod:: Mastodon.admin_retention + diff --git a/mastodon/accounts.py b/mastodon/accounts.py index 30c9c63..439ee75 100644 --- a/mastodon/accounts.py +++ b/mastodon/accounts.py @@ -84,7 +84,7 @@ class Mastodon(Internals): self.access_token = response['access_token'] self.__set_refresh_token(response.get('refresh_token')) self.__set_token_expired(int(response.get('expires_in', 0))) - except Exception as e: + except Exception: raise MastodonIllegalArgumentError('Invalid request') # Step 3: Check scopes, persist, et cetera @@ -133,6 +133,16 @@ class Mastodon(Internals): id = self.__unpack_id(id) return self.__api_request('GET', f'/api/v1/accounts/{id}') + @api_version("4.3.0", "4.3.0", _DICT_VERSION_ACCOUNT) + def accounts(self, ids: List[Union[Account, IdType]]) -> List[Account]: + """ + Fetch information from multiple accounts by a list of user `id`. + + Does not require authentication for publicly visible accounts. + """ + ids = [self.__unpack_id(id) for id in ids] + return self.__api_request('GET', '/api/v1/accounts', {"id": ids}) + @api_version("1.0.0", "2.1.0", _DICT_VERSION_ACCOUNT) def account_verify_credentials(self) -> Account: """ @@ -255,7 +265,7 @@ class Mastodon(Internals): """ params = self.__generate_params(locals()) - if params["following"] == False: + if params["following"] is False: del params["following"] return self.__api_request('GET', '/api/v1/accounts/search', params) diff --git a/tests/test_account.py b/tests/test_account.py index aebfaa2..8f395cc 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -8,6 +8,15 @@ def test_account(api): account = api.account(api.account_verify_credentials()) assert account +@pytest.mark.vcr() +def test_accounts(api): + account_ids = [ + api.account_lookup("mastodonpy_test").id, + api.account_lookup("mastodonpy_test_2").id + ] + accounts = api.accounts(account_ids) + assert len(accounts) == 2 + @pytest.mark.vcr() def test_verify_credentials(api): account_a = api.account_verify_credentials() From a99d5a83c9a755a497fbb35a14be9f0e8668934d Mon Sep 17 00:00:00 2001 From: Alex Camilleri Date: Wed, 23 Oct 2024 01:26:52 +0200 Subject: [PATCH 2/5] Reverted some auto formatting --- mastodon/accounts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mastodon/accounts.py b/mastodon/accounts.py index 439ee75..bbcbf49 100644 --- a/mastodon/accounts.py +++ b/mastodon/accounts.py @@ -84,7 +84,7 @@ class Mastodon(Internals): self.access_token = response['access_token'] self.__set_refresh_token(response.get('refresh_token')) self.__set_token_expired(int(response.get('expires_in', 0))) - except Exception: + except Exception as e: raise MastodonIllegalArgumentError('Invalid request') # Step 3: Check scopes, persist, et cetera @@ -265,7 +265,7 @@ class Mastodon(Internals): """ params = self.__generate_params(locals()) - if params["following"] is False: + if params["following"] == False: del params["following"] return self.__api_request('GET', '/api/v1/accounts/search', params) From fba5e6a9e6da8a712686a50fcee00e1674a0e718 Mon Sep 17 00:00:00 2001 From: Alex Camilleri Date: Wed, 23 Oct 2024 01:32:35 +0200 Subject: [PATCH 3/5] key fix --- mastodon/accounts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mastodon/accounts.py b/mastodon/accounts.py index bbcbf49..47e2436 100644 --- a/mastodon/accounts.py +++ b/mastodon/accounts.py @@ -141,7 +141,7 @@ class Mastodon(Internals): Does not require authentication for publicly visible accounts. """ ids = [self.__unpack_id(id) for id in ids] - return self.__api_request('GET', '/api/v1/accounts', {"id": ids}) + return self.__api_request('GET', '/api/v1/accounts', {"id[]": ids}) @api_version("1.0.0", "2.1.0", _DICT_VERSION_ACCOUNT) def account_verify_credentials(self) -> Account: From 02dd7967cbdf6087a599dbcd556633095fbb54e0 Mon Sep 17 00:00:00 2001 From: Nano Date: Fri, 22 Nov 2024 18:02:48 +0800 Subject: [PATCH 4/5] fix(statuses): prevent dynamic changes to `keyword_args` The use of `locals()` in status_reply caused issues due to its dynamic nature. When `user_id = self.__get_logged_in_id()` was executed, `locals()` dynamically updated `keyword_args` to include the `self` reference again, even after it was explicitly deleted. This led to a TypeError: `Mastodon.status_post() got multiple values for argument 'self'`. This commit resolves the issue by replacing `keyword_args = locals()` with `keyword_args = locals().copy()`. By creating a static copy of the local variables at the time of execution, it prevents unintended modifications caused by dynamic changes to the local scope. Fixed #388 --- mastodon/statuses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mastodon/statuses.py b/mastodon/statuses.py index 0d349ef..b366a91 100644 --- a/mastodon/statuses.py +++ b/mastodon/statuses.py @@ -332,7 +332,7 @@ class Mastodon(Internals): are replying to, removing every other mentioned user from the conversation. """ - keyword_args = locals() + keyword_args = locals().copy() del keyword_args["self"] del keyword_args["to_status"] del keyword_args["untag"] From e3a16f4fd58d7dd948ba68c81d50a26158aa7838 Mon Sep 17 00:00:00 2001 From: Konano Date: Fri, 22 Nov 2024 18:17:09 +0800 Subject: [PATCH 5/5] fix(polls): prevent dynamic changes to `poll_params` --- mastodon/polls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mastodon/polls.py b/mastodon/polls.py index 20a1d87..013dfe9 100644 --- a/mastodon/polls.py +++ b/mastodon/polls.py @@ -56,6 +56,6 @@ class Mastodon(Internals): Set multiple to True to allow people to choose more than one answer. Set hide_totals to True to hide the results of the poll until it has expired. """ - poll_params = locals() + poll_params = locals().copy() del poll_params["self"] return poll_params