From 1735fa6b3ce8af9c97d8f170a3b0ba0c2cb8a5ad Mon Sep 17 00:00:00 2001 From: robertn702 <8119609+robertn702@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:39:06 -0500 Subject: [PATCH] fix(search): fall back to api.reddit.com when search.json is blocked www.reddit.com/search.json and /r//search.json answer 403 for every session, so search and export fail with code: forbidden on 0.4.1. api.reddit.com serves the same Listing JSON for the same cookies. Closes #27 --- rdt_cli/client.py | 8 +++++++- rdt_cli/constants.py | 3 +++ tests/test_client.py | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/rdt_cli/client.py b/rdt_cli/client.py index 7762976..a3569a9 100644 --- a/rdt_cli/client.py +++ b/rdt_cli/client.py @@ -8,6 +8,7 @@ from .config import DEFAULT_CONFIG, RuntimeConfig from .constants import ( ALL_URL, + API_FALLBACK_BASE_URL, COMMENT_URL, DEFAULT_LIMIT, HOME_URL, @@ -30,6 +31,7 @@ VOTE_URL, ) from .exceptions import ( + ForbiddenError, RedditApiError, ) from .fingerprint import BrowserFingerprint @@ -262,7 +264,11 @@ def search( } if after: params["after"] = after - return self._get(url, params=params) + try: + return self._get(url, params=params) + except ForbiddenError: + logger.info("search.json blocked, retrying via %s", API_FALLBACK_BASE_URL) + return self._get(API_FALLBACK_BASE_URL + url.removesuffix(".json"), params=params) # ── User ──────────────────────────────────────────────────────── diff --git a/rdt_cli/constants.py b/rdt_cli/constants.py index 055064b..f74c2ff 100644 --- a/rdt_cli/constants.py +++ b/rdt_cli/constants.py @@ -32,6 +32,9 @@ # Search SEARCH_URL = "/search.json" SUBREDDIT_SEARCH_URL = "/r/{subreddit}/search.json" +# Reddit answers 403 for www.reddit.com/search.json; the api. host still serves +# the same Listing JSON for the same cookies (jackwener/rdt-cli#27). +API_FALLBACK_BASE_URL = "https://api.reddit.com" # User USER_ABOUT_URL = "/user/{username}/about.json" diff --git a/tests/test_client.py b/tests/test_client.py index d6bbf71..f533daf 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -2,9 +2,11 @@ from unittest.mock import patch +import pytest + from rdt_cli.auth import Credential from rdt_cli.client import RedditClient -from rdt_cli.exceptions import RedditApiError +from rdt_cli.exceptions import ForbiddenError, RedditApiError def test_validate_session_success_updates_username_and_capabilities() -> None: @@ -67,3 +69,40 @@ def test_get_more_comments_uses_api_morechildren() -> None: "raw_json": 1, }, ) + + +def test_search_falls_back_to_api_host_when_search_json_is_forbidden() -> None: + cred = Credential(cookies={"reddit_session": "abc"}) + with RedditClient(cred) as client: + with patch.object(client, "_get", side_effect=[ForbiddenError(), {"ok": True}]) as mock_get: + data = client.search("test", limit=5) + + assert data == {"ok": True} + assert mock_get.call_count == 2 + first, second = mock_get.call_args_list + assert first.args[0] == "/search.json" + assert second.args[0] == "https://api.reddit.com/search" + assert first.kwargs["params"] == second.kwargs["params"] + + +def test_subreddit_search_falls_back_to_api_host_when_search_json_is_forbidden() -> None: + cred = Credential(cookies={"reddit_session": "abc"}) + with RedditClient(cred) as client: + with patch.object(client, "_get", side_effect=[ForbiddenError(), {"ok": True}]) as mock_get: + data = client.search("asyncio", subreddit="python", limit=3) + + assert data == {"ok": True} + first, second = mock_get.call_args_list + assert first.args[0] == "/r/python/search.json" + assert second.args[0] == "https://api.reddit.com/r/python/search" + assert second.kwargs["params"]["restrict_sr"] == "on" + + +def test_search_does_not_fall_back_on_other_errors() -> None: + cred = Credential(cookies={"reddit_session": "abc"}) + with RedditClient(cred) as client: + with patch.object(client, "_get", side_effect=RedditApiError("boom")) as mock_get: + with pytest.raises(RedditApiError): + client.search("test") + + assert mock_get.call_count == 1