From 3efc8d1d5599a3bd4e1e2691a2f13baaadd9cc42 Mon Sep 17 00:00:00 2001 From: Panniantong Date: Tue, 24 Feb 2026 12:49:58 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20Reddit=20=E5=AD=90=E7=89=88=E5=9D=97?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E9=A1=B5=E6=94=AF=E6=8C=81=20+=20--version?= =?UTF-8?q?=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - reddit.py: 新增 _parse_listing() 处理 /r/sub/hot|new|top 等列表页 之前只能读单个帖子,列表页直接报 ValueError - cli.py: 添加 --version flag (标准 CLI 惯例) --- agent_reach/channels/reddit.py | 50 ++++++++++++++++++++++++++++++++++ agent_reach/cli.py | 1 + 2 files changed, 51 insertions(+) diff --git a/agent_reach/channels/reddit.py b/agent_reach/channels/reddit.py index 174006a..30bbfbf 100644 --- a/agent_reach/channels/reddit.py +++ b/agent_reach/channels/reddit.py @@ -74,6 +74,10 @@ class RedditChannel(Channel): data = resp.json() + # Subreddit listing page: /r/sub/, /r/sub/hot, /r/sub/new, /r/sub/top + if isinstance(data, dict) and data.get("kind") == "Listing": + return self._parse_listing(data, url) + if isinstance(data, list) and len(data) >= 1: # Post page: [post_listing, comments_listing] post = data[0]["data"]["children"][0]["data"] @@ -103,6 +107,52 @@ class RedditChannel(Channel): raise ValueError(f"Could not parse Reddit response for: {url}") + def _parse_listing(self, data: dict, url: str) -> ReadResult: + """Parse a subreddit listing (hot/new/top/rising).""" + children = data.get("data", {}).get("children", []) + + # Extract subreddit name and sort from URL + parsed = urlparse(url) + path_parts = [p for p in parsed.path.strip("/").split("/") if p] + subreddit = path_parts[1] if len(path_parts) >= 2 else "reddit" + sort_type = path_parts[2] if len(path_parts) >= 3 else "hot" + + lines = [] + for i, child in enumerate(children, 1): + if child.get("kind") != "t3": + continue + post = child.get("data", {}) + title = post.get("title", "") + author = post.get("author", "") + score = post.get("score", 0) + num_comments = post.get("num_comments", 0) + permalink = post.get("permalink", "") + post_url = post.get("url", "") + is_self = post.get("is_self", False) + + lines.append(f"### {i}. {title}") + lines.append(f"👤 u/{author} · ⬆ {score} · 💬 {num_comments}") + if not is_self and post_url: + lines.append(f"🔗 {post_url}") + lines.append(f"📎 https://www.reddit.com{permalink}") + # Add selftext preview (first 200 chars) + selftext = post.get("selftext", "") + if selftext: + preview = selftext[:200].replace("\n", " ") + if len(selftext) > 200: + preview += "..." + lines.append(f"> {preview}") + lines.append("") + + content = "\n".join(lines) if lines else "No posts found." + return ReadResult( + title=f"r/{subreddit} — {sort_type}", + content=content, + url=url, + platform="reddit", + extra={"subreddit": subreddit, "sort": sort_type, "count": len(children)}, + ) + def _extract_comments(self, comments_data: dict, depth: int = 0, max_depth: int = 3) -> str: """Recursively extract comments.""" lines = [] diff --git a/agent_reach/cli.py b/agent_reach/cli.py index 4d6ba2c..3ed5105 100644 --- a/agent_reach/cli.py +++ b/agent_reach/cli.py @@ -36,6 +36,7 @@ def main(): description="👁️ Give your AI Agent eyes to see the entire internet", ) parser.add_argument("-v", "--verbose", action="store_true", help="Show debug logs") + parser.add_argument("--version", action="version", version=f"Agent Reach v{__version__}") sub = parser.add_subparsers(dest="command", help="Available commands") # ── read ──