Skip to content

fix(search): fall back to api.reddit.com when search.json is blocked - #28

Open
robertn702 wants to merge 1 commit into
jackwener:mainfrom
robertn702:fix/search-api-fallback
Open

robertn702 wants to merge 1 commit into
jackwener:mainfrom
robertn702:fix/search-api-fallback

Conversation

@robertn702

Copy link
Copy Markdown

Search is broken in 0.4.1. www.reddit.com/search.json and /r/<sub>/search.json answer 403 ("You've been blocked by network security") for every session, while listings still work through the same transport and cookies. api.reddit.com/search returns the same Listing JSON for the same cookies.

Closes #27.

What changed

  • rdt_cli/client.py retries search() against api.reddit.com when the .json path raises ForbiddenError. The retry drops the .json suffix and sends the same params, so subreddit search, sort and time filters are unchanged.
  • rdt_cli/constants.py adds API_FALLBACK_BASE_URL.
  • tests/test_client.py adds three tests. Two cover the fallback for global and subreddit search, one covers a non-403 error that should propagate without a retry.

Testing

uv run ruff check . passes, uv run python -m pytest -q gives 126 passed, 66 deselected.

Live check from this checkout with browser cookies. uv run rdt search "python asyncio" -n 3 and uv run rdt search "asyncio" -r python -s top -t year -n 2 both return listings.

rdt export calls search(), so it's fixed as well.

www.reddit.com/search.json and /r/<sub>/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 jackwener#27
@derekhsu

derekhsu commented Nov 6, 2026

Copy link
Copy Markdown

Independently verified on macOS — this fixes both search paths.

Environment: rdt-cli 0.4.1 (PyPI, uv tool), Python 3.14, macOS 14.8.4, browser-cookie auth (rdt status → authenticated: true, 8 cookies).

I applied the two source hunks from this PR to a clean 0.4.1 install and ran the real CLI:

$ rdt -v search "crouton alpha" -n 4
GET https://www.reddit.com/search.json?q=crouton+alpha&sort=relevance&t=all&limit=4&restrict_sr=off&raw_json=1 -> 403 Blocked
search.json blocked, retrying via https://api.reddit.com
GET https://api.reddit.com/search?q=crouton+alpha&... -> 200 OK

and the subreddit-scoped path:

$ rdt -v search "beam free" -r opencode -n 3
GET https://www.reddit.com/r/opencode/search.json?...&restrict_sr=on -> 403 Blocked
GET https://api.reddit.com/r/opencode/search?...&restrict_sr=on -> 200 OK

The fallback response is the same Listing JSON shape, so --json / --compact output parses unchanged and the rdt search → rdt read <id> chain works end to end. Catching only ForbiddenError is the right scope — 429/5xx still surface instead of being silently retried against a different host.

Two observations from the same investigation, outside this PR's scope:

0.4.1 is still the latest release on PyPI, so right now there is no installable version whose search works. Would be great to get this merged and shipped as 0.4.2.

caioniehues added a commit to caioniehues/rdt-cli that referenced this pull request Nov 28, 2026
Auth
- Find Reddit cookies in every profile of Firefox/LibreWolf (including the
  XDG ~/.config/mozilla/firefox location of Firefox 147+, snap, flatpak) and
  Chrome/Chromium/Brave/Edge/Vivaldi. Firefox DBs are read from a copy that
  includes the -wal file, so fresh logins are seen and the browser lock is
  avoided. Only logged-in sessions count (reddit_session, or a token_v2 JWT
  with sub=user); anonymous jars are skipped. Drops the uv subprocess path.
  Covers upstream jackwener#9, jackwener#16, jackwener#23.
- RDT_COOKIE / RDT_SESSION / RDT_MODHASH env auth; public commands no longer
  extract browser cookies on their own (upstream jackwener#17).
- rdt login reports the source and no longer claims "already authenticated"
  after silently extracting.

Upstream fixes
- search falls back to api.reddit.com when search.json is 403 (jackwener#27, jackwener#28).
- comment sends api_type/thing_id/text and raises on json.errors (jackwener#14).

Reading posts
- read/upvote/save/comment accept reddit.com and redd.it post URLs.
- read/show --markdown: post + full comment tree as Markdown; --article
  appends the linked page of link posts via the defuddle CLI.
- morechildren requests are batched at 100 IDs (was 414 URI Too Long); when
  Reddit blocks the API (403), --expand-more warns and keeps loaded comments.
- Error codes are no longer swallowed as Rich markup.

Tests
- Unit tests no longer write to the real ~/.config/rdt-cli.
- New tests for browser discovery, env auth, Markdown, URL parsing, batching.
ticoAg added a commit to ticoAg/rdt-cli that referenced this pull request Nov 28, 2026
# Conflicts:
#	rdt_cli/client.py
#	tests/test_client.py

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

search: www.reddit.com/search.json now returns 403 "Blocked"; api.reddit.com/search works as fallback

2 participants