6

I've been experimenting with the Twitter API because I want to display a few lists of tweets on a special page.

Among those lists is a list with all tweets containing a specific hashtag (e.g. #test)

However I cannot find how to get that list in either XML or JSON (preferably the latter), does anyone know how? It is also fine if it can be done in TweetSharp

dtech
  • 13,741
  • 11
  • 48
  • 73

5 Answers5

9

You can simply fetch http://search.twitter.com/search.json?q=%23test to get a list of tweets containing #test in JSON, where %23test is #test URL encoded.

I'm not familiar with TweetSharp, but I guess there must be a search command that you can use to search for #test, and then transform the resulting tweets into JSON yourself.

gnab
  • 9,251
  • 1
  • 21
  • 13
  • Too bad the service (search.twitter.com) seems to be down frequently :( Is there any alternative using api.twitter.com – dtech Mar 13 '11 at 20:31
  • 2
    **IT'S NO LONGER WORKING ...DON'T WASTE YOUR TIME HERE** – kumar kundan Jul 02 '15 at 06:03
  • 1
    This is using the old API (v1). For the new one (v1.1) go to: https://dev.twitter.com/rest/reference/get/search/tweets. The rest still applies. In this case the URL you'd use would be: https://api.twitter.com/1.1/search/tweets.json?q=%23test however you now need to authenticate the request with oAuth. – hammygoonan Jun 02 '16 at 02:16
8

First install TweetSharp using github https://github.com/danielcrenna/tweetsharp

Here is the code to do a search

TwitterService service = new TwitterService();
var tweets = service.Search("#Test", 100);
List<TwitterSearchStatus> resultList = new List<TwitterSearchStatus>(tweets.Statuses);    

If you have more then one page results you can setup a loop and call each page

 service.Search("#Test", i += 1, 100);
4

It seems like there is a change in the API since last few months. Here is the updated code:

TwitterSearchResult res = twitter.Search(new SearchOptions { Q = "xbox" });
IEnumerable<TwitterStatus> status = res.Statuses;
Prashant
  • 966
  • 9
  • 26
0

u access with this url for your tweet searchs. But u have to use OAuth protocols.

https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi

Emre Erol
  • 470
  • 1
  • 7
  • 25
0

I struggled with the same problem. Here is my vague solution . Enjoy Programming. It will get out of the function whenever your required number of tweets are acquired/fetched.

        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 { }
                }

}

taimoor1990
  • 158
  • 1
  • 13