-
Notifications
You must be signed in to change notification settings - Fork 3
/
bsky_post.py
53 lines (44 loc) · 1.85 KB
/
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
from datetime import datetime
from atproto import Client, models
def bsky_post_xp(client, image_files, text, links):
if image_files:
post_images = []
for image_file in image_files:
print("Creating image upload for", image_file)
with open(image_file, 'rb') as f:
img_data = f.read()
upload = client.com.atproto.repo.upload_blob(img_data)
# TODO image alt texts, copy from Twitter?
post_images.append(models.AppBskyEmbedImages.Image(alt='Img alt', image=upload.blob))
embed = models.AppBskyEmbedImages.Main(images=post_images)
else:
embed = None
facets = []
for link in links:
if text.find(link) != -1:
print("Creating facet for", link)
# Find the starting index of the URL
start_index = text.find(link)
# Calculate the byte start and byte end
byte_start = len(text[:start_index].encode('utf-8'))
byte_end = byte_start + len(link.encode('utf-8'))
#print(byte_start, byte_end)
facets.append(
models.AppBskyRichtextFacet.Main(
features=[models.AppBskyRichtextFacet.Link(uri=link)],
index=models.AppBskyRichtextFacet.ByteSlice(byteStart=byte_start, byteEnd=byte_end),
)
)
client.com.atproto.repo.create_record(
models.ComAtprotoRepoCreateRecord.Data(
repo=client.me.did,
collection=models.ids.AppBskyFeedPost,
record=models.AppBskyFeedPost.Main(
createdAt=datetime.now().isoformat(), text=text, embed=embed, facets=facets
),
)
)
def bsky_post(user, pw, text, image_files, links):
client = Client()
client.login(user, pw)
bsky_post_xp(client, image_files, text, links)