Hello there,
I did it! Please use the following code which is an example on how to solve this problem.
Regards,
Linvi
I did it! Please use the following code which is an example on how to solve this problem.
privateconststring SEARCH_KEYWORD = "hello"; publicstaticvoid Main() { var stream = CreateSafeFilteredStreamWithSingleTrack(SEARCH_KEYWORD); var streamThread = new Thread(stream.StartStreamMatchingAllConditions); streamThread.Start(); // We simulate that the stream stopped after running for 5 seconds Thread.Sleep(5000); stream.StopStream(); // Ensure that we do not close the application streamThread.Join(); } privatestatic IFilteredStream CreateSafeFilteredStreamWithSingleTrack(string track) { bool restorePreviousData = false; long lastTweetIdReceived = -1; var stream = Stream.CreateFilteredStream(); stream.AddTrack(track); stream.MatchingTweetReceived += (sender, args) => { if (restorePreviousData) { restorePreviousData = false; var sinceId = lastTweetIdReceived; var maxId = args.Tweet.Id; var restoreThread = new Thread(() => RestorePreviousDataBetween(sinceId, maxId)); restoreThread.Start(); } lastTweetIdReceived = args.Tweet.Id; StoreTweets(args.Tweet); }; stream.StreamStarted += (sender, args) => { Console.WriteLine("Stream Started"); bool didTheStreamReceivedAnyTweetYet = lastTweetIdReceived != -1; if (didTheStreamReceivedAnyTweetYet) { restorePreviousData = true; } }; stream.StreamStopped += (sender, args) => { constint awaitDuration = 30000; Console.WriteLine("Stream Stopped"); Console.WriteLine("Restarting the stream in {0} seconds", awaitDuration/1000); Thread.Sleep(awaitDuration); stream.StartStreamMatchingAllConditions(); }; return stream; } privatestatic ITweetSearchParameters GenerateSearchBeforeMaxId(long maxId) { var search = Search.GenerateTweetSearchParameter(SEARCH_KEYWORD); search.MaxId = maxId; search.MaximumNumberOfResults = 100; search.SearchType = SearchResultType.Recent; return search; } privatestaticvoid WaitForDataToBeIndexedByTwitterSearchApi(long maxId) { Console.WriteLine("Waiting for Twitter Search API to index the tweets that have been missed..."); var search = Search.GenerateTweetSearchParameter(SEARCH_KEYWORD); search.MaxId = maxId + 1; search.SinceId = maxId - 1; var searchResult = Search.SearchTweets(search); while (!searchResult.Any()) { Thread.Sleep(10000); searchResult = Search.SearchTweets(search); } Console.WriteLine("Twitter Search API successfully indexed the tweets..."); // We wait 5 more seconds as the TwitterSearchAPI can index tweets older tweets after more recent tweets Thread.Sleep(5000); } privatestaticvoid RestorePreviousDataBetween(long sinceId, long maxId) { Console.WriteLine("Restoring missed data..."); WaitForDataToBeIndexedByTwitterSearchApi(maxId); var tweetHelper = TweetinviContainer.Resolve<ITweetHelper>(); var result = Search.SearchTweets(GenerateSearchBeforeMaxId(maxId)).ToArray(); var lastTweetId = tweetHelper.GetOldestTweetId(result.Select(x => x.TweetDTO)); while (lastTweetId > sinceId) { StoreTweets(result.ToArray()); Console.WriteLine(string.Format("Restoring {0} tweets", result.Length)); result = Search.SearchTweets(GenerateSearchBeforeMaxId(lastTweetId)).ToArray(); lastTweetId = tweetHelper.GetOldestTweetId(result.Select(x => x.TweetDTO)); } var finalResultSet = result.Where(x => x.Id > sinceId).ToArray(); Console.WriteLine(string.Format("Restoring {0} tweets", finalResultSet.Length)); Console.WriteLine("All tweets have now been restored!"); StoreTweets(finalResultSet); } privatestaticvoid StoreTweets(params ITweet[] tweets) { // Do whatever you want here }
Linvi