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

Cannot understand basic pagination w/ search or get it working #525

Open
santekotturi opened this issue Apr 12, 2024 · 1 comment
Open

Comments

@santekotturi
Copy link

santekotturi commented Apr 12, 2024

I've read the pagination docs 100x today and spent the past 4 hours trying to get basic pagination to work to no avail.

I have a tweet that I want to fetch all of the replies to.

This query works great returning the first 10 results but I cannot get anything more than 10 tweets from this paginator

   const searchPaginator = await twitterClient.v2.search({
      query: `conversation_id:${conversation_id}`,
      expansions: ['attachments.media_keys', 'author_id'],
      'tweet.fields': [
        'in_reply_to_user_id',
        'author_id',
        'created_at',
        'conversation_id',
        'public_metrics',
        'referenced_tweets'
      ],
      'media.fields': ['url', 'preview_image_url', 'type', 'height', 'width'],
      'user.fields': ['username', 'name', 'profile_image_url'],
      sort_order: 'relevancy',
    });

    let allReplies: any[] = [];
    let allUsers: any[] = [];
    let allMedia: any[] = [];


    let iter = 0
    for await (const tweet of searchPaginator) {
      logger.log('Iterating over tweets', iter, tweet);
      const availableMeta = searchPaginator.meta;
      const availableIncludes = searchPaginator.includes;

      logger.log('Available meta', availableMeta);
      logger.log('Available includes', availableIncludes);

      allReplies = [...allReplies, tweet];
      allUsers = [...allUsers, ...availableIncludes.users];
      allMedia = [...allMedia, ...availableIncludes.media];
  
    }

From the docs, I cant connect the concept of await homeTimeline.fetchLast(1000); to how that works in conjunction with

    const mySearch = await client.v2.search('nodeJS');

for await (const tweet of mySearch) {
  const availableMeta = mySearch.meta;
  const availableIncludes = mySearch.includes;

  // availableMeta and availableIncludes are filled with .meta and .includes
  // fetched at the time you were reading this tweet
  // Once the next page is automatically fetched, they can be updated!
}

In the second example (which I need in order to be pulling the includes users/media), there's no example of next(), fetchNext() or fetchlast() would be called.

Any examples or pseudocode would be greatly appreciated 🙏

@santekotturi
Copy link
Author

// I have tried chatGPT, gemini and Claude, none of them can figure out a working implementation of the code...

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

1 participant