From 9c29c904c757330f5190f7ba8cf47104320fd10d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 13 Apr 2023 14:03:23 +0200 Subject: [PATCH] [mastodon] try to get account IDs without access token Try to query the public '/api/v1/accounts/lookup' endpoint and fall back to '/v1/accounts/search' if it returns an error. '/api/v1/accounts/lookup' is available since Mastodon v3.4.0. The version of an instance can be found at '/api/v1/instance'. --- gallery_dl/extractor/mastodon.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gallery_dl/extractor/mastodon.py b/gallery_dl/extractor/mastodon.py index e49d29af..e190c7eb 100644 --- a/gallery_dl/extractor/mastodon.py +++ b/gallery_dl/extractor/mastodon.py @@ -227,6 +227,12 @@ class MastodonAPI(): if username.startswith("id:"): return username[3:] + try: + return self.account_lookup(username)["id"] + except Exception: + # fall back to account search + pass + if "@" in username: handle = "@" + username else: @@ -246,6 +252,11 @@ class MastodonAPI(): endpoint = "/v1/accounts/{}/following".format(account_id) return self._pagination(endpoint, None) + def account_lookup(self, username): + endpoint = "/v1/accounts/lookup" + params = {"acct": username} + return self._call(endpoint, params).json() + def account_search(self, query, limit=40): """Search for accounts""" endpoint = "/v1/accounts/search"