/// <summary> /// Custom event handler for handling monitored new comments as they come in. /// See Reddit.NETTests for more complex examples. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void C_NewCommentsUpdated(object sender, CommentsUpdateEventArgs e) { foreach (Comment comment in e.Added) { Console.WriteLine("[" + comment.Subreddit + "/" + comment.Root.Title + "] New Comment by " + comment.Author + ": " + comment.Body); } }
private async void Comments_NewUpdated(object sender, CommentsUpdateEventArgs e) { try { foreach (var comment in e.NewComments) { Parallel.ForEach(CommentHandlers, c => { //_logger.Information($"Processing comment: {comment.Id} Subreddit: {_subredditName} Handler: {c.GetType().Name}"); c.Process(comment, _callback); }); } } catch (RedditGatewayTimeoutException exception) { _logger.Error(exception, $"Exception caught in {nameof(Comments_NewUpdated)}. Redoing event and continuing..."); Subreddit.Comments.NewUpdated -= Comments_NewUpdated; SubscribeToCommentFeed(); } catch (RedditServiceUnavailableException exception) { _logger.Error(exception, $"Exception caught in {nameof(Comments_NewUpdated)}. Redoing event and continuing..."); Subreddit.Comments.NewUpdated -= Comments_NewUpdated; SubscribeToCommentFeed(); } catch (Exception exception) { _logger.Error(exception, $"Unexpected exception caught in {nameof(Comments_NewUpdated)}"); } }
private void C_AddNewCommentToQueue(object sender, CommentsUpdateEventArgs eventArgs) { foreach (var comment in eventArgs.Added) { var payload = BuildRedditQueueMessageFromRedditMessage(new CommentWrapper(comment)); _rabbitPublisher.Publish <QueueMessage>(payload, _routingKey); } var dept = _serviceConfigurations.QueueHost; }
private void C_NewCommentsUpdated(object sender, CommentsUpdateEventArgs e) { foreach (Comment comment in e.Added) { if (!NewComments.ContainsKey(comment.Fullname)) { NewComments.Add(comment.Fullname, comment); } } }
private void C_NewCommentsUpdated(object sender, CommentsUpdateEventArgs e) { foreach (Comment comment in e.Added) { if (comment.Author == _botSetting.BotName) { continue; } Logger.LogDebug($"New comment detected of /u/{comment.Author} in /r/{comment.Subreddit}"); _handleComment(comment); } }
private void C_NewCommentsUpdated(object sender, CommentsUpdateEventArgs e) { foreach (Comment comment in e.Added) { _logger.LogDebug($"New comment detected of /u/{comment.Author} in /r/{comment.Subreddit}"); if (comment.Depth > 5) { continue; } if (comment.Body.StartsWith("I'm sad that", StringComparison.OrdinalIgnoreCase)) { _buildReplyComment(comment); } } }
protected void TriggerUpdate(CommentsUpdateEventArgs args, string type) { switch (type) { case "confidence": OnConfidenceUpdated(args); break; case "top": OnTopUpdated(args); break; case "new": OnNewUpdated(args); break; case "controversial": OnControversialUpdated(args); break; case "old": OnOldUpdated(args); break; case "random": OnRandomUpdated(args); break; case "qa": OnQAUpdated(args); break; case "live": OnLiveUpdated(args); break; } }
internal virtual void OnQAUpdated(CommentsUpdateEventArgs e) { QAUpdated?.Invoke(this, e); }
internal virtual void OnRandomUpdated(CommentsUpdateEventArgs e) { RandomUpdated?.Invoke(this, e); }
internal virtual void OnControversialUpdated(CommentsUpdateEventArgs e) { ControversialUpdated?.Invoke(this, e); }
internal virtual void OnConfidenceUpdated(CommentsUpdateEventArgs e) { ConfidenceUpdated?.Invoke(this, e); }
private void MonitorCommentsThread(MonitoringSnapshot monitoring, string key, string type, string subKey, int startDelayMs = 0, int?monitoringDelayMs = null) { if (startDelayMs > 0) { Thread.Sleep(startDelayMs); } monitoringDelayMs = (monitoringDelayMs.HasValue ? monitoringDelayMs : Monitoring.Count() * MonitoringWaitDelayMS); while (!Terminate && Monitoring.Get(key).Contains(subKey)) { if (MonitoringExpiration.HasValue && DateTime.Now > MonitoringExpiration.Value) { MonitorModel.RemoveMonitoringKey(key, subKey, ref Monitoring); Threads.Remove(key); break; } while (!IsScheduled()) { if (Terminate) { break; } Thread.Sleep(15000); } if (Terminate) { break; } List <Comment> oldList; List <Comment> newList; try { switch (type) { default: throw new RedditControllerException("Unrecognized type '" + type + "'."); case "confidence": oldList = confidence; newList = GetConfidence(); break; case "top": oldList = top; newList = GetTop(); break; case "new": oldList = newComments; newList = GetNew(); break; case "controversial": oldList = controversial; newList = GetControversial(); break; case "old": oldList = old; newList = GetOld(); break; case "random": oldList = random; newList = GetRandom(); break; case "qa": oldList = qa; newList = GetQA(); break; case "live": oldList = live; newList = GetLive(); break; } if (Lists.ListDiff(oldList, newList, out List <Comment> added, out List <Comment> removed)) { // Event handler to alert the calling app that the list has changed. --Kris CommentsUpdateEventArgs args = new CommentsUpdateEventArgs { NewComments = newList, OldComments = oldList, Added = added, Removed = removed }; TriggerUpdate(args, type); } } catch (Exception) when(!BreakOnFailure) { } Wait(monitoringDelayMs.Value); } }
private void MonitorCommentsThread(MonitoringSnapshot monitoring, string key, string type, string subKey, int startDelayMs = 0) { if (startDelayMs > 0) { Thread.Sleep(startDelayMs); } while (!Terminate && Monitoring.Get(key).Contains(subKey)) { List <Comment> oldList; List <Comment> newList; switch (type) { default: throw new RedditControllerException("Unrecognized type '" + type + "'."); case "confidence": oldList = confidence; newList = GetConfidence(); break; case "top": oldList = top; newList = GetTop(); break; case "new": oldList = newComments; newList = GetNew(); break; case "controversial": oldList = controversial; newList = GetControversial(); break; case "old": oldList = old; newList = GetOld(); break; case "random": oldList = random; newList = GetRandom(); break; case "qa": oldList = qa; newList = GetQA(); break; case "live": oldList = live; newList = GetLive(); break; } if (Lists.ListDiff(oldList, newList, out List <Comment> added, out List <Comment> removed)) { // Event handler to alert the calling app that the list has changed. --Kris CommentsUpdateEventArgs args = new CommentsUpdateEventArgs { NewComments = newList, OldComments = oldList, Added = added, Removed = removed }; TriggerUpdate(args, type); } Thread.Sleep(Monitoring.Count() * MonitoringWaitDelayMS); } }