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

How can I prioritize fetching the most recent tweets first #244

Open
yuhai5237 opened this issue Oct 24, 2024 · 3 comments
Open

How can I prioritize fetching the most recent tweets first #244

yuhai5237 opened this issue Oct 24, 2024 · 3 comments

Comments

@yuhai5237
Copy link

I am using user_tweets = await client.get_user_tweets('XXXXX') to retrieve tweet information, but it returns older tweets first. I would like to prioritize returning the most recently created tweets. How can I achieve this?

@Davis-3450
Copy link

You can't, just fetch certain amount and manually check them.

@szsyzx
Copy link

szsyzx commented Nov 1, 2024

@yuhai5237 Not Found
Sorry, the page or file you tried to access was not found.

@github-staff github-staff deleted a comment from yuhai5237 Nov 1, 2024
@iukea1
Copy link

iukea1 commented Jan 2, 2025

I am using user_tweets = await client.get_user_tweets('XXXXX') to retrieve tweet information, but it returns older tweets first. I would like to prioritize returning the most recently created tweets. How can I achieve this?

Maybe this will help (I'm shooting in the dark a little bit here)

import asyncio
from typing import Optional
from twikit import Client, Tweet

AUTH_INFO_1 = '...'
AUTH_INFO_2 = '...'
PASSWORD = '...'

# Initialize the client with authentication
client = Client(auth1=AUTH_INFO_1, auth2=AUTH_INFO_2, password=PASSWORD)

USER_ID = '44196397'
CHECK_INTERVAL = 60 * 5  # 5 minutes in seconds


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


async def get_latest_tweet() -> Optional[Tweet]:
    try:
        tweets = await client.get_user_tweets(USER_ID, 'Replies')
        return tweets[0] if tweets else None
    except Exception as e:
        print(f"Error fetching tweets: {e}")
        return None


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

    while True:
        await asyncio.sleep(CHECK_INTERVAL)
        latest_tweet = await get_latest_tweet()

        if (
            latest_tweet
            and before_tweet
            and before_tweet != latest_tweet
            and before_tweet.created_at_datetime < latest_tweet.created_at_datetime
        ):
            callback(latest_tweet)

        if latest_tweet:
            before_tweet = latest_tweet


if __name__ == "__main__":
    asyncio.run(main())

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

4 participants