/// <summary>
        /// Start a query stream
        /// </summary>
        /// <param name="tfSettingPath"></param>
        /// <param name="extraSettingPath"></param>
        public static async Task StartFilteredStream(string tfSettingPath, string resultPath)
        {
            filteredStream = Tweetinvi.Stream.CreateFilteredStream(twitterCert);
            StreamWriter writer = new StreamWriter(resultPath);

            for (int i = 0; i < File.ReadLines(tfSettingPath).Count(); i++)
            {
                filteredStream.AddTrack(File.ReadLines(tfSettingPath)
                                        .Skip(i)
                                        .Take(1)
                                        .First()
                                        );
            }

            // When stream found one matching...
            filteredStream.MatchingTweetReceived += (sender, args) =>
            {
                FilteredTweetCount++;
                Console.WriteLine("[ " + DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString() + " ]"
                                  + "  Found {0} filtered tweet(s).", FilteredTweetCount.ToString());

                /*
                 *  Record these following information:
                 *    - Is it retweeted? True or False?
                 *    - Retweet count
                 *    - Length of this tweet
                 *    - The user name who created it
                 *    - The tweet's country the user sent the tweet
                 *    - The tweet's full name of the place where the user sent the tweet
                 */

                string resultTweet = string.Empty;

                if (args.Tweet.Place != null) // Try recording all location info.
                {
                    resultTweet = @"""" + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToLongTimeString() +
                                  @""",""" + args.Tweet.Text + @"""," +
                                  args.Tweet.IsRetweet.ToString() + "," + args.Tweet.RetweetCount.ToString() + "," +
                                  args.Tweet.PublishedTweetLength.ToString() + "," + args.Tweet.FavoriteCount.ToString() + ",,," +
                                  args.Tweet.Place.Country.ToString() + "," + args.Tweet.Place.FullName.ToString().Replace(",", " ");
                }
                else // ...what a pity, no location information! So, just record something else available only!
                {
                    resultTweet = @"""" + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToLongTimeString() +
                                  @""",""" + args.Tweet.Text + @"""," + args.Tweet.IsRetweet.ToString() + "," + args.Tweet.RetweetCount.ToString() + "," +
                                  args.Tweet.PublishedTweetLength.ToString() + "," + args.Tweet.FavoriteCount.ToString();
                }


                writer.WriteLine(resultTweet);
            };

            filteredStream.StreamStopped += (sender, args) =>
            {
                writer.Flush();
                writer.Dispose();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("====================STOPPED====================");
                Console.WriteLine("TwitterAnalyzer Filter Stream stopped.");
                Console.WriteLine();

                if (args.Exception != null)
                {
                    Console.WriteLine("Exceptions: ");
                    Console.WriteLine(args.Exception.Message);
                    Console.WriteLine();
                    if (args.Exception.InnerException != null)
                    {
                        Console.WriteLine(args.Exception.InnerException.Message);
                        Console.WriteLine();
                    }
                }

                if (args.DisconnectMessage != null)
                {
                    Console.WriteLine("Reason: ");
                    Console.WriteLine(args.DisconnectMessage.Reason);
                    Console.WriteLine();
                }


                Console.WriteLine("====================STOPPED====================");
            };

            filteredStream.StreamPaused += (sender, args) =>
            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("====================PAUSED====================");
                Console.WriteLine("TwitterAnalyzer Filter Stream paused.");
                Console.WriteLine("====================PAUSED====================");
            };

            await filteredStream.StartStreamMatchingAllConditionsAsync();
        }
Beispiel #2
0
        public void Start(string keyWords)
        {
            if(!Running)
            {
                Tweetinvi.Auth.ApplicationCredentials = new Tweetinvi.Core.Credentials.TwitterCredentials(
                    ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret);

                var words = keyWords.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                if(words.Any())
                {
                    m_stream = Tweetinvi.Stream.CreateFilteredStream();
                    m_stream.MatchingTweetReceived += OnMatchingTweekReceived;
                    m_stream.StreamStopped += OnStreamStopped;
                    foreach(string word in words)
                    {
                        m_stream.AddTrack(word);
                    }
                    m_streamTask = m_stream.StartStreamMatchingAnyConditionAsync();
                }
            }
        }