Skip to content

Get_tweet_by_id("tweet_id") failing. KeyError: 'itemContent' reply_next_cursor = entries[-1]['content']['itemContent']['value'] #332

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

Open
retconned opened this issue Mar 20, 2025 · 3 comments

Comments

@retconned
Copy link

The library was working fine and suddenly it stopped working.
I attached the example code for a reproducible result,
all i do is get replies from a tweet, when testing i was adhering and going under to rate limits. the account is fine and not locked or suspended

Bug:

Main issue : await client.get_tweet_by_id("tweet_id")

Traceback (most recent call last):
  File "/Users/random/Dev/Projects/python/twt-py/./replies.py", line 315, in <module>
    asyncio.run(main())
  File "/Users/random/miniconda3/envs/twt-env/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/random/miniconda3/envs/twt-env/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/random/miniconda3/envs/twt-env/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/random/Dev/Projects/python/twt-py/./replies.py", line 311, in main
    await get_replies("1902389256498094446", websocket_url)
  File "/Users/random/Dev/Projects/python/twt-py/./replies.py", line 151, in get_replies
    tweet = await client.get_tweet_by_id("1902389256498094446")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/random/miniconda3/envs/twt-env/lib/python3.11/site-packages/twikit/client/client.py", line 1635, in get_tweet_by_id
   reply_next_cursor = entries[-1]['content']['itemContent']['value']
                        ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'itemContent'
Image

example code :

import asyncio
import os
from datetime import datetime
from dotenv import load_dotenv
from twikit import Client

load_dotenv()

client = Client("en-US")


async def get_replies(
    tweet_id: str,
):
    """
    Fetch all replies for a given tweet ID with rate limiting

    Args:
        tweet_id: The Twitter tweet ID to fetch replies for
    """

    # Fetch the tweet
    tweet = await client.get_tweet_by_id(tweet_id)
    total_replies = tweet.reply_count
    print(f"Total replies to fetch: {total_replies}")

    all_replies = []
    current_replies = tweet.replies

    while len(all_replies) < total_replies:
        replies_in_batch = 0
        for reply in current_replies:
            scraped_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

            reply_data = {
                "parent_id": tweet_id,
                "reply_id": reply.id,
                "username": reply.user.screen_name,
                "text": reply.text,
                "created_at": reply.created_at,
                "scraped_at": scraped_at,
            }

            all_replies.append(reply_data)
            replies_in_batch += 1

            print(all_replies)

            if len(all_replies) >= total_replies:
                break

        if not hasattr(current_replies, "next") or len(all_replies) >= total_replies:
            print("Reached the end of available replies or rate limit.")
            break

        try:
            current_replies = await current_replies.next()

        except Exception as e:
            print(f"Error fetching next batch: {e}")
            break

    print(f"Completed fetching {len(all_replies)} replies")


async def main():
    username = os.getenv("TWITTER_USERNAME", "")
    email = os.getenv("TWITTER_EMAIL", "")
    password = os.getenv("TWITTER_PASSWORD", "")
    cookies_file = os.getenv("TWITTER_COOKIES_FILE", "cookies.json")

    await client.login(
        auth_info_1=username,
        auth_info_2=email,
        password=password,
        cookies_file=cookies_file,
    )

    client.save_cookies(cookies_file)

    await get_replies("1902389676964479023")


if __name__ == "__main__":
    asyncio.run(main())
@YukiteruDev
Copy link

Same issue here.

@vl4dvl4d
Copy link

Same! Stuck on await client.get_tweet_by_id(tweet.id)
Response: 'itemContent'

@d60 please check it, one of the most used functions disabled now

@Psychopumpum
Copy link

Remove the itemContent key on client.py line: 1635
reply_next_cursor = entries[-1]['content']['value']

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

No branches or pull requests

5 participants
@YukiteruDev @retconned @Psychopumpum @vl4dvl4d and others