Skip to content

feat: add short-index support for 'xhs read' - #16

Closed
Pleasurecruise wants to merge 2 commits into
jackwener:mainfrom
Pleasurecruise:main
Closed

feat: add short-index support for 'xhs read'#16
Pleasurecruise wants to merge 2 commits into
jackwener:mainfrom
Pleasurecruise:main

Conversation

@Pleasurecruise

Copy link
Copy Markdown
Contributor

Introduce short-index navigation so users can open listed notes with xhs read <N>. Persist an ordered index from listing commands (search/feed/hot/user-posts) and resolve numeric indices in the read command to note_id + xsec_token. Key changes:

  • Add INDEX_CACHE_FILE constant and .idea to .gitignore.
  • Implement save_note_index() and get_note_by_index() in xhs_cli/cookies.py (reads/writes index cache, enforces 0o600 perms).
  • Add _save_index_from_items() and wire it into search, feed, hot, and user-posts in xhs_cli/commands/reading.py; allow read to accept numeric indices and raise a usage error when missing.
  • Add comprehensive tests for CLI index behavior and cookies index handling (tests/test_cli.py, tests/test_cookies.py).

This change improves interactive ergonomics by avoiding manual copying of long note IDs when opening results from recent listings.

Introduce short-index navigation so users can open listed notes with `xhs read <N>`. Persist an ordered index from listing commands (search/feed/hot/user-posts) and resolve numeric indices in the `read` command to note_id + xsec_token. Key changes:

- Add INDEX_CACHE_FILE constant and .idea to .gitignore.
- Implement save_note_index() and get_note_by_index() in xhs_cli/cookies.py (reads/writes index cache, enforces 0o600 perms).
- Add _save_index_from_items() and wire it into search, feed, hot, and user-posts in xhs_cli/commands/reading.py; allow `read` to accept numeric indices and raise a usage error when missing.
- Add comprehensive tests for CLI index behavior and cookies index handling (tests/test_cli.py, tests/test_cookies.py).
- Update docs (README.md and SKILL.md) to document short-index workflow and bump package version to 0.6.0.
- Update uv.lock to reflect the version bump and add the xhshow dependency.

This change improves interactive ergonomics by avoiding manual copying of long note IDs when opening results from recent listings.
Copilot AI review requested due to automatic review settings March 12, 2026 22:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds “short-index” navigation so users can open recently listed notes via xhs read <N> by persisting the last listing’s ordered note index to disk and resolving numeric args in read.

Changes:

  • Persist a ~/.xiaohongshu-cli/index_cache.json note index from listing commands and enforce 0600 permissions.
  • Extend xhs read to accept numeric indices and resolve them to {note_id, xsec_token}.
  • Add unit tests covering index persistence and CLI index resolution; update docs to describe the workflow.

Reviewed changes

Copilot reviewed 7 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
xhs_cli/cookies.py Adds index cache path + save/load helpers for short-index lookups.
xhs_cli/constants.py Introduces INDEX_CACHE_FILE constant.
xhs_cli/commands/reading.py Saves index during listing commands and resolves numeric read arguments.
tests/test_cookies.py Adds tests for index cache persistence, permissions, and lookup behavior.
tests/test_cli.py Adds CLI tests for reading by short index and index extraction from results.
README.md Documents short-index navigation usage and cache location.
SKILL.md Updates command docs/examples to include short-index read workflow.
.gitignore Ignores .idea.
uv.lock Bumps project version and updates locked dependencies.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread xhs_cli/cookies.py Outdated
Comment thread tests/test_cli.py Outdated
Harden get_note_by_index to ensure the cookies file parses as a list and that each entry is a dict containing both note_id and xsec_token, returning None for malformed data to avoid crashes. Update the read-by-index test to replace the simple run_client_action stub with a fake that captures the note_id and xsec_token passed to the client, and assert those values are forwarded correctly along with the existing YAML output checks.
@jackwener

Copy link
Copy Markdown
Owner

Thanks for the idea and the original implementation here — the short-index workflow is genuinely useful, and I pulled that idea forward into a replacement PR: #17.

I am closing this PR in favor of #17 because that branch is rebased on current main and keeps the newer note-context hardening intact:

  • it preserves xsec_source alongside xsec_token for short-index reads/comments
  • it clears the index cache on empty list results, so old indices are not reused accidentally
  • it applies cleanly on top of the current codebase without the conflicts in this branch

I also added a Co-authored-by trailer so the implementation still credits your contribution. Please take a look at #17.

@jackwener jackwener closed this Mar 13, 2026
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.

3 participants