|
3 | 3 | import json
|
4 | 4 | import os
|
5 | 5 | import re
|
| 6 | +import sys |
| 7 | + |
| 8 | +import requests |
6 | 9 |
|
7 | 10 | WORKER_SENTINEL = "\n\n// CONFIGS\n\n"
|
8 | 11 | 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 |
9 | 24 |
|
10 | 25 |
|
11 | 26 | 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 |
17 | 59 |
|
18 | 60 |
|
19 | 61 | def parse_config(dirname, markdown_contents):
|
|
0 commit comments