fa0311 / twitter-openapi-typescript

Implementation of Twitter internal API (Twitter graphql API) in TypeScript
https://www.npmjs.com/package/twitter-openapi-typescript
Other
73 stars 11 forks source link

Count does not limit number of tweet results #58

Closed simon-marcus closed 12 months ago

simon-marcus commented 1 year ago

I've noticed that setting the value of count in getUserTweets does not appear to limit the number of results: it returns 100 results by default. This would be really useful to make smaller requests.

 const response = await client.getTweetApi().getUserTweets({userId: '44196397', count: 4});

I've reproduced the code here: https://replit.com/@simonasaservice/TweetDemo#index.js

import { TwitterOpenApi } from 'twitter-openapi-typescript';
const api = new TwitterOpenApi();

async function getTweets(userId) {
  const client = await api.getClient();
  const count = 5;
  const params = {
    userId: '44196397',
    count: count
  }
  console.log(`Should return ${count} results.`)
  const response = await client.getTweetApi().getUserTweets(params);

  const result = response.data.data;
  if (result) {
    console.log(`Response contains ${result.length} results.`)
    // console.log(result)
    return result
  }
  return null
}

getTweets();

Please let me know if there's anything else I could test, and thanks for this outstanding library! 🙏

fa0311 commented 1 year ago

Thank you for sending us your Issues. The count parameter exists in the official Twitter client, but seems to be ignored on the server side.

If logged in, it always returns 25 responses and if not logged in, it always seems to return 100 responses.

simon-marcus commented 1 year ago

Thank you for the fast reply! After additional testing, I think that the public API for getUserTweets() returns a selection of the most popular tweets for some (maybe blue verified?) users—not the most recent tweets. It won't return any results at all for other users. I have updated my code with some examples if you want to see.

To get reliable results, I will need to to use a logged-in version of the API. Is there a version that you have been working on? I see you mention the Logged in branch, but I am not clear on how I should use this. I would be grateful for your suggestion!

fa0311 commented 1 year ago

getUserTweets({ userId }) is the same as the tweets you get when you visit https://twitter.com/<screenName>

The csrf token and auth token are required to use the login version of the API.

const client = await api.getClientFromCookies('<csrf toke>', '<auth token>');
await client.getUserApi().getUserTweets({ userId });

You need the cookie that is given to you after you log in with your browser. ct0 is the csrf token and auth_token is the auth token.

An external program is needed to automate this process. As far as I know, this is all there is, except for implementations using WebDriver and older implementations.

Recommend (Python) https://github.com/tsukumijima/tweepy-authlib My implementation (Python) https://github.com/fa0311/TwitterFrontendFlow