Get all tweets containing a hashtag

Jul 21, 2011 at 12:46 PM
Edited Jul 21, 2011 at 12:46 PM

Hi guys,

Pretty simple question, and I can't find what I want to do.

I want to get all tweets containing an hashtag for a specified period of time. It will be used to log all of tweets for further analysis.

I try two methods :

1. With the Search type

 

//Get all tweets containing the 'hashtag'
List<Search> tweets = (from tweet in twitterContext.Search
                       where tweet.Type == SearchType.Search &&
                             tweet.Query == hashtag &&
                             tweet.Hashtag == hashtag.Remove(0, 1) &&
                             tweet.PageSize == 100 &&
                             tweet.Since == DateTime.Now.AddDays(-2) &&
                             tweet.Until == DateTime.Now
                       select tweet).ToList();

2. With Streaming
var streaming = (from stream in twitterContext.Streaming
		 where stream.Type == StreamingType.Filter &&
		       stream.Track == hashtag
		 select stream)
			.StreamingCallback(stream => LogStream(stream))
			.SingleOrDefault();

Unfortunately, I don't get any result with either request.
Here's what I'm using to create my context:

SingleUserAuthorizer singleUserAuthorizer = new SingleUserAuthorizer()
{
  Credentials = new InMemoryCredentials()
  {
      ConsumerKey = "...",
      ConsumerSecret = "...",
      OAuthToken = "...",
     AccessToken = "..."
}
}
using (var twitterContext = new TwitterContext(singleUserAuthorizer, "https://api.twitter.com/1/", "https://search.twitter.com/"))
{
   ...
}
I'm still not sure which methods is the best, so I need your advice.
Thanks for your time.

 



 

Coordinator
Jul 21, 2011 at 1:51 PM

Hi esylvestre,

On your search, you must use only the Date part, like this:

tweet.Since == DateTime.Now.AddDays(-2).Date &&
tweet.Until == DateTime.Now.Date



The Streaming entity still uses a username and password, which are assigned to the TwitterContext instance, like this:

            twitterCtx.StreamingUserName = "";
            twitterCtx.StreamingPassword = "";

There are also examples in the LINQ to Twitter source code in the LinqToTwitterDemos project.

@JoeMayo 

Jul 21, 2011 at 2:59 PM
JoeMayo wrote:

Hi esylvestre,

On your search, you must use only the Date part, like this:

tweet.Since == DateTime.Now.AddDays(-2).Date &&
tweet.Until == DateTime.Now.Date



The Streaming entity still uses a username and password, which are assigned to the TwitterContext instance, like this:

            twitterCtx.StreamingUserName = "";
            twitterCtx.StreamingPassword = "";

There are also examples in the LINQ to Twitter source code in the LinqToTwitterDemos project.

@JoeMayo 

Allright, thanks for the info, I'll look into the demos.

Last question, where can I find the StreamingUserName & Password ? Is it my Twitter account or I need to do something else ?

Thanks.

Coordinator
Jul 21, 2011 at 3:10 PM

Yes, you can use your Twitter username and password.

Joe

Oct 5, 2012 at 6:06 AM

Dear Joe,

Is it possible to search tweets for multiple hashtags?

I mean #ios,#pc,#microsoft,#bicycle

OR should i need to query separately and merge the results?

 

Thanks

Nirmal

Coordinator
Oct 6, 2012 at 2:28 AM

The best way to find out what you can get from twitter search is to try it here:

https://twitter.com/i/#!/search-home

And then reproduce the query with LINQ to Twitter. Their search engine isn't like Bing, Google, or Yahoo and they tend to tweak it over time; it's not always easy to predict whether a certain query will work.

Joe

Dec 21, 2012 at 2:54 AM

I cannot get this to work either.  When I search for my string on https://twitter.com/i/#!/search-home I get results, but using the below code, my collection has a count of zero....

 

 

                using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/"))
                {
                    List<Search> searchResponse =
                        (from tweet in twitterCtx.Search
                         where tweet.Type == SearchType.Search &&
                                tweet.Query == "windowsphone"
                         select tweet).ToList();

                    twitStatus = searchResponse.FirstOrDefault().Statuses[0].Text;
                }

 

Jul 19, 2013 at 6:36 AM
Edited Jul 19, 2013 at 6:45 AM
Hi there.

I am also facing the same issue.
My aim is to get the results based on the multiple hash keys using linqtotwitter.

below code is working fine with
string TwitterSearchKey= "google OR india";

linqtotwitter not giving returns result when i pass
TwitterSearchKey= "#google OR #india";

But when i search on twitter then it returns results for the above #keys
#google OR #india
using (var twitterCtx = new TwitterContext(auth))
            {
var items = from search in twitterCtx.Search
                            where search.Type == SearchType.Search &&
                            search.Query == TwitterSearchKey.Trim()
                            && search.Count == 2
                            from Status status in search.Statuses
                            select status;
                foreach (var t in items)
                {
                   //here code to display the tweets
                }
Thanks in advance
Pankaj Deharia
Coordinator
Jul 19, 2013 at 6:42 AM
Hi,

I have a couple suggestions for you. First, you can visit the Twitter Search page and test the query you want to use to see what you get back. Twitter isn't like a regular search engine, so you might want to check to see if there are some nuances in what you're searching for that affect the way Twitter response. Second, you need to pass constant values or variables to LINQ to Twitter query expressions. In your example, you should remove the expression and perform all the manipulation of TwitterSearchKey before assigning it to the Query property.

@JoeMayo