-
-
Notifications
You must be signed in to change notification settings - Fork 421
Prevent KeyError when accessing content.value for next cursor #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactor 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 ClientclassDiagram
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
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
WalkthroughUpdated 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
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
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🔭 Outside diff range comments (2)
twikit/client/client.py (2)
1525-1531
: Likely same KeyError in _get_more_replies: mirror the cursor extraction fallback.When paginating more replies, the code still assumes
entries[-1]['content']['itemContent']['value']
. If the payload matches the shape you just fixed (content.value
), this will throw the same KeyError on subsequent pages.Recommend making it consistent and defensive:
- if entries[-1]['entryId'].startswith('cursor'): - next_cursor = entries[-1]['content']['itemContent']['value'] - _fetch_next_result = partial(self._get_more_replies, tweet_id, next_cursor) + if entries[-1]['entryId'].startswith('cursor'): + content = entries[-1].get('content', {}) + next_cursor = content.get('value') or content.get('itemContent', {}).get('value') + _fetch_next_result = ( + partial(self._get_more_replies, tweet_id, next_cursor) + if next_cursor is not None else None + )
1633-1641
: Refactor cursor extraction across all client methodsThe search script confirms that brittle
entry['…']['content']['value']
andentry['…']['itemContent']['value']
reads occur in dozens of places (both intwikit/client/client.py
andtwikit/guest/client.py
). We should centralize the fallback logic into a helper and replace every direct indexing with a safe extractor.Key locations (non-exhaustive):
- twikit/client/client.py – lines 1526, 1616, 755–761, 772–779, 830–834, 931–937, 1633–1637, 1723–1726, 1919–1922, 2004–2006, 2064–2066, 2284–2288, 2678–2680, 3524–3526, 3596–3598, 3627–3629, 3737–3739, 3951–3954, 3959–3962, 4022–4025, 4201–4204
- twikit/guest/client.py – lines 401–404
Suggested approach:
- Add a private utility in
client.py
(or a shared module), e.g.:def _extract_cursor(entry): content = entry.get('content', {}) # Twitter sometimes nests value under itemContent return content.get('value') or content.get('itemContent', {}).get('value')- Replace every
…['content']['value']
or…['item']['itemContent']['value']
with:cursor = _extract_cursor(entry_or_item)- Remove all direct indexing to avoid future KeyErrors.
This consolidation will both eliminate repeated diffs and ensure uniform, defensive cursor handling.
🧹 Nitpick comments (1)
twikit/client/client.py (1)
1568-1574
: Docstring example should use await.The method is async; the example should reflect that to prevent confusion.
- >>> tweet = client.get_tweet_by_id(target_tweet_id) + >>> tweet = await client.get_tweet_by_id(target_tweet_id)
@T3los Thank you for your work! There's a same pattern at twikit/twikit/client/client.py Line 1526 in 3b18105
May you also fix this? I have tested the change locally and it works well. |
Possible fix for a bug where client.get_tweet_by_id() throws a KeyError when the tweet has replies.
Summary by Sourcery
Bug Fixes:
Summary by CodeRabbit