twikit icon indicating copy to clipboard operation
twikit copied to clipboard

Prevent KeyError when accessing content.value for next cursor

Open T3los opened this issue 5 months ago • 3 comments

Possible fix for a bug where client.get_tweet_by_id() throws a KeyError when the tweet has replies.

Summary by Sourcery

Bug Fixes:

  • Adjust reply_next_cursor lookup to use content.value instead of content.itemContent.value to avoid KeyError for tweets with replies

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where additional replies in long tweet threads sometimes failed to load.
    • Corrected pagination handling to ensure subsequent pages of replies load consistently.
    • Improves reliability when expanding and navigating through reply chains.
    • No changes to the public API or user-facing settings.

T3los avatar Aug 12 '25 15:08 T3los

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactor cursor extraction in get_tweet_by_id to use content.value instead of content.itemContent.value, preventing KeyError when handling tweets with replies.

Class diagram for updated get_tweet_by_id method in Client

classDiagram
    class Client {
        +async get_tweet_by_id(tweet_id)
        -_get_more_replies(tweet_id, reply_next_cursor)
    }
    Client --> "calls" _get_more_replies
    %% Highlight change in reply_next_cursor extraction
    class get_tweet_by_id {
        -reply_next_cursor = entries[-1]['content']['itemContent']['value']
        +reply_next_cursor = entries[-1]['content']['value']
    }

File-Level Changes

Change Details Files
Simplified reply cursor parsing to avoid missing key errors
  • Removed dependency on content.itemContent
  • Switched to directly reading content.value for cursor
twikit/client/client.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Aug 12 '25 15:08 sourcery-ai[bot]

Walkthrough

Updated get_tweet_by_id to extract the next-replies cursor from entries[-1]['content']['value'] instead of entries[-1]['content']['itemContent']['value']. The pagination flow and call to _get_more_replies remain unchanged. No public interfaces were modified.

Changes

Cohort / File(s) Summary of Changes
Replies pagination cursor fix
twikit/client/client.py
Adjusted cursor extraction in get_tweet_by_id to read from content.value rather than content.itemContent.value; pagination and control flow unchanged; no signature changes.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant TwitterAPI as Twitter API
  Client->>TwitterAPI: fetch tweet by ID
  TwitterAPI-->>Client: response with entries
  Client->>Client: parse entries[-1].content.value as cursor
  alt more replies available
    Client->>TwitterAPI: _get_more_replies(cursor)
    TwitterAPI-->>Client: additional replies
  end

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Poem

I twitch my nose at threads that sprawl,
Hop to the cursor, heed the call.
From content.value, paths align,
Replies now queue in tidy line.
Thump-thump—pagination’s right,
A rabbit’s fix by moonlit night. 🐇✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings
🧪 Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Aug 12 '25 15:08 coderabbitai[bot]

@T3los Thank you for your work!

There's a same pattern at https://github.com/d60/twikit/blob/3b18105ba0adb25ab78ba81d5df78825674f000d/twikit/client/client.py#L1526

May you also fix this? I have tested the change locally and it works well.

xiaoland avatar Aug 20 '25 06:08 xiaoland