Skip to content

Give your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.

feat: move reddit, facebook, instagram to unified entry via OpenCLI

Pnantc724ef8c33dc
diff --git a/agent_reach/backends/opencli.py b/agent_reach/backends/opencli.py
index a43eb981..e081370f 100644
--- a/agent_reach/backends/opencli.py
+++ b/agent_reach/backends/opencli.py
@@ -245,7 +245,7 @@ def opencli_call(site: str, args, *, timeout: float, login_hint: str = ""):
def opencli_items(rows) -> list:
"""Map OpenCLI result rows to unified items, keeping each row in `raw`."""
- from agent_reach.runtime.result import make_item
+ from agent_reach.runtime.result import make_item, unix_to_iso
if isinstance(rows, dict):
rows = rows.get("items") or rows.get("data") or [rows]
@@ -268,7 +268,11 @@ def opencli_items(rows) -> list:
title=pick("title", "name"),
url=pick("url", "link", "note_url"),
author=pick("author", "user", "nickname", "username"),
- published_at=pick("published_at", "created_at", "time", "date"),
+ published_at=(
+ unix_to_iso(r.get("created_utc"))
+ if isinstance(r.get("created_utc"), (int, float))
+ else pick("published_at", "created_at", "time", "date")
+ ),
text=pick("content", "text", "desc", "description", "body"),
raw=r,
)
diff --git a/agent_reach/channels/_opencli_site.py b/agent_reach/channels/_opencli_site.py
index 2c7ec3da..794b0053 100644
--- a/agent_reach/channels/_opencli_site.py
+++ b/agent_reach/channels/_opencli_site.py
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
"""Shared channel helper for OpenCLI browser-session-only platforms."""
+from agent_reach.backends.opencli import opencli_call, opencli_items
+from agent_reach.runtime.result import NOT_FOUND, UPSTREAM_BROKEN, ReachError
from agent_reach.utils.url import host_matches
from .base import Channel
@@ -45,3 +47,18 @@ class OpenCLISiteChannel(Channel):
f"需要时请先在 Chrome 里登录 {self.login_hint}"
)
return "warn", st.hint
+
+ # ── unified entry helper ──
+
+ def _opencli(self, args, timeout: float, *, empty_is_error: bool = True) -> list:
+ """Run one OpenCLI command for this site and return unified items."""
+ hint = (
+ f"Ask the user to log in to {self.login_hint} in their own Chrome (the one "
+ "with the OpenCLI extension). Never log in for them."
+ )
+ items = opencli_items(opencli_call(self.site, list(args), timeout=timeout, login_hint=hint))
+ if not items:
+ if empty_is_error:
+ raise ReachError(UPSTREAM_BROKEN, "OpenCLI returned nothing; the page most likely did not load")
+ raise ReachError(NOT_FOUND, "nothing found")
+ return items
diff --git a/agent_reach/channels/facebook.py b/agent_reach/channels/facebook.py
index 3ae8bd87..b36e830f 100644
--- a/agent_reach/channels/facebook.py
+++ b/agent_reach/channels/facebook.py
@@ -2,6 +2,7 @@
"""Facebook — OpenCLI backend using the user's logged-in Chrome session."""
from ._opencli_site import OpenCLISiteChannel
+from .base import Action, Param
class FacebookChannel(OpenCLISiteChannel):
@@ -11,3 +12,30 @@ class FacebookChannel(OpenCLISiteChannel):
domains = ("facebook.com", "fb.com", "fb.watch")
usage = "opencli facebook search/profile/feed/groups -f yaml"
login_hint = "facebook.com"
+
+ level = "experimental"
+ actions = (
+ Action(
+ "search",
+ "Search Facebook",
+ (
+ Param("query", "what to search for"),
+ Param("limit", "max results", required=False, type=int, default=5),
+ ),
+ (("opencli", "_opencli_search"),),
+ live_test={"query": "openai", "limit": 2},
+ ),
+ Action(
+ "profile",
+ "A user's or page's profile",
+ (Param("username", "username or page name"),),
+ (("opencli", "_opencli_profile"),),
+ live_test={"username": "zuck"},
+ ),
+ )
+
+ def _opencli_search(self, *, query: str, limit: int, timeout: float) -> list:
+ return self._opencli(["search", query, "--limit", str(limit)], timeout)[:limit]
+
+ def _opencli_profile(self, *, username: str, timeout: float) -> list:
+ return self._opencli(["profile", username], timeout, empty_is_error=False)[:1]
diff --git a/agent_reach/channels/instagram.py b/agent_reach/channels/instagram.py
index 6d607d4b..08c92b0c 100644
--- a/agent_reach/channels/instagram.py
+++ b/agent_reach/channels/instagram.py
@@ -2,6 +2,7 @@
"""Instagram — OpenCLI backend using the user's logged-in Chrome session."""
from ._opencli_site import OpenCLISiteChannel
+from .base import Action, Param
class InstagramChannel(OpenCLISiteChannel):
@@ -11,3 +12,43 @@ class InstagramChannel(OpenCLISiteChannel):
domains = ("instagram.com", "instagr.am")
usage = "opencli instagram search/profile/user/explore -f yaml"
login_hint = "instagram.com"
+
+ level = "experimental"
+ actions = (
+ Action(
+ "search",
+ "Search users (not posts)",
+ (
+ Param("query", "what to search for"),
+ Param("limit", "max results", required=False, type=int, default=5),
+ ),
+ (("opencli", "_opencli_search"),),
+ live_test={"query": "nasa", "limit": 2},
+ ),
+ Action(
+ "profile",
+ "A user's profile",
+ (Param("username", "Instagram username"),),
+ (("opencli", "_opencli_profile"),),
+ live_test={"username": "nasa"},
+ ),
+ Action(
+ "posts",
+ "A user's recent posts",
+ (
+ Param("username", "Instagram username"),
+ Param("limit", "max posts", required=False, type=int, default=6),
+ ),
+ (("opencli", "_opencli_posts"),),
+ live_test={"username": "nasa", "limit": 2},
+ ),
+ )
+
+ def _opencli_search(self, *, query: str, limit: int, timeout: float) -> list:
+ return self._opencli(["search", query, "--limit", str(limit)], timeout)[:limit]
+
+ def _opencli_profile(self, *, username: str, timeout: float) -> list:
+ return self._opencli(["profile", username], timeout, empty_is_error=False)[:1]
+
+ def _opencli_posts(self, *, username: str, limit: int, timeout: float) -> list:
+ return self._opencli(["user", username, "--limit", str(limit)], timeout)[:limit]
diff --git a/agent_reach/channels/reddit.py b/agent_reach/channels/reddit.py
index 83c681fb..85a9d528 100644
--- a/agent_reach/channels/reddit.py
+++ b/agent_reach/channels/reddit.py
@@ -14,12 +14,27 @@ import shutil
import time
from pathlib import Path
+from agent_reach.backends.opencli import opencli_call, opencli_items
+from agent_reach.runtime.result import BAD_INPUT, UPSTREAM_BROKEN, ReachError
from agent_reach.utils.paths import (
PrivatePathError,
read_small_text_no_follow,
)
-from .base import Channel
+from .base import Action, Channel, Param
+
+_REDDIT_LOGIN_HINT = (
+ "Ask the user to log in to reddit.com in their own Chrome (the one with the "
+ "OpenCLI extension). Never log in or read browser cookies for them."
+)
+
+
+def _reddit(args, timeout: float) -> list:
+ items = opencli_items(opencli_call("reddit", list(args), timeout=timeout, login_hint=_REDDIT_LOGIN_HINT))
+ if not items:
+ raise ReachError(UPSTREAM_BROKEN, "OpenCLI returned nothing; the page most likely did not load")
+ return items
+
_CREDENTIAL_FILE = "~/.config/rdt-cli/credential.json"
_CREDENTIAL_TTL_SECONDS = 7 * 86400
@@ -27,12 +42,43 @@ _MAX_CREDENTIAL_BYTES = 1024 * 1024
# Pinned to the 0.4.2 state — PyPI still only has 0.4.1 (upstream issue #10).
_RDT_GIT_SOURCE = "git+https://github.com/public-clis/rdt-cli.git@5e4fb3720d5c174e976cd425ccc3b879d52cac66"
+
class RedditChannel(Channel):
name = "reddit"
description = "Reddit 帖子和评论"
backends = ["OpenCLI", "rdt-cli"]
tier = 1 # no zero-config path exists — see module docstring
+ level = "optional"
+ actions = (
+ Action(
+ "search",
+ "Search posts",
+ (
+ Param("query", "what to search for"),
+ Param("limit", "max results", required=False, type=int, default=5),
+ ),
+ (("opencli", "_opencli_search"),),
+ live_test={"query": "python", "limit": 2},
+ ),
+ Action(
+ "read",
+ "Read a post and its top comments",
+ (Param("target", "post link or id"),),
+ (("opencli", "_opencli_read"),),
+ ),
+ Action(
+ "subreddit",
+ "Latest posts in a subreddit",
+ (
+ Param("name", "subreddit name without r/, e.g. python"),
+ Param("limit", "max posts", required=False, type=int, default=5),
+ ),
+ (("opencli", "_opencli_subreddit"),),
+ live_test={"name": "python", "limit": 2},
+ ),
+ )
+
def can_handle(self, url: str) -> bool:
from agent_reach.utils.url import host_matches
@@ -149,3 +195,23 @@ class RedditChannel(Channel):
'"modhash": null, "saved_at": 0, "last_verified_at": null}\n\n'
"Doctor 不会运行会自动读取浏览器并写文件的 `rdt status`。"
)
+
+ # ── unified entry backends ──
+
+ def _opencli_search(self, *, query: str, limit: int, timeout: float) -> list:
+ return _reddit(["search", query, "--limit", str(limit)], timeout)[:limit]
+
+ def _opencli_read(self, *, target: str, timeout: float) -> list:
+ t = str(target).strip()
+ if not (self.can_handle(t) or t.isalnum()):
+ raise ReachError(BAD_INPUT, "target must be a reddit.com post link or post id")
+ items = _reddit(["read", t, "--limit", "20"], timeout)
+ post, comments = items[0], items[1:]
+ if comments:
+ lines = [post["text"] or ""] + [f"\n@{c['author'] or ''}: {c['text'] or ''}" for c in comments]
+ post["text"] = "\n".join(lines).strip()
+ post["raw"] = {"post": post["raw"], "comments": [c["raw"] for c in comments]}
+ return [post]
+
+ def _opencli_subreddit(self, *, name: str, limit: int, timeout: float) -> list:
+ return _reddit(["subreddit", name.removeprefix("r/"), "--limit", str(limit)], timeout)[:limit]