Streaming APIs - Public Streams

Jun 8, 2013 at 8:18 PM
How do I fetch Public Streams Using LinqToTwitter by passing a list of User IDs I want to follow as mentioned here in Twitter API Docs https://dev.twitter.com/docs/api/1.1/post/statuses/filter
Coordinator
Jun 9, 2013 at 9:02 PM
Hi,

You would populate the Follow property with a comma-separated list of User IDs, like this:
            (from strm in twitterCtx.Streaming
             where strm.Type == StreamingType.Filter &&
                   strm.Track == "twitter,JoeMayo,linq2twitter,microsoft,google,oracle" &&
                   strm.Follow == "15411837,16761255"
             select strm)
            .StreamingCallback(strm =>
            {
                if (strm.Status != TwitterErrorStatus.Success)
                {
                    Console.WriteLine(strm.Error.ToString());
                    return;
                }

                Console.WriteLine(strm.Content + "\n");

                if (count++ >= 2)
                {
                    strm.CloseStream();
                }
            })
            .SingleOrDefault();
@JoeMayo
Jun 17, 2013 at 10:31 AM
Edited Jun 17, 2013 at 10:33 AM
A follow-up on this; do you know how to dynamically build a Linq statement if I don't know at compile time what filters will be active? This does not work:
public IQueryable<Streaming> BuildQuery(TwitterContext context, string track, string follow, string locations)
{
    var query = from stream in context.Streaming
                where stream.Type == StreamingType.Filter
                    && (track == null || stream.Track == track)
                    && (follow == null || stream.Follow == follow)
                    && (locations == null || stream.Locations == locations)
                select stream;
    return query;
}
LinqToTwitter also gives me errors if I set any of the filters to an empty string or null, so I need a way to include the respective parts of the where clause only if filters of that type are active.
Coordinator
Jun 17, 2013 at 2:19 PM
You can do conditional expressions. Here's an example from the LinqToTwitterDemo project from the downloadable source code in the SearchDemos.cs file:
        static void DynamicSearchDemo(TwitterContext twitterCtx)
        {
            const string TwitterSearchGeocodeFormat = "{0},{1},{2}";
            string query = "Twitter";
            string language = null;
            string locale = null;
            string latitude = "37.781157";
            string longitude = "-122.398720";
            uint radius = 1;
            string radiusUnitType = "mi";

            var srchQuery =
                from srch in twitterCtx.Search
                where srch.Type == SearchType.Search
                select srch;

            if (!string.IsNullOrWhiteSpace(query))
            {
                srchQuery = srchQuery.Where(srch => srch.Query == query);
            }

            if (!string.IsNullOrWhiteSpace(language))
            {
                srchQuery = srchQuery.Where(srch => srch.SearchLanguage == language);
            }

            if (!string.IsNullOrWhiteSpace(locale))
            {
                srchQuery = srchQuery.Where(srch => srch.Locale == locale);
            }

            if (!string.IsNullOrWhiteSpace(longitude) && !string.IsNullOrWhiteSpace(latitude) && radius > 0)
            {
                var radiusString = string.Format("{0}{1}", radius, radiusUnitType.ToString().ToLower());
                var geoCodeParameter = String.Format(TwitterSearchGeocodeFormat, latitude, longitude, radiusString);

                srchQuery = srchQuery.Where(srch => srch.GeoCode == geoCodeParameter);
            }

            var srchResult = srchQuery.SingleOrDefault();

            Console.WriteLine("\nQuery: {0}\n", srchResult.SearchMetaData.Query);
            srchResult.Statuses.ForEach(entry =>
                Console.WriteLine(
                    "ID: {0, -15}, Source: {1}\nContent: {2}\n",
                    entry.StatusID, entry.Source, entry.Text));
        }
There's also an example that shows how to build an expression tree.

@JoeMayo
Jun 18, 2013 at 7:43 AM
Thanks!