Skip to content
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

example get user tweets not working #282

Open
moeinrahimi1 opened this issue Jan 8, 2025 · 2 comments
Open

example get user tweets not working #282

moeinrahimi1 opened this issue Jan 8, 2025 · 2 comments

Comments

@moeinrahimi1
Copy link

import asyncio
from typing import NoReturn

from twikit import Client, Tweet

client = Client()

USER_ID = '4397016993'
CHECK_INTERVAL = 60 * 5


def callback(tweet: Tweet) -> None:
    print(f'New tweet posted : {tweet.text}')


async def get_latest_tweet() -> Tweet:
    return await client.get_user_tweets(USER_ID, 'Replies')[0]


async def main() -> NoReturn:
    before_tweet = await get_latest_tweet()

    while True:
        await asyncio.sleep(CHECK_INTERVAL)
        latest_tweet = await get_latest_tweet()
        if (
            before_tweet != latest_tweet and
            before_tweet.created_at_datetime < latest_tweet.created_at_datetime
        ):
            callable(latest_tweet)
        before_tweet = latest_tweet

asyncio.run(main())


RuntimeWarning: coroutine 'Client.get_user_tweets' was never awaited
  return await client.get_user_tweets(USER_ID, 'Replies')[0]
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "E:\Fanni\twitter\app.py", line 37, in <module>
    asyncio.run(main())
  File "C:\Users\Fani\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\Fani\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fani\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 684, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "E:\Fanni\twitter\app.py", line 25, in main
    before_tweet = await get_latest_tweet()
                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\Fanni\twitter\app.py", line 21, in get_latest_tweet
    return await client.get_user_tweets(USER_ID, 'Replies')[0]
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'coroutine' object is not subscriptable
@Pygot
Copy link

Pygot commented Jan 8, 2025

Hi @moeinrahimi1 ! Your code doesn't make much sense. Tried to code it for you, well... I wasn't able to get the reply text, instead I was able to get the tweet that the user is replying to. I never used this library so I had a bit of struggle. Hope this code helps at least a little bit!:

main.txt

@mahrtayyab
Copy link

mahrtayyab commented Jan 8, 2025

await client.get_user_tweets(USER_ID, 'Replies') will return coroutine, which obviously isn't subscriptable, try

async def get_latest_tweet() -> Tweet:
    return (await client.get_user_tweets(USER_ID, 'Replies'))[0]

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

3 participants