Wanting to Query the twitter stream for ..

Oct 3, 2011 at 2:43 AM

Can someone suggest what the Linq2Twitter might be to get a list of tweets as follows:

mentions @sv_code_camp or includes #svcc

That happened in the past "x" hours.

thanks

Coordinator
Oct 3, 2011 at 3:15 AM

Hi Peter,

Here's a couple ways to do it.  This should give you an idea of how to tweak params to get the result you want:

using System;
using System.Linq;
using LinqToTwitter;

namespace SvccSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            var ctx = new TwitterContext();

            Console.WriteLine("With just query: \n");
            var search =
                (from tweet in ctx.Search
                 where tweet.Type == SearchType.Search &&
                       tweet.Query == "@sv_code_camp OR #svcc" &&
                       tweet.Since == DateTime.Now.AddDays(-5).Date
                 select tweet)
                .SingleOrDefault();

            search.Entries.ForEach(tweet => Console.WriteLine("Tweet: {0}\n", tweet.Content));

            Console.WriteLine("\nWith advanced options: \n");

            var searchAdv =
                (from tweet in ctx.Search
                 where tweet.Type == SearchType.Search &&
                       tweet.WordOr == "#svcc @sv_code_camp" &&
                       tweet.Since == DateTime.Now.AddDays(-5).Date
                 select tweet)
                .SingleOrDefault();

            searchAdv.Entries.ForEach(tweet => Console.WriteLine("Tweet: {0}\n", tweet.Content));

            Console.ReadKey();
        }
    }
}

@JoeMayo

 

Oct 3, 2011 at 7:21 PM

that worked great!  Is there a direct way to get the author's of the tweet's handle?  I'm doing what is below which feels kind of funny.

LinqToTwitter.AtomAuthor atomAuthor = rec.Author;

                string authorHandle = string.Empty;
                if (atomAuthor.Name.IndexOf("(") > 0)
                {
                    authorHandle = atomAuthor.Name.Substring(0, atomAuthor.Name.IndexOf("(") - 1).Trim();
                }

 


Coordinator
Oct 3, 2011 at 7:46 PM

Right, that's the only place the author handle/name is available and it needs to be parsed out.  Several of the properties contain combined data like this, but I'm hesitant to automate further because potential future changes to the Twitter API might break peoples code.

Joe

Oct 5, 2011 at 10:22 PM

I currently have what s below, but I'm not getting tweets from the sv_code_camp twitter account unless the tweets have #svcc in them.  Any suggestions?  

Also, I currently have this in a web url that I have a scheduled task hitting every 5 minutes to update my SqlServer database (for caching purposes).  Any better way to do that (OK, just 5 or 6 ways is all I need :))

 var search =
            (from tweet in ctx.Search
             where tweet.Type == SearchType.Search &&
                   tweet.Query == "@sv_code_camp OR #svcc OR #SVCC OR #Svcc OR @SV_CODE_CAMP" &&
                   tweet.Since == DateTime.Now.AddDays(numDaysToGoBack * -1).Date
             select tweet)
            .SingleOrDefault();
Thanks for the help!

Coordinator
Oct 5, 2011 at 11:13 PM

Peter,

I tried a couple combinations and didn't find anything that worked well.  Here's a tip - try the search page to see if you can get the right query first: http://twitter.com/#!/search-home.  There's an advanced query option too and you can watch what the query looks like through Fiddler. If that doesn't work, a potential hack is to do two queries where the second excludes "#svcc", like this:

"@sv_code_camp OR @SV_CODE_CAMP -#svcc -#SVCC"

Polling works, as you see.  Another option is to write a Windows Service with a timer.

The most efficient approach would be to use a filter stream - there are streaming examples in the downloadable code LinqToTwitterDemo.  A stream gives you the status as soon as it's tweeted and you don't have to poll - I would do this as a Windows Service. The stream approach is a fair amount of work in comparison to the polling you have in-place now.

Joe

Oct 5, 2011 at 11:21 PM

Thanks for the tips.  I'm into 30 second fixes since code camp is in 2 days.  I'll give this a try after I've had some rest and everyone has gone home.

Joe, I really appreciate you writing the code above for me. I would never have gotten it working on my compressed coding schedule without you.  I'd say the little add I made to code camp (planned 2 hours programming, turned into 14 hours programming) has been a huge success!  People really like it.