forked from bluesky-social/cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_bsky_post.py
executable file
·378 lines (327 loc) · 12 KB
/
create_bsky_post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python3
"""
Script demonstrating how to create posts using the Bluesky API, covering most of the features and embed options.
To run this Python script, you need the 'requests' and 'bs4' (BeautifulSoup) packages installed.
"""
import re
import os
import sys
import json
import argparse
from typing import Dict, List
from datetime import datetime, timezone
import requests
from bs4 import BeautifulSoup
def bsky_login_session(pds_url: str, handle: str, password: str) -> Dict:
resp = requests.post(
pds_url + "/xrpc/com.atproto.server.createSession",
json={"identifier": handle, "password": password},
)
resp.raise_for_status()
return resp.json()
def parse_mentions(text: str) -> List[Dict]:
spans = []
# regex based on: https://atproto.com/specs/handle#handle-identifier-syntax
mention_regex = rb"[$|\W](@([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(mention_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"handle": m.group(1)[1:].decode("UTF-8"),
}
)
return spans
def test_parse_mentions():
assert parse_mentions("prefix @handle.example.com @handle.com suffix") == [
{"start": 7, "end": 26, "handle": "handle.example.com"},
{"start": 27, "end": 38, "handle": "handle.com"},
]
assert parse_mentions("handle.example.com") == []
assert parse_mentions("@bare") == []
assert parse_mentions("💩💩💩 @handle.example.com") == [
{"start": 13, "end": 32, "handle": "handle.example.com"}
]
assert parse_mentions("email@example.com") == []
assert parse_mentions("cc:@example.com") == [
{"start": 3, "end": 15, "handle": "example.com"}
]
def parse_urls(text: str) -> List[Dict]:
spans = []
# partial/naive URL regex based on: https://stackoverflow.com/a/3809435
# tweaked to disallow some training punctuation
url_regex = rb"[$|\W](https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(url_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"url": m.group(1).decode("UTF-8"),
}
)
return spans
def test_parse_urls():
assert parse_urls(
"prefix https://example.com/index.html http://bsky.app suffix"
) == [
{"start": 7, "end": 37, "url": "https://example.com/index.html"},
{"start": 38, "end": 53, "url": "http://bsky.app"},
]
assert parse_urls("example.com") == []
assert parse_urls("💩💩💩 http://bsky.app") == [
{"start": 13, "end": 28, "url": "http://bsky.app"}
]
assert parse_urls("runonhttp://blah.comcontinuesafter") == []
assert parse_urls("ref [https://bsky.app]") == [
{"start": 5, "end": 21, "url": "https://bsky.app"}
]
# note: a better regex would not mangle these:
assert parse_urls("ref (https://bsky.app/)") == [
{"start": 5, "end": 22, "url": "https://bsky.app/"}
]
assert parse_urls("ends https://bsky.app. what else?") == [
{"start": 5, "end": 21, "url": "https://bsky.app"}
]
def parse_facets(pds_url: str, text: str) -> List[Dict]:
"""
parses post text and returns a list of app.bsky.richtext.facet objects for any mentions (@handle.example.com) or URLs (https://example.com)
indexing must work with UTF-8 encoded bytestring offsets, not regular unicode string offsets, to match Bluesky API expectations
"""
facets = []
for m in parse_mentions(text):
resp = requests.get(
pds_url + "/xrpc/com.atproto.identity.resolveHandle",
params={"handle": m["handle"]},
)
# if handle couldn't be resolved, just skip it! will be text in the post
if resp.status_code == 400:
continue
did = resp.json()["did"]
facets.append(
{
"index": {
"byteStart": m["start"],
"byteEnd": m["end"],
},
"features": [{"$type": "app.bsky.richtext.facet#mention", "did": did}],
}
)
for u in parse_urls(text):
facets.append(
{
"index": {
"byteStart": u["start"],
"byteEnd": u["end"],
},
"features": [
{
"$type": "app.bsky.richtext.facet#link",
# NOTE: URI ("I") not URL ("L")
"uri": u["url"],
}
],
}
)
return facets
def parse_uri(uri: str) -> Dict:
if uri.startswith("at://"):
repo, collection, rkey = uri.split("/")[2:5]
return {"repo": repo, "collection": collection, "rkey": rkey}
elif uri.startswith("https://bsky.app/"):
repo, collection, rkey = uri.split("/")[4:7]
if collection == "post":
collection = "app.bsky.feed.post"
elif collection == "lists":
collection = "app.bsky.graph.list"
elif collection == "feed":
collection = "app.bsky.feed.generator"
return {"repo": repo, "collection": collection, "rkey": rkey}
else:
raise Exception("unhandled URI format: " + uri)
def get_reply_refs(pds_url: str, parent_uri: str) -> Dict:
uri_parts = parse_uri(parent_uri)
resp = requests.get(
pds_url + "/xrpc/com.atproto.repo.getRecord",
params=uri_parts,
)
resp.raise_for_status()
parent = resp.json()
root = parent
parent_reply = parent["value"].get("reply")
if parent_reply is not None:
root_uri = parent_reply["root"]["uri"]
root_repo, root_collection, root_rkey = root_uri.split("/")[2:5]
resp = requests.get(
pds_url + "/xrpc/com.atproto.repo.getRecord",
params={
"repo": root_repo,
"collection": root_collection,
"rkey": root_rkey,
},
)
resp.raise_for_status()
root = resp.json()
return {
"root": {
"uri": root["uri"],
"cid": root["cid"],
},
"parent": {
"uri": parent["uri"],
"cid": parent["cid"],
},
}
def upload_file(pds_url, access_token, filename, img_bytes) -> Dict:
suffix = filename.split(".")[-1].lower()
mimetype = "application/octet-stream"
if suffix in ["png"]:
mimetype = "image/png"
elif suffix in ["jpeg", "jpg"]:
mimetype = "image/jpeg"
elif suffix in ["webp"]:
mimetype = "image/webp"
# WARNING: a non-naive implementation would strip EXIF metadata from JPEG files here by default
resp = requests.post(
pds_url + "/xrpc/com.atproto.repo.uploadBlob",
headers={
"Content-Type": mimetype,
"Authorization": "Bearer " + access_token,
},
data=img_bytes,
)
resp.raise_for_status()
return resp.json()["blob"]
def upload_images(
pds_url: str, access_token: str, image_paths: List[str], alt_text: str
) -> Dict:
images = []
for ip in image_paths:
with open(ip, "rb") as f:
img_bytes = f.read()
# this size limit specified in the app.bsky.embed.images lexicon
if len(img_bytes) > 1000000:
raise Exception(
f"image file size too large. 1000000 bytes maximum, got: {len(img_bytes)}"
)
blob = upload_file(pds_url, access_token, ip, img_bytes)
images.append({"alt": alt_text or "", "image": blob})
return {
"$type": "app.bsky.embed.images",
"images": images,
}
def fetch_embed_url_card(pds_url: str, access_token: str, url: str) -> Dict:
# the required fields for an embed card
card = {
"uri": url,
"title": "",
"description": "",
}
# fetch the HTML
resp = requests.get(url)
resp.raise_for_status()
soup = BeautifulSoup(resp.text, "html.parser")
title_tag = soup.find("meta", property="og:title")
if title_tag:
card["title"] = title_tag["content"]
description_tag = soup.find("meta", property="og:description")
if description_tag:
card["description"] = description_tag["content"]
image_tag = soup.find("meta", property="og:image")
if image_tag:
img_url = image_tag["content"]
if "://" not in img_url:
img_url = url + img_url
resp = requests.get(img_url)
resp.raise_for_status()
card["thumb"] = upload_file(pds_url, access_token, img_url, resp.content)
return {
"$type": "app.bsky.embed.external",
"external": card,
}
def get_embed_ref(pds_url: str, ref_uri: str) -> Dict:
uri_parts = parse_uri(ref_uri)
resp = requests.get(
pds_url + "/xrpc/com.atproto.repo.getRecord",
params=uri_parts,
)
print(resp.json())
resp.raise_for_status()
record = resp.json()
return {
"$type": "app.bsky.embed.record",
"record": {
"uri": record["uri"],
"cid": record["cid"],
},
}
def create_post(args):
session = bsky_login_session(args.pds_url, args.handle, args.password)
# trailing "Z" is preferred over "+00:00"
now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
# these are the required fields which every post must include
post = {
"$type": "app.bsky.feed.post",
"text": args.text,
"createdAt": now,
}
# indicate included languages (optional)
if args.lang:
post["langs"] = args.lang
# parse out mentions and URLs as "facets"
if len(args.text) > 0:
facets = parse_facets(args.pds_url, post["text"])
if facets:
post["facets"] = facets
# if this is a reply, get references to the parent and root
if args.reply_to:
post["reply"] = get_reply_refs(args.pds_url, args.reply_to)
if args.image:
post["embed"] = upload_images(
args.pds_url, session["accessJwt"], args.image, args.alt_text
)
elif args.embed_url:
post["embed"] = fetch_embed_url_card(
args.pds_url, session["accessJwt"], args.embed_url
)
elif args.embed_ref:
post["embed"] = get_embed_ref(args.pds_url, args.embed_ref)
print("creating post:", file=sys.stderr)
print(json.dumps(post, indent=2), file=sys.stderr)
resp = requests.post(
args.pds_url + "/xrpc/com.atproto.repo.createRecord",
headers={"Authorization": "Bearer " + session["accessJwt"]},
json={
"repo": session["did"],
"collection": "app.bsky.feed.post",
"record": post,
},
)
print("createRecord response:", file=sys.stderr)
print(json.dumps(resp.json(), indent=2))
resp.raise_for_status()
def main():
parser = argparse.ArgumentParser(description="bsky.app post upload example script")
parser.add_argument(
"--pds-url", default=os.environ.get("ATP_PDS_HOST") or "https://bsky.social"
)
parser.add_argument("--handle", default=os.environ.get("ATP_AUTH_HANDLE"))
parser.add_argument("--password", default=os.environ.get("ATP_AUTH_PASSWORD"))
parser.add_argument("text", default="")
parser.add_argument("--image", action="append")
parser.add_argument("--alt-text")
parser.add_argument("--lang", action="append")
parser.add_argument("--reply-to")
parser.add_argument("--embed-url")
parser.add_argument("--embed-ref")
args = parser.parse_args()
if not (args.handle and args.password):
print("both handle and password are required", file=sys.stderr)
sys.exit(-1)
if args.image and len(args.image) > 4:
print("at most 4 images per post", file=sys.stderr)
sys.exit(-1)
create_post(args)
if __name__ == "__main__":
main()