Skip to content

Commit 16564d1

Browse files
authored
Update post_repo_to_bsky.py
1 parent 1d149d2 commit 16564d1

File tree

1 file changed

+81
-25
lines changed

1 file changed

+81
-25
lines changed

scripts/post_repo_to_bsky.py

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,82 @@
1-
# Create post text
2-
post_text = f"📦 {title}\n{summary}\n{url}"
3-
4-
# Find character‐based indices
5-
char_start = post_text.find(url)
6-
if char_start < 0:
7-
raise ValueError("URL not found in post_text")
8-
9-
# Compute byte‐based indices
10-
prefix = post_text[:char_start]
11-
byte_start = len(prefix.encode('utf-8'))
12-
byte_end = byte_start + len(url.encode('utf-8'))
13-
14-
# Create the facet
15-
facets = [
16-
{
17-
"index": {"byteStart": byte_start, "byteEnd": byte_end},
18-
"features": [
19-
{
20-
"$type": "app.bsky.richtext.facet#link",
21-
"uri": url
22-
}
23-
]
24-
}
25-
]
1+
#!/usr/bin/env python3
2+
import os
3+
import random
4+
import requests
5+
from atproto import Client
6+
from summarize import summarize
7+
8+
GITHUB_API = "https://api.github.com"
9+
USERNAME = os.environ["GITHUB_USERNAME"]
10+
ORGS = os.environ.get("GITHUB_ORGS", "")
11+
TOKEN = os.environ.get("GITHUB_PAT") or os.environ.get("GITHUB_TOKEN")
12+
13+
HEADERS = {
14+
"Accept": "application/vnd.github+json",
15+
**({"Authorization": f"Bearer {TOKEN}"} if TOKEN else {})
16+
}
17+
18+
def fetch_repos_for_user(user: str):
19+
url = f"{GITHUB_API}/users/{user}/repos?per_page=100&type=public"
20+
r = requests.get(url, headers=HEADERS)
21+
r.raise_for_status()
22+
return r.json()
23+
24+
def choose_repo():
25+
# get your user’s repos
26+
repos = fetch_repos_for_user(USERNAME)
27+
# plus any orgs you’ve listed
28+
for org in [o.strip() for o in ORGS.split(",") if o.strip()]:
29+
repos += fetch_repos_for_user(org)
30+
31+
# filter to >=5 stars
32+
eligible = [r for r in repos if r.get("stargazers_count", 0) >= 5]
33+
if not eligible:
34+
raise RuntimeError("No repositories found with ≥5 stars.")
35+
return random.choice(eligible)
36+
37+
def make_post_text(repo: dict):
38+
title = repo["name"]
39+
url = repo["html_url"]
40+
desc = repo.get("description") or ""
41+
summary = summarize(url, desc)
42+
post_text = f"📦 {title}\n\n{summary}\n\n{url}"
43+
return post_text, url
44+
45+
def make_facets(post_text: str, url: str):
46+
# find character-based index
47+
char_start = post_text.find(url)
48+
if char_start < 0:
49+
raise ValueError(f"URL '{url}' not found in the post text")
50+
51+
# compute byte offsets
52+
prefix_bytes = post_text[:char_start].encode("utf-8")
53+
url_bytes = url.encode("utf-8")
54+
byte_start = len(prefix_bytes)
55+
byte_end = byte_start + len(url_bytes)
56+
57+
print(f"→ link byteStart={byte_start}, byteEnd={byte_end}")
58+
59+
return [
60+
{
61+
"index": {"byteStart": byte_start, "byteEnd": byte_end},
62+
"features": [
63+
{
64+
"$type": "app.bsky.richtext.facet#link",
65+
"uri": url
66+
}
67+
]
68+
}
69+
]
70+
71+
def post_to_bsky(post_text: str, facets: list):
72+
client = Client()
73+
client.login(os.environ["BSKY_HANDLE"], os.environ["BSKY_PASSWORD"])
74+
client.send_post(post_text, facets=facets)
75+
print("✅ Posted to Bluesky!")
76+
77+
if __name__ == "__main__":
78+
repo = choose_repo()
79+
post_text, url = make_post_text(repo)
80+
facets = make_facets(post_text, url)
81+
post_to_bsky(post_text, facets)
2682

0 commit comments

Comments
 (0)