private static void StreamFilterExample(IToken token) { FilteredStream stream = new FilteredStream(); // Adding Tracks filters stream.AddTrack("Tweetinvi Linvi"); // stream.AddTrack("Linvi"); stream.StartStream(token, x => ProcessTweet(x)); }
public void GetTweets(string Search_Variable) { try { stream = (FilteredStream)Tweetinvi.Stream.CreateFilteredStream(GetCredentials); stream.AddTrack(Search_Variable); stream.JsonObjectReceived += Stream_JsonObjectReceived; stream.StartStreamMatchingAnyCondition(); } catch (Exception ex) { throw ex; } }
async void StartTwitterStream(IToken token) { var stream = new FilteredStream(); CrawlingLog.Log.Info("Init TwitterUpdateStrategy with Hashtag '{0}'".FormatWith(HashtagTrigger)); Log.Info("Init TwitterUpdateStrategy with Hashtag '{0}'".FormatWith(HashtagTrigger), this); stream.AddTrack(HashtagTrigger); stream.StreamStarted += (sender, args) => CrawlingLog.Log.Info("Twitter stream started"); // The following call blocks stream.StartStream(token, x => HandleTweet(x)); }
public void StartStreamTrackRandomUniqueWord() { // Arrange var randomWord = String.Format("Tweetinvi{0}", new Random().Next()); var expectedMessage = String.Format("Hello {0}", randomWord); bool result = false; Thread t = new Thread(() => { IFilteredStream stream = new FilteredStream(); stream.AddTrack(randomWord); Func <ITweet, bool> listen = delegate(ITweet tweet) { if (tweet != null) { result = tweet.Text == expectedMessage; } // End the stream return(false); }; stream.StartStream(TokenSingleton.Token, listen); }); t.Start(); // Act ITweet newTweet = new Tweet(expectedMessage, TokenTestSingleton.Instance); newTweet.Publish(); Thread.Sleep(500); t.Join(); // Assert Assert.AreEqual(result, true); // Cleanup newTweet.Destroy(); }
public static void SearchTwitterStreaming() { var token = new TwitterToken.Token(_accessToken, _accessTokenSecret, _consumerKey, _consumerSecret); // Create the stream var myStream = new FilteredStream(); myStream.AddTrack("#apple"); myStream.AddTrack("#google"); myStream.AddTrack("#twitter"); myStream.AddTrack("#tesla"); myStream.AddTrack("#ford"); myStream.AddTrack("#shell"); myStream.AddTrack("#bks"); // Starting the stream by specifying credentials thanks to the Token and a delegate specifying what you want to do when you receive a tweet using (var conn = new SqlCeConnection(_sqlConnection)) { conn.Open(); var insert = new SqlCeCommand("INSERT INTO Tweets(Text, UserFollowers, RetweetCount, CreatedAt) Values(@Text, @UserFollowers, @RetweetCount, @CreatedAt)", conn); try { myStream.StartStream(token, tweet => { insert.Parameters.Clear(); insert.Parameters.AddWithValue("@Text", tweet.Text); insert.Parameters.AddWithValue("@UserFollowers", tweet.Creator.FollowersCount); insert.Parameters.AddWithValue("@RetweetCount", tweet.RetweetCount); insert.Parameters.AddWithValue("@CreatedAt", tweet.CreatedAt); var status = insert.ExecuteNonQuery(); //Console.WriteLine("{0}: {1}", status, tweet.Text); }); } catch { } finally { conn.Close(); } } }
public void StartStreamTrackWord1ORWord2InTheSameTweet() { // Arrange var randomWord1 = String.Format("Tweetinvi{0}", new Random().Next()); var randomWord2 = String.Format("Tweetinvi2{0}", new Random().Next()); var expectedMessage1 = String.Format("Hello {0}", randomWord1); var expectedMessage2 = String.Format("Hello {0}", randomWord2); var expectedMessage3 = String.Format("Hello {0} {1}", randomWord1, randomWord2); int i = 0; Thread t = new Thread(() => { IFilteredStream stream = new FilteredStream(); stream.AddTrack(randomWord1); stream.AddTrack(randomWord2); Func <ITweet, bool> listen = delegate(ITweet tweet) { if (tweet != null) { bool result = tweet.Text == expectedMessage1 || tweet.Text == expectedMessage2 || tweet.Text == expectedMessage3; if (result) { Debug.WriteLine(tweet.Text); ++i; } } // End the stream return(true); }; Timer timer = new Timer(5000); timer.Elapsed += (sender, args) => { timer.Stop(); stream.StopStream(); }; timer.Start(); stream.StartStream(TokenSingleton.Token, listen); }); t.Start(); // Act ITweet newTweet1 = new Tweet(expectedMessage1, TokenTestSingleton.Instance); newTweet1.Publish(); ITweet newTweet2 = new Tweet(expectedMessage2, TokenTestSingleton.Instance); newTweet2.Publish(); ITweet newTweet3 = new Tweet(expectedMessage3, TokenTestSingleton.Instance); newTweet3.Publish(); t.Join(); // Cleanup newTweet1.Destroy(); newTweet2.Destroy(); newTweet3.Destroy(); // Assert Assert.AreEqual(i, 3); }