Search code examples
c#twitterlinq-to-twitter

Retrieve all the tweets of a specific hashtag


I am using linq2twitter library to retrieve tweets of a specific hashtag, and I am able to achieve that but the problem is that it is only gives 100 tweets.

   string consumerKey = "MyConsumerKey";
        string consumerSecret = "MyConsumerSecret";
        string accessToken = "MyAcessToken";
        string accessTokenSecret = "MyAccessToken";
        string Query = "#HashTag";
        var auth = new SingleUserAuthorizer
        {
            CredentialStore = new InMemoryCredentialStore
            {
                ConsumerKey = consumerKey,
                ConsumerSecret = consumerSecret,
                OAuthToken = accessToken,
                OAuthTokenSecret = accessTokenSecret
            }
        };

        var context = new TwitterContext(auth);
        int count = 0;
        var searchResults =
                         (from search in context.Search
                          where search.Type == SearchType.Search &&
                                search.Query == Query &&
                                search.IncludeEntities == true 
                          select search).SingleOrDefault();
        foreach (var item in searchResults.Statuses)
        {

            count++;
        }
        Console.WriteLine(count);

Is there any way to achieve all the tweets? Or am I doing something wrong?


Solution

  • Check out the Twitter API that Linq2Twitter wraps:

    Count

    The number of tweets to return per page, up to a maximum of 100. Defaults to 15. This was formerly the “rpp” parameter in the old Search API.

    Meaning that each call to the search API can return at most 100 results. If you want to return more, you'll have to manually page your requests, probably using the MaxID parameter (called max_id in the twitter API):

    max_id

    Returns results with an ID less than (that is, older than) or equal to the specified ID

    So take the dataset of 100 results you have, grab the lowest ID, and pass it as the MaxId of the next call.