Get all tweets with specific hashtag
Asked Answered
S

5

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

Stefanistefania answered 13/3, 2011 at 16:17 Comment(0)
D
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.

Dentistry answered 13/3, 2011 at 17:1 Comment(3)
Too bad the service (search.twitter.com) seems to be down frequently :( Is there any alternative using api.twitter.comStefanistefania
IT'S NO LONGER WORKING ...DON'T WASTE YOUR TIME HEREWhelk
This is using the old API (v1). For the new one (v1.1) go to: dev.twitter.com/rest/reference/get/search/tweets. The rest still applies. In this case the URL you'd use would be: api.twitter.com/1.1/search/tweets.json?q=%23test however you now need to authenticate the request with oAuth.Eyelid
O
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);
Obcordate answered 8/9, 2012 at 17:4 Comment(0)
G
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;
Gyn answered 20/6, 2013 at 6:48 Comment(0)
T
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

Transeunt answered 25/5, 2015 at 12:7 Comment(0)
V
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 { }
                }

}

Viola answered 11/7, 2015 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.