Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluesky post extractor #4722

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/supportedsites.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ Consider all sites to be NSFW unless otherwise known.
<td>Blogs, Labels, Posts, Search Results</td>
<td></td>
</tr>
<tr>
<td>Bluesky</td>
<td>https://bsky.app/</td>
<td>Posts</td>
<td></td>
</tr>
<tr>
<td>Bunkr</td>
<td>https://bunkrr.su/</td>
Expand Down
1 change: 1 addition & 0 deletions gallery_dl/extractor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bbc",
"behance",
"blogger",
"bluesky",
"bunkr",
"catbox",
"chevereto",
Expand Down
89 changes: 89 additions & 0 deletions gallery_dl/extractor/bluesky.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Extractors for https://bsky.social/"""

from .common import Extractor, Message
from .. import text, util

import json
import dataclasses
import posixpath

import chitose

@dataclasses.dataclass(unsafe_hash=True)
class PostReference():
user_id: str = dataclasses.field(hash=True)
post_id: str = dataclasses.field(hash=True)

class BlueskyAPI():
def __init__(self, extractor, instance="bsky.social"):
self.root = extractor.root
self.extractor = extractor

self.agent = chitose.BskyAgent(service=f'https://{instance}')

# BSKY_USER = extractor.config("username"),
# BSKY_PASSWD = extractor.config("password")

username, password = extractor._get_auth_info()

self.agent.login(username, password)

def bskyGetDid(self, user_id: str) -> str:
return json.loads(self.agent.get_profile(actor=user_id))['did']

def bskyTupleToUri(self, post_reference: PostReference) -> str:
return f"at://{self.bskyGetDid(post_reference.user_id)}/app.bsky.feed.post/{post_reference.post_id}"

def bskyGetThread(self, post_reference) -> dict:
thread_response = self.agent.get_post_thread(
uri=self.bskyTupleToUri(post_reference)
)
thread_response = json.loads(thread_response)

return thread_response

def getSkeetJson(self, post_reference):
thread_response = self.bskyGetThread(post_reference)
thread_response['thread']['post']['id'] = post_reference.post_id

json_obj = thread_response['thread']['post']

return json_obj

class _BlueskyPostExtractor(Extractor):
"""Extractor for bluesky posts"""
category = "bsky.social"
subcategory = "post"
directory_fmt = ("{category}", "{user_id}")
filename_fmt = "{post_id} {filename}.{extension}"
root = "https://bsky.social"
referer = False
pattern = r"(?:https?://)?bsky.app/profile/(?P<user_id>[^/]+)/post/(?P<post_id>[^ )]+)"
example = "https://bsky.app/profile/im.giovanh.com/post/3kaxkwevkn626"

def __init__(self, match):
Extractor.__init__(self, match)
self.user_id, self.post_id = match.groups()

self.api = BlueskyAPI(self)

self.json_obj = self.api.getSkeetJson(PostReference(self.user_id, self.post_id))
self.metadata = self.json_obj
self.metadata.update(match.groupdict())

def items(self):

yield Message.Directory, self.metadata

for image_def in self.json_obj.get('embed', {}).get('images', []):
src_url = image_def['fullsize']
name = posixpath.split(src_url)[-1].replace('@', '.')
img_meta = text.nameext_from_url(name, None)
img_meta.update(self.metadata)
yield Message.Url, src_url, img_meta
Loading