Search code examples
twitterlinq-to-twitter

TwitterContext Limitations outside of LinqToTwitter API calls


Follow up to Extract BearerToken from LinqToTwitter IAuthorizer

Although I am not using the LTT library for anything past authorization (at this point), I still seem to be limited to 200 tweets when calling directly to the /statuses/user_timeline API with

{parameter string: user_id={0}&screen_name={1}&count=3200&exclude_replies=true&include_rts=false&trim_user=false&contributor_details=false}

And

webClient.Headers.Add(String.Format("Authorization:  Bearer {0}", BearerToken));

Is this a limit of TwitterContext Authorization? If so how can I change that limit without using the library calls?

i.e. I am not using

statusResponse = (from tweet in twitterCtx.Status ...)

I don't use the library because it is utilizing the Twitter search object which may produce inconsistent results per Twitter's limitations on the search object.

Thank you in advance!


Solution

  • According to Twitter's statuses/user_timeline documentation, the max value of count is 200. However, that's a per query max. You can make multiple queries to retrieve up to 3200 tweets. Twitter has a nice explanation, in their Working with Timelines page, on how to work with timelines to retrieve those 3200 tweets. I realize you aren't querying with LINQ to Twitter, but for the benefit of anyone else who finds this answer, this how LINQ to Twitter does it:

    static async Task RunUserTimelineQueryAsync(TwitterContext twitterCtx)
    {
        //List<Status> tweets =
        //    await
        //    (from tweet in twitterCtx.Status
        //     where tweet.Type == StatusType.User &&
        //           tweet.ScreenName == "JoeMayo"
        //     select tweet)
        //    .ToListAsync();
    
        const int MaxTweetsToReturn = 200;
        const int MaxTotalResults = 100;
    
        // oldest id you already have for this search term
        ulong sinceID = 1;
    
        // used after the first query to track current session
        ulong maxID;
    
        var combinedSearchResults = new List<Status>();
    
        List<Status> tweets =
            await
            (from tweet in twitterCtx.Status
             where tweet.Type == StatusType.User &&
                   tweet.ScreenName == "JoeMayo" &&
                   tweet.Count == MaxTweetsToReturn &&
                   tweet.SinceID == sinceID &&
                   tweet.TweetMode == TweetMode.Extended
             select tweet)
            .ToListAsync();
    
        if (tweets != null)
        {
            combinedSearchResults.AddRange(tweets);
            ulong previousMaxID = ulong.MaxValue;
            do
            {
                // one less than the newest id you've just queried
                maxID = tweets.Min(status => status.StatusID) - 1;
    
                Debug.Assert(maxID < previousMaxID);
                previousMaxID = maxID;
    
                tweets =
                    await
                    (from tweet in twitterCtx.Status
                     where tweet.Type == StatusType.User &&
                           tweet.ScreenName == "JoeMayo" &&
                           tweet.Count == MaxTweetsToReturn &&
                           tweet.MaxID == maxID &&
                           tweet.SinceID == sinceID &&
                           tweet.TweetMode == TweetMode.Extended
                     select tweet)
                    .ToListAsync();
    
                combinedSearchResults.AddRange(tweets);
    
            } while (tweets.Any() && combinedSearchResults.Count < MaxTotalResults);
    
            PrintTweetsResults(tweets);
        }
        else
        {
            Console.WriteLine("No entries found.");
        }
    }
    

    which you can find in the LINQ to Twitter documentation on Querying the User Timeline. I also wrote a blog post, Working with Timelines with LINQ to Twitter, to explain LINQ to Twitter's approach to paging. It's for an earlier (non-async) version, but the concepts are still the same.