1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +01:00

[bluesky] allow using DIDs as user handles (#4438)

This commit is contained in:
Mike Fährmann 2024-02-08 20:15:54 +01:00
parent aee5580c62
commit 6aea818d4e
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -25,7 +25,7 @@ class BlueskyExtractor(Extractor):
def __init__(self, match): def __init__(self, match):
Extractor.__init__(self, match) Extractor.__init__(self, match)
self.handle = match.group(1) self.user = match.group(1)
def _init(self): def _init(self):
self.api = BlueskyAPI(self) self.api = BlueskyAPI(self)
@ -77,7 +77,7 @@ class BlueskyUserExtractor(BlueskyExtractor):
pass pass
def items(self): def items(self):
base = "{}/profile/{}/".format(self.root, self.handle) base = "{}/profile/{}/".format(self.root, self.user)
return self._dispatch_extractors(( return self._dispatch_extractors((
(BlueskyPostsExtractor , base + "posts"), (BlueskyPostsExtractor , base + "posts"),
(BlueskyRepliesExtractor, base + "replies"), (BlueskyRepliesExtractor, base + "replies"),
@ -85,10 +85,6 @@ class BlueskyUserExtractor(BlueskyExtractor):
(BlueskyLikesExtractor , base + "likes"), (BlueskyLikesExtractor , base + "likes"),
), ("media",)) ), ("media",))
def posts(self):
did = self.api.resolve_handle(self.handle)
return self.api.get_author_feed(did)
class BlueskyPostsExtractor(BlueskyExtractor): class BlueskyPostsExtractor(BlueskyExtractor):
subcategory = "posts" subcategory = "posts"
@ -96,8 +92,7 @@ class BlueskyPostsExtractor(BlueskyExtractor):
example = "https://bsky.app/profile/HANDLE/posts" example = "https://bsky.app/profile/HANDLE/posts"
def posts(self): def posts(self):
did = self.api.resolve_handle(self.handle) return self.api.get_author_feed(self.user, "posts_and_author_threads")
return self.api.get_author_feed(did, "posts_and_author_threads")
class BlueskyRepliesExtractor(BlueskyExtractor): class BlueskyRepliesExtractor(BlueskyExtractor):
@ -106,8 +101,7 @@ class BlueskyRepliesExtractor(BlueskyExtractor):
example = "https://bsky.app/profile/HANDLE/replies" example = "https://bsky.app/profile/HANDLE/replies"
def posts(self): def posts(self):
did = self.api.resolve_handle(self.handle) return self.api.get_author_feed(self.user, "posts_with_replies")
return self.api.get_author_feed(did, "posts_with_replies")
class BlueskyMediaExtractor(BlueskyExtractor): class BlueskyMediaExtractor(BlueskyExtractor):
@ -116,8 +110,7 @@ class BlueskyMediaExtractor(BlueskyExtractor):
example = "https://bsky.app/profile/HANDLE/media" example = "https://bsky.app/profile/HANDLE/media"
def posts(self): def posts(self):
did = self.api.resolve_handle(self.handle) return self.api.get_author_feed(self.user, "posts_with_media")
return self.api.get_author_feed(did, "posts_with_media")
class BlueskyLikesExtractor(BlueskyExtractor): class BlueskyLikesExtractor(BlueskyExtractor):
@ -126,8 +119,7 @@ class BlueskyLikesExtractor(BlueskyExtractor):
example = "https://bsky.app/profile/HANDLE/likes" example = "https://bsky.app/profile/HANDLE/likes"
def posts(self): def posts(self):
did = self.api.resolve_handle(self.handle) return self.api.get_actor_likes(self.user)
return self.api.get_actor_likes(did)
class BlueskyPostExtractor(BlueskyExtractor): class BlueskyPostExtractor(BlueskyExtractor):
@ -140,8 +132,7 @@ class BlueskyPostExtractor(BlueskyExtractor):
self.post_id = match.group(2) self.post_id = match.group(2)
def posts(self): def posts(self):
did = self.api.resolve_handle(self.handle) return self.api.get_post_thread(self.user, self.post_id)
return self.api.get_post_thread(did, self.post_id)
class BlueskyAPI(): class BlueskyAPI():
@ -165,7 +156,7 @@ class BlueskyAPI():
def get_actor_likes(self, actor): def get_actor_likes(self, actor):
endpoint = "app.bsky.feed.getActorLikes" endpoint = "app.bsky.feed.getActorLikes"
params = { params = {
"actor": actor, "actor": self._did_from_actor(actor),
"limit": "100", "limit": "100",
} }
return self._pagination(endpoint, params) return self._pagination(endpoint, params)
@ -173,7 +164,7 @@ class BlueskyAPI():
def get_author_feed(self, actor, filter="posts_and_author_threads"): def get_author_feed(self, actor, filter="posts_and_author_threads"):
endpoint = "app.bsky.feed.getAuthorFeed" endpoint = "app.bsky.feed.getAuthorFeed"
params = { params = {
"actor" : actor, "actor" : self._did_from_actor(actor),
"filter": filter, "filter": filter,
"limit" : "100", "limit" : "100",
} }
@ -182,13 +173,14 @@ class BlueskyAPI():
def get_post_thread(self, actor, post_id): def get_post_thread(self, actor, post_id):
endpoint = "app.bsky.feed.getPostThread" endpoint = "app.bsky.feed.getPostThread"
params = { 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"],) return (self._call(endpoint, params)["thread"],)
def get_profile(self, actor): def get_profile(self, actor):
endpoint = "app.bsky.actor.getProfile" endpoint = "app.bsky.actor.getProfile"
params = {"actor": actor} params = {"actor": self._did_from_actor(actor)}
return self._call(endpoint, params) return self._call(endpoint, params)
@memcache(keyarg=1) @memcache(keyarg=1)
@ -197,6 +189,11 @@ class BlueskyAPI():
params = {"handle": handle} params = {"handle": handle}
return self._call(endpoint, params)["did"] 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): def authenticate(self):
self.headers["Authorization"] = self._authenticate_impl(self.username) self.headers["Authorization"] = self._authenticate_impl(self.username)