Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117#!/usr/bin/env python3"""The atproto custom feed server — the main feed as a normal Bluesky feed.
Implements `app.bsky.feed.getFeedSkeleton` for the main feed: the union ofevery post the fit judge confirmed for any topic, deduped, ordered by gradethen recency. Because aligned.click uses a did:web feed generator, the PDSresolves the generator record and calls back here athttps://aligned.click/xrpc/app.bsky.feed.getFeedSkeleton.
The feed is public — a custom feed is a public surface. The skeleton carriesonly at:// URIs; the actual post content is served by each author's PDS, andthe extras (grade, why, topic) live on aligned.click's own pages instead.
python3 feedgen/server.py # http://127.0.0.1:8784
The generator record (the `app.bsky.feed.generator` under the aligned.clickaccount) is published separately — see lexicons/ and auth/."""
import argparseimport jsonimport osimport sysfrom http.server import BaseHTTPRequestHandler, ThreadingHTTPServerfrom pathlib import Path
ROOT = Path(__file__).parent.parentsys.path.insert(0, str(ROOT))
from feed import store # noqa: E402from shared.log import log # noqa: E402
PORT = int(os.environ.get("FEEDGEN_PORT", "8784"))# The generator record this server answers for. Anything else is refused.MAIN_FEED_URI = os.environ.get("FEEDGEN_MAIN_URI", "at://aligned.click/app.bsky.feed.generator/main")# Default quality floor for the main feed skeleton — a feed should be good# until topic density suggests otherwise; env-tunable.MIN_GRADE = int(os.environ.get("FEEDGEN_MIN_GRADE", "6"))# Default fit floor for the main feed skeleton — how strong a fit must be for# a post to count; env-tunable.MIN_FIT = int(os.environ.get("FEEDGEN_MIN_FIT", "5"))
PAGE = 50
class Handler(BaseHTTPRequestHandler): protocol_version = "HTTP/1.1" server_version = "aligned-feedgen"
def reply(self, status, body=b"", content_type="application/json; charset=utf-8"): if isinstance(body, (dict, list)): body = json.dumps(body).encode() elif isinstance(body, str): body = body.encode() self.send_response(status) self.send_header("Content-Type", content_type) self.send_header("Content-Length", str(len(body))) self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() if self.command != "HEAD": self.wfile.write(body)
def send_error(self, code, message=None, explain=None): # noqa: A003 self.reply(code, {"error": message or "error"})
def log_message(self, format, *args): # noqa: A003 pass
def log_request(self, status): try: line = f"{self.command} {self.path} -> {status}" except Exception: # noqa: BLE001 line = f"{self.command} {self.path}" log(line)
def do_GET(self): if self.path == "/healthz": return self.reply(200, {"ok": True}) if self.path.startswith("/xrpc/app.bsky.feed.getFeedSkeleton"): return self.skeleton() return self.reply(404, {"error": "not found"})
def skeleton(self): from urllib.parse import urlparse, parse_qs params = parse_qs(urlparse(self.path).query) feed = (params.get("feed") or [""])[0] if feed != MAIN_FEED_URI: return self.reply(400, {"error": "unknown feed"}) limit = min(int((params.get("limit") or [PAGE])[0]), 100) cursor = int((params.get("cursor") or ["0"])[0]) posts = store.main_feed(limit + cursor, min_grade=MIN_GRADE, min_fit=MIN_FIT) window = posts[cursor:cursor + limit] next_cursor = str(cursor + len(window)) if len(window) == limit and \ cursor + len(window) < len(posts) else None body = {"feed": [{"post": p["uri"]} for p in window]} if next_cursor: body["cursor"] = next_cursor return self.reply(200, body)
def main() -> None: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--port", type=int, default=PORT) args = parser.parse_args() server = ThreadingHTTPServer(("127.0.0.1", args.port), Handler) log(f"feedgen at http://127.0.0.1:{args.port} (feed {MAIN_FEED_URI})") try: server.serve_forever() except KeyboardInterrupt: pass
if __name__ == "__main__": main()