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

Search tweets not working properly #24

Open
bio1oo1 opened this issue Dec 20, 2024 · 3 comments
Open

Search tweets not working properly #24

bio1oo1 opened this issue Dec 20, 2024 · 3 comments

Comments

@bio1oo1
Copy link

bio1oo1 commented Dec 20, 2024

after successful scraper login, I am trying to search tweets, but it's just returning empty object
here is my basic code

await this.scraper.login(
          process.env.TWITTER_USERNAME!,
          process.env.TWITTER_PASSWORD!,
          process.env.TWITTER_EMAIL!,
          process.env.TWITTER_2FA_SECRET!,
          process.env.TWITTER_API_KEY!,
          process.env.TWITTER_API_SECRET_KEY!,
          process.env.TWITTER_ACCESS_TOKEN!,
          process.env.TWITTER_ACCESS_TOKEN_SECRET!
        );
const mentions = await this.scraper.searchTweets(
          '#nodejs', 20, SearchMode.Latest
        );
console.log("mentions", mentions);

In my console, it's just mentions Object [AsyncGenerator] {}

@abcfy2
Copy link
Contributor

abcfy2 commented Dec 20, 2024

export async function* getTweetTimeline(
query: string,
maxTweets: number,
fetchFunc: FetchTweets,
): AsyncGenerator<Tweet, void> {
let nTweets = 0;
let cursor: string | undefined = undefined;
while (nTweets < maxTweets) {
const batch: FetchTweetsResponse = await fetchFunc(
query,
maxTweets,
cursor,
);
const { tweets, next } = batch;
if (tweets.length === 0) {
break;
}
for (const tweet of tweets) {
if (nTweets < maxTweets) {
cursor = next;
yield tweet;
} else {
break;
}
nTweets++;
}
}
}

Because this function returns a Generator, so you have to iterate over it to get the result.

For example:

for (const mention of mentions) {
  console.log(mention);
};

More details can be found at Generator and function*

@bio1oo1
Copy link
Author

bio1oo1 commented Dec 20, 2024

@abcfy2
I tried that as well, and it's returning an error saying TypeError: mentions is not iterable

@abcfy2
Copy link
Contributor

abcfy2 commented Dec 20, 2024

@abcfy2 I tried that as well, and it's returning an error saying TypeError: mentions is not iterable

Oh sorry, it's an AsyncGenerator, so you have to use await in your for loop iterator:

// remove await because 'await' has no effect on the type of this expression
const mentions = this.scraper.searchTweets(
          '#nodejs', 20, SearchMode.Latest
        );

for await (const mention of mentions) {
  console.log(mention);
};

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

2 participants