[mastodon] add 'following' extractor (#1891)

This commit is contained in:
Mike Fährmann
2021-09-25 23:55:52 +02:00
parent 2c2932973c
commit 9377543162
2 changed files with 29 additions and 0 deletions

View File

@@ -115,6 +115,29 @@ class MastodonUserExtractor(MastodonExtractor):
)
class MastodonFollowingExtractor(MastodonExtractor):
"""Extractor for followed mastodon users"""
subcategory = "following"
pattern = BASE_PATTERN + r"/users/([^/?#]+)/following"
test = (
("https://mastodon.social/users/0x4f/following", {
"extractor": False,
"count": ">= 20",
}),
("https://mastodon.social/users/id:10843/following"),
("https://pawoo.net/users/yoru_nine/following"),
("https://baraag.net/users/pumpkinnsfw/following"),
)
def items(self):
api = MastodonAPI(self)
account_id = api.account_id_by_username(self.item)
for account in api.account_following(account_id):
account["_extractor"] = MastodonUserExtractor
yield Message.Queue, account["url"], account
class MastodonStatusExtractor(MastodonExtractor):
"""Extractor for images from a status"""
subcategory = "status"
@@ -170,6 +193,10 @@ class MastodonAPI():
return account["id"]
raise exception.NotFoundError("account")
def account_following(self, account_id):
endpoint = "/v1/accounts/{}/following".format(account_id)
return self._pagination(endpoint, None)
def account_search(self, query, limit=40):
"""Search for accounts"""
endpoint = "/v1/accounts/search"

View File

@@ -91,6 +91,8 @@ class TestExtractorResults(unittest.TestCase):
for url, kwdict in zip(tjob.url_list, tjob.kwdict_list):
if "_extractor" in kwdict:
extr = kwdict["_extractor"].from_url(url)
if extr is None and not result.get("extractor", True):
continue
self.assertIsInstance(extr, kwdict["_extractor"])
self.assertEqual(extr.url, url)
else: