twikit icon indicating copy to clipboard operation
twikit copied to clipboard

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

Open retconned opened this issue 11 months ago • 3 comments

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())

retconned avatar Mar 20 '25 07:03 retconned

Same issue here.

YukiteruDev avatar Mar 20 '25 10:03 YukiteruDev

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

vl4dvl4d avatar Mar 26 '25 21:03 vl4dvl4d

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

Psychopumpum avatar Apr 04 '25 23:04 Psychopumpum