/// <summary> /// Parses the connection file for getting access to twitter. /// </summary> protected override void ProcessRecord() { try { // Connect and authenticate var connection = TwitterConnection.Create(ConnectionFile); var service = connection.Service; // Search twitter using await and IAsyncResult from the twitter API var searchOptions = new SearchOptions(); searchOptions.Q = Query; searchOptions.Count = Limit; var statuses = new BlockingCollection <Tweet>(searchOptions.Count.Value); Task.Factory.FromAsync(service.Search(searchOptions, (tweets, response) => { switch (response.StatusCode) { case HttpStatusCode.OK: break; default: // TODO: Delegate Exception back to the pipeline thread if (null != response.Error) { throw new WebException(response.Error.Message); } throw new WebException(@"Querying Twitter failed!"); } // Add the tweets foreach (var status in tweets.Statuses) { statuses.Add(Tweet.Create(status)); } }), (result) => { // TODO: End action }).Wait(); // Write the tweets to the pipeline WriteObject(statuses, true); } catch (Exception ex) { WriteError(new ErrorRecord(ex, TwitterErrors.TwitterConnectionError.ToString(), ErrorCategory.ConnectionError, ConnectionFile)); } }
protected override void ProcessRecord() { try { // Connect and authenticate var connection = TwitterConnection.Create(ConnectionFile); var service = connection.Service; // Search twitter using await and IAsyncResult from the twitter API var errors = new BlockingCollection <TwitterError>(); var statuses = new BlockingCollection <Tweet>(); var resetEvent = new ManualResetEvent(false); Task.Factory.FromAsync(service.StreamFilterAndTrack(Track, (artifact, response) => { // TODO: There is always HTTP 0 returned! if (null == response.Error) { response.StatusCode = HttpStatusCode.OK; } switch (response.StatusCode) { case HttpStatusCode.OK: break; default: if (null != response.Error) { errors.Add(response.Error); } else { errors.Add(new TwitterError { Message = @"Streaming Twitter failed!" }); } // Activate the event resetEvent.Set(); return; } // Add the tweet var streamStatus = artifact as TwitterUserStreamStatus; if (null != streamStatus) { var status = streamStatus.Status; statuses.Add(Tweet.Create(status)); } }), (response) => { // TODO: End action }); // Wait for the event resetEvent.WaitOne(WaitTime); service.CancelStreaming(); // Write the tweets to the pipeline WriteObject(statuses, true); // Check the errors if (errors.Any()) { WriteObject(errors, true); } } catch (Exception ex) { WriteError(new ErrorRecord(ex, TwitterErrors.TwitterConnectionError.ToString(), ErrorCategory.ConnectionError, ConnectionFile)); } }