Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit 20a79e6

Browse files
committed
Render https:// posts as at://
1 parent da39b44 commit 20a79e6

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

render-configs.py

+47-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,59 @@
33
import json
44
import os
55
import re
6+
import sys
7+
8+
import requests
69

710
WORKER_SENTINEL = "\n\n// CONFIGS\n\n"
811
LIST_ITEM_REGEX = re.compile(r"^- ")
12+
POST_REGEX = re.compile(r"^.*[\./]bsky\.app/profile/(.+?)/post/([a-z0-9]+)")
13+
14+
15+
def resolve_handles(handles):
16+
dids = {}
17+
for handle in handles:
18+
url = f"https://bsky.social/xrpc/com.atproto.identity.resolveHandle?handle={handle}"
19+
response = requests.get(url)
20+
if response.status_code == 200:
21+
did = response.json()["did"]
22+
dids[handle] = did
23+
return dids
924

1025

1126
def render_search_terms(search_terms):
12-
rv = []
13-
for term in search_terms:
14-
term = re.compile(LIST_ITEM_REGEX).sub("", term)
15-
rv.append(term)
16-
return rv
27+
handles = set()
28+
rendered_terms = []
29+
30+
# strip out list item markers
31+
terms = [re.compile(LIST_ITEM_REGEX).sub("", term) for term in search_terms]
32+
33+
# collect handles and pins
34+
for term in terms:
35+
post_matches = POST_REGEX.match(term)
36+
if post_matches:
37+
handle = post_matches.group(1)
38+
handles.add(handle)
39+
40+
# resolve handles
41+
dids = resolve_handles(handles)
42+
43+
# replace handles with DIDs
44+
for term in terms:
45+
post_matches = POST_REGEX.match(term)
46+
if post_matches:
47+
handle = post_matches.group(1)
48+
rkey = post_matches.group(2)
49+
did = dids[handle]
50+
if did:
51+
at_url = f"at://{did}/app.bsky.feed.post/{rkey}"
52+
rendered_terms.append(at_url)
53+
else:
54+
print(f"WARN: Failed to resolve handle {handle}", file=sys.stderr)
55+
else:
56+
rendered_terms.append(term)
57+
58+
return rendered_terms
1759

1860

1961
def parse_config(dirname, markdown_contents):

0 commit comments

Comments
 (0)