Farrukh Ahmed
Farrukh Ahmed

Reputation: 493

Retrieving all tweets of a particular hashtag using TweetSharp

I am using tweetsharp library to retrieve tweets of a particular hashtag

 var service = new TwitterService(Consumer Key, Consumer Secret);
 service.AuthenticateWith("Access Token", "AccessTokenSecret");
 TwitterSearchResult tweets = service.Search(new SearchOptions { Q = "#Pride",Lang="en"});
 IEnumerable<TwitterStatus> status = tweets.Statuses;
    foreach (var item in status)
            {
                Console.WriteLine(item.User.ScreenName + " " + "Says:" + "\n" + item.Text+"\n"+ "ReTweeted:"+" "+item.RetweetCount);
            }

I am able to retrieve tweets from above code snippet but it only returns 100 tweets, seems like twitter restrict it to 100 tweets. Can anybody here tell me how to retrieve all the tweets of a particular hashtag.

Upvotes: 2

Views: 1504

Answers (1)

Here is the code..

     string maxid = "1000000000000"; // dummy value
    int tweetcount = 0;


    if (maxid != null)
    {
        var tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count) });
        List<TwitterStatus> resultList = new List<TwitterStatus>(tweets_search.Statuses);
        maxid = resultList.Last().IdStr;
        foreach (var tweet in tweets_search.Statuses)
        {
            try
            {
                ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
                tweetcount++;
            }
            catch { }
        }

        while (maxid != null && tweetcount < Convert.ToInt32(count))
        {
            maxid = resultList.Last().IdStr;
            tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count), MaxId = Convert.ToInt64(maxid) });
            resultList = new List<TwitterStatus>(tweets_search.Statuses);
            foreach (var tweet in tweets_search.Statuses)
            {
                try
                {
                    ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
                    tweetcount++;
                }
                catch { }
            }
        }

    }

Upvotes: 1

Related Questions