Skip to content

Commit

Permalink
[mastodon] add 'following' extractor (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Sep 25, 2021
1 parent 2c29329 commit 9377543
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions gallery_dl/extractor/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ def statuses(self):
)


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"
Expand Down Expand Up @@ -170,6 +193,10 @@ def account_id_by_username(self, username):
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"
Expand Down
2 changes: 2 additions & 0 deletions test/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def _run_test(self, extr, url, result):
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:
Expand Down

0 comments on commit 9377543

Please sign in to comment.