Skip to content

Commit

Permalink
[bluesky] allow using DIDs as user handles (#4438)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Feb 8, 2024
1 parent aee5580 commit 6aea818
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions gallery_dl/extractor/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BlueskyExtractor(Extractor):

def __init__(self, match):
Extractor.__init__(self, match)
self.handle = match.group(1)
self.user = match.group(1)

def _init(self):
self.api = BlueskyAPI(self)
Expand Down Expand Up @@ -77,27 +77,22 @@ def initialize(self):
pass

def items(self):
base = "{}/profile/{}/".format(self.root, self.handle)
base = "{}/profile/{}/".format(self.root, self.user)
return self._dispatch_extractors((
(BlueskyPostsExtractor , base + "posts"),
(BlueskyRepliesExtractor, base + "replies"),
(BlueskyMediaExtractor , base + "media"),
(BlueskyLikesExtractor , base + "likes"),
), ("media",))

def posts(self):
did = self.api.resolve_handle(self.handle)
return self.api.get_author_feed(did)


class BlueskyPostsExtractor(BlueskyExtractor):
subcategory = "posts"
pattern = BASE_PATTERN + r"/profile/([^/?#]+)/posts"
example = "https://bsky.app/profile/HANDLE/posts"

def posts(self):
did = self.api.resolve_handle(self.handle)
return self.api.get_author_feed(did, "posts_and_author_threads")
return self.api.get_author_feed(self.user, "posts_and_author_threads")


class BlueskyRepliesExtractor(BlueskyExtractor):
Expand All @@ -106,8 +101,7 @@ class BlueskyRepliesExtractor(BlueskyExtractor):
example = "https://bsky.app/profile/HANDLE/replies"

def posts(self):
did = self.api.resolve_handle(self.handle)
return self.api.get_author_feed(did, "posts_with_replies")
return self.api.get_author_feed(self.user, "posts_with_replies")


class BlueskyMediaExtractor(BlueskyExtractor):
Expand All @@ -116,8 +110,7 @@ class BlueskyMediaExtractor(BlueskyExtractor):
example = "https://bsky.app/profile/HANDLE/media"

def posts(self):
did = self.api.resolve_handle(self.handle)
return self.api.get_author_feed(did, "posts_with_media")
return self.api.get_author_feed(self.user, "posts_with_media")


class BlueskyLikesExtractor(BlueskyExtractor):
Expand All @@ -126,8 +119,7 @@ class BlueskyLikesExtractor(BlueskyExtractor):
example = "https://bsky.app/profile/HANDLE/likes"

def posts(self):
did = self.api.resolve_handle(self.handle)
return self.api.get_actor_likes(did)
return self.api.get_actor_likes(self.user)


class BlueskyPostExtractor(BlueskyExtractor):
Expand All @@ -140,8 +132,7 @@ def __init__(self, match):
self.post_id = match.group(2)

def posts(self):
did = self.api.resolve_handle(self.handle)
return self.api.get_post_thread(did, self.post_id)
return self.api.get_post_thread(self.user, self.post_id)


class BlueskyAPI():
Expand All @@ -165,15 +156,15 @@ def __init__(self, extractor):
def get_actor_likes(self, actor):
endpoint = "app.bsky.feed.getActorLikes"
params = {
"actor": actor,
"actor": self._did_from_actor(actor),
"limit": "100",
}
return self._pagination(endpoint, params)

def get_author_feed(self, actor, filter="posts_and_author_threads"):
endpoint = "app.bsky.feed.getAuthorFeed"
params = {
"actor" : actor,
"actor" : self._did_from_actor(actor),
"filter": filter,
"limit" : "100",
}
Expand All @@ -182,13 +173,14 @@ def get_author_feed(self, actor, filter="posts_and_author_threads"):
def get_post_thread(self, actor, post_id):
endpoint = "app.bsky.feed.getPostThread"
params = {
"uri": "at://{}/app.bsky.feed.post/{}".format(actor, post_id),
"uri": "at://{}/app.bsky.feed.post/{}".format(
self._did_from_actor(actor), post_id),
}
return (self._call(endpoint, params)["thread"],)

def get_profile(self, actor):
endpoint = "app.bsky.actor.getProfile"
params = {"actor": actor}
params = {"actor": self._did_from_actor(actor)}
return self._call(endpoint, params)

@memcache(keyarg=1)
Expand All @@ -197,6 +189,11 @@ def resolve_handle(self, handle):
params = {"handle": handle}
return self._call(endpoint, params)["did"]

def _did_from_actor(self, actor):
if actor.startswith("did:"):
return actor
return self.resolve_handle(actor)

def authenticate(self):
self.headers["Authorization"] = self._authenticate_impl(self.username)

Expand Down

0 comments on commit 6aea818

Please sign in to comment.