private void UpdateMatchesBasedOnTweetInReplyToUser(ITweet tweet, MatchOn matchOn,
                                                            Dictionary <long, Action <ITweet> > matchingFollowersAndActions, MatchedTweetReceivedEventArgs matchingTracksEventArgs,
                                                            Dictionary <long, Action <ITweet> > matchingQuotedFollowersAndActions)
        {
            if (matchOn.HasFlag(MatchOn.Everything) ||
                matchOn.HasFlag(MatchOn.FollowerInReplyTo))
            {
                var             userId = tweet.InReplyToUserId;
                Action <ITweet> actionToExecuteWhenMatchingFollower;

                if (userId != null && _followingUserIds.TryGetValue(userId, out actionToExecuteWhenMatchingFollower))
                {
                    matchingFollowersAndActions.TryAdd(userId.Value, actionToExecuteWhenMatchingFollower);
                    matchingTracksEventArgs.MatchOn |= MatchOn.FollowerInReplyTo;
                }

                if (tweet.QuotedTweet != null)
                {
                    var             quotedTweetCreatorId = tweet.QuotedTweet.InReplyToUserId;
                    Action <ITweet> actionToExecuteWhenMatchingFollowerFromQuotedTweet;

                    if (quotedTweetCreatorId != null && _followingUserIds.TryGetValue(quotedTweetCreatorId, out actionToExecuteWhenMatchingFollowerFromQuotedTweet))
                    {
                        matchingQuotedFollowersAndActions.TryAdd(quotedTweetCreatorId.Value, actionToExecuteWhenMatchingFollowerFromQuotedTweet);
                        matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.FollowerInReplyTo;
                    }
                }
            }
        }
        private void UpdateMatchesBasedOnUserMentions(ITweet tweet, MatchOn matchOn, Dictionary <string, Action <ITweet> > matchingTrackAndActions,
                                                      MatchedTweetReceivedEventArgs matchingTracksEventArgs, Dictionary <string, Action <ITweet> > matchingQuotedTrackAndActions)
        {
            if (matchOn.HasFlag(MatchOn.Everything) ||
                matchOn.HasFlag(MatchOn.AllEntities) ||
                matchOn.HasFlag(MatchOn.UserMentionEntities))
            {
                var mentionsScreenName = tweet.Entities.UserMentions.Select(x => x.ScreenName);
                mentionsScreenName.ForEach(x =>
                {
                    var tracksMatchingMentionScreenName = _streamTrackManager.GetMatchingTracksAndActions(x);
                    tracksMatchingMentionScreenName.ForEach(t => { matchingTrackAndActions.TryAdd(t.Item1, t.Item2); });
                    if (tracksMatchingMentionScreenName.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.UserMentionEntities;
                    }
                });

                if (tweet.QuotedTweet != null)
                {
                    var quotedMentionsScreenName = tweet.QuotedTweet.Entities.UserMentions.Select(x => x.ScreenName);
                    quotedMentionsScreenName.ForEach(x =>
                    {
                        var tracksMatchingMentionScreenName = _streamTrackManager.GetMatchingTracksAndActions(x);
                        tracksMatchingMentionScreenName.ForEach(t => { matchingQuotedTrackAndActions.TryAdd(t.Item1, t.Item2); });
                        if (tracksMatchingMentionScreenName.Count > 0)
                        {
                            matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.UserMentionEntities;
                        }
                    });
                }
            }
        }
        private void UpdateMatchesBasedOnHashTagEntities(ITweet tweet, MatchOn matchOn, Dictionary <string, Action <ITweet> > matchingTrackAndActions,
                                                         MatchedTweetReceivedEventArgs matchingTracksEventArgs, Dictionary <string, Action <ITweet> > matchingQuotedTrackAndActions)
        {
            if (matchOn.HasFlag(MatchOn.Everything) ||
                matchOn.HasFlag(MatchOn.AllEntities) ||
                matchOn.HasFlag(MatchOn.HashTagEntities))
            {
                var hashTags = tweet.Entities.Hashtags.Select(x => x.Text);

                hashTags.ForEach(x =>
                {
                    var tracksMatchingHashTag = _streamTrackManager.GetMatchingTracksAndActions(x);
                    tracksMatchingHashTag.ForEach(t => { matchingTrackAndActions.TryAdd(t.Item1, t.Item2); });
                    if (tracksMatchingHashTag.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.HashTagEntities;
                    }
                });

                if (tweet.QuotedTweet != null)
                {
                    var quotedHashTags = tweet.QuotedTweet.Entities.Hashtags.Select(x => x.Text);

                    quotedHashTags.ForEach(x =>
                    {
                        var tracksMatchingHashTag = _streamTrackManager.GetMatchingTracksAndActions(x);
                        tracksMatchingHashTag.ForEach(t => { matchingQuotedTrackAndActions.TryAdd(t.Item1, t.Item2); });
                        if (tracksMatchingHashTag.Count > 0)
                        {
                            matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.HashTagEntities;
                        }
                    });
                }
            }
        }
        // Update Event Args
        private void UpdateMatchesBasedOnTweetText(ITweet tweet, MatchOn matchOn, Dictionary <string, Action <ITweet> > matchingTrackAndActions,
                                                   MatchedTweetReceivedEventArgs matchingTracksEventArgs, Dictionary <string, Action <ITweet> > matchingQuotedTrackAndActions)
        {
            if (matchOn.HasFlag(MatchOn.Everything) ||
                matchOn.HasFlag(MatchOn.TweetText))
            {
                var tracksMatchingTweetText = _streamTrackManager.GetMatchingTracksAndActions(tweet.FullText);
                tracksMatchingTweetText.ForEach(x => { matchingTrackAndActions.TryAdd(x.Item1, x.Item2); });
                if (tracksMatchingTweetText.Count > 0)
                {
                    matchingTracksEventArgs.MatchOn |= MatchOn.TweetText;
                }

                if (tweet.QuotedTweet != null)
                {
                    var tracksMatchingQuotedTweetText = _streamTrackManager.GetMatchingTracksAndActions(tweet.QuotedTweet.FullText);
                    tracksMatchingQuotedTweetText.ForEach(x => { matchingQuotedTrackAndActions.TryAdd(x.Item1, x.Item2); });
                    if (tracksMatchingQuotedTweetText.Count > 0)
                    {
                        matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.TweetText;
                    }
                }
            }
        }
        private void UpdateMatchesBasedOnTweetLocation(ITweet tweet, MatchOn matchOn, Dictionary <ILocation, Action <ITweet> > matchingLocationAndActions,
                                                       MatchedTweetReceivedEventArgs matchingTracksEventArgs, Dictionary <ILocation, Action <ITweet> > matchingQuotedLocationAndActions)
        {
            if (matchOn.HasFlag(MatchOn.Everything) ||
                matchOn.HasFlag(MatchOn.TweetLocation))
            {
                var matchedLocations = GetMatchedLocations(tweet).ToArray();
                matchedLocations.ForEach(x => { matchingLocationAndActions.TryAdd(x.Key, x.Value); });
                if (matchedLocations.Length > 0)
                {
                    matchingTracksEventArgs.MatchOn |= MatchOn.TweetLocation;
                }

                if (tweet.QuotedTweet != null)
                {
                    var quotedMatchedLocations = GetMatchedLocations(tweet.QuotedTweet).ToArray();
                    quotedMatchedLocations.ForEach(x => { matchingQuotedLocationAndActions.TryAdd(x.Key, x.Value); });
                    if (quotedMatchedLocations.Length > 0)
                    {
                        matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.TweetLocation;
                    }
                }
            }
        }
        private void UpdateMatchesBasedOnUrlEntities(
            ITweet tweet,
            MatchOn matchOn,
            Dictionary <string, Action <ITweet> > matchingTrackAndActions,
            MatchedTweetReceivedEventArgs matchingTracksEventArgs,
            Dictionary <string, Action <ITweet> > matchingQuotedTrackAndActions)
        {
            if (matchOn.HasFlag(MatchOn.Everything) ||
                matchOn.HasFlag(MatchOn.AllEntities) ||
                matchOn.HasFlag(MatchOn.URLEntities))
            {
                var expandedURLs = tweet.Entities.Urls.Select(x => x.ExpandedURL);
                expandedURLs = expandedURLs.Union(tweet.Entities.Medias.Select(x => x.ExpandedURL));
                expandedURLs.ForEach(x =>
                {
                    var tracksMatchingExpandedURL = _streamTrackManager.GetMatchingTracksAndActions(x);
                    tracksMatchingExpandedURL.ForEach(t => { matchingTrackAndActions.TryAdd(t.Item1, t.Item2); });
                    if (tracksMatchingExpandedURL.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.URLEntities;
                    }
                });

                var displayedURLs = tweet.Entities.Urls.Select(x => x.DisplayedURL);
                displayedURLs = displayedURLs.Union(tweet.Entities.Medias.Select(x => x.DisplayURL));
                displayedURLs.ForEach(x =>
                {
                    var tracksMatchingDisplayedURL = _streamTrackManager.GetMatchingTracksAndActions(x);
                    tracksMatchingDisplayedURL.ForEach(t => { matchingTrackAndActions.TryAdd(t.Item1, t.Item2); });
                    if (tracksMatchingDisplayedURL.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.URLEntities;
                    }
                });

                if (tweet.QuotedTweet != null)
                {
                    var quotedTweetExpandedURLs = tweet.QuotedTweet.Entities.Urls.Select(x => x.ExpandedURL);
                    quotedTweetExpandedURLs = quotedTweetExpandedURLs.Union(tweet.QuotedTweet.Entities.Medias.Select(x => x.ExpandedURL));
                    quotedTweetExpandedURLs.ForEach(x =>
                    {
                        var tracksMatchingExpandedURL = _streamTrackManager.GetMatchingTracksAndActions(x);
                        tracksMatchingExpandedURL.ForEach(t => { matchingQuotedTrackAndActions.TryAdd(t.Item1, t.Item2); });
                        if (tracksMatchingExpandedURL.Count > 0)
                        {
                            matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.URLEntities;
                        }
                    });

                    var quotedTweetDisplayedURLs = tweet.QuotedTweet.Entities.Urls.Select(x => x.DisplayedURL);
                    quotedTweetDisplayedURLs = quotedTweetDisplayedURLs.Union(tweet.QuotedTweet.Entities.Medias.Select(x => x.DisplayURL));
                    quotedTweetDisplayedURLs.ForEach(x =>
                    {
                        var tracksMatchingDisplayedURL = _streamTrackManager.GetMatchingTracksAndActions(x);
                        tracksMatchingDisplayedURL.ForEach(t => { matchingQuotedTrackAndActions.TryAdd(t.Item1, t.Item2); });
                        if (tracksMatchingDisplayedURL.Count > 0)
                        {
                            matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.URLEntities;
                        }
                    });
                }
            }
        }
Exemple #7
0
        private MatchedTweetReceivedEventArgs GetMatchingTweetEventArgsAndRaiseMatchingElements(ITweet tweet, string json)
        {
            var matchingTracksEventArgs = new MatchedTweetReceivedEventArgs(tweet, json);

            var matchingTrackAndActions     = new Dictionary <string, Action <ITweet> >();
            var matchingLocationAndActions  = new Dictionary <ILocation, Action <ITweet> >();
            var matchingFollowersAndActions = new Dictionary <long, Action <ITweet> >();

            if (MatchOn.HasFlag(MatchOn.Everything) ||
                MatchOn.HasFlag(MatchOn.TweetText))
            {
                var tracksMatchingTweetText = _streamTrackManager.GetMatchingTracksAndActions(tweet.FullText);

                tracksMatchingTweetText.ForEach(x =>
                {
                    matchingTrackAndActions.TryAdd(x.Item1, x.Item2);
                });

                if (tracksMatchingTweetText.Count > 0)
                {
                    matchingTracksEventArgs.MatchOn |= MatchOn.TweetText;
                }
            }

            if (MatchOn.HasFlag(MatchOn.Everything) ||
                MatchOn.HasFlag(MatchOn.AllEntities) ||
                MatchOn.HasFlag(MatchOn.URLEntities))
            {
                var expandedURLs = tweet.Entities.Urls.Select(x => x.ExpandedURL);
                expandedURLs = expandedURLs.Union(tweet.Entities.Medias.Select(x => x.ExpandedURL));

                expandedURLs.ForEach(x =>
                {
                    var tracksMatchingExpandedURL = _streamTrackManager.GetMatchingTracksAndActions(x);
                    tracksMatchingExpandedURL.ForEach(t =>
                    {
                        matchingTrackAndActions.TryAdd(t.Item1, t.Item2);
                    });

                    if (tracksMatchingExpandedURL.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.URLEntities;
                    }
                });

                var displayedURLs = tweet.Entities.Urls.Select(x => x.DisplayedURL);
                displayedURLs = displayedURLs.Union(tweet.Entities.Medias.Select(x => x.DisplayURL));

                displayedURLs.ForEach(x =>
                {
                    var tracksMatchingDisplayedURL = _streamTrackManager.GetMatchingTracksAndActions(x);

                    tracksMatchingDisplayedURL.ForEach(t =>
                    {
                        matchingTrackAndActions.TryAdd(t.Item1, t.Item2);
                    });

                    if (tracksMatchingDisplayedURL.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.URLEntities;
                    }
                });
            }

            if (MatchOn.HasFlag(MatchOn.Everything) ||
                MatchOn.HasFlag(MatchOn.AllEntities) ||
                MatchOn.HasFlag(MatchOn.HashTagEntities))
            {
                var hashTags = tweet.Entities.Hashtags.Select(x => x.Text);

                hashTags.ForEach(x =>
                {
                    var tracksMatchingHashTag = _streamTrackManager.GetMatchingTracksAndActions(x);

                    tracksMatchingHashTag.ForEach(t =>
                    {
                        matchingTrackAndActions.TryAdd(t.Item1, t.Item2);
                    });

                    if (tracksMatchingHashTag.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.HashTagEntities;
                    }
                });
            }

            if (MatchOn.HasFlag(MatchOn.Everything) ||
                MatchOn.HasFlag(MatchOn.AllEntities) ||
                MatchOn.HasFlag(MatchOn.UserMentionEntities))
            {
                var mentionsScreenName = tweet.Entities.UserMentions.Select(x => x.ScreenName);

                mentionsScreenName.ForEach(x =>
                {
                    var tracksMatchingMentionScreenName = _streamTrackManager.GetMatchingTracksAndActions(x);

                    tracksMatchingMentionScreenName.ForEach(t =>
                    {
                        matchingTrackAndActions.TryAdd(t.Item1, t.Item2);
                    });

                    if (tracksMatchingMentionScreenName.Count > 0)
                    {
                        matchingTracksEventArgs.MatchOn |= MatchOn.UserMentionEntities;
                    }
                });
            }

            if (MatchOn.HasFlag(MatchOn.Everything) ||
                MatchOn.HasFlag(MatchOn.TweetLocation))
            {
                var matchedLocations = GetMatchedLocations(tweet).ToArray();

                matchedLocations.ForEach(x =>
                {
                    matchingLocationAndActions.TryAdd(x.Key, x.Value);
                });

                if (matchedLocations.Length > 0)
                {
                    matchingTracksEventArgs.MatchOn |= MatchOn.TweetLocation;
                }
            }

            if (MatchOn.HasFlag(MatchOn.Everything) ||
                MatchOn.HasFlag(MatchOn.Follower))
            {
                var             userId = tweet.CreatedBy?.Id;
                Action <ITweet> actionToExecuteWhenMatchingFollower;

                if (userId != null && _followingUserIds.TryGetValue(userId, out actionToExecuteWhenMatchingFollower))
                {
                    matchingFollowersAndActions.TryAdd(userId.Value, actionToExecuteWhenMatchingFollower);
                    matchingTracksEventArgs.MatchOn |= MatchOn.Follower;
                }
            }

            if (MatchOn.HasFlag(MatchOn.Everything) ||
                MatchOn.HasFlag(MatchOn.FollowerInReplyTo))
            {
                var             userId = tweet.InReplyToUserId;
                Action <ITweet> actionToExecuteWhenMatchingFollower;

                if (userId != null && _followingUserIds.TryGetValue(userId, out actionToExecuteWhenMatchingFollower))
                {
                    matchingFollowersAndActions.TryAdd(userId.Value, actionToExecuteWhenMatchingFollower);
                    matchingTracksEventArgs.MatchOn |= MatchOn.FollowerInReplyTo;
                }
            }

            var matchingTracks    = matchingTrackAndActions.Select(x => x.Key).ToArray();
            var matchingLocations = matchingLocationAndActions.Select(x => x.Key).ToArray();
            var matchingFollowers = matchingFollowersAndActions.Select(x => x.Key).ToArray();

            matchingTracksEventArgs.MatchingTracks    = matchingTracks;
            matchingTracksEventArgs.MatchingLocations = matchingLocations;
            matchingTracksEventArgs.MatchingFollowers = matchingFollowers;

            CallMultipleActions(tweet, matchingTrackAndActions.Select(x => x.Value));
            CallMultipleActions(tweet, matchingLocationAndActions.Select(x => x.Value));
            CallMultipleActions(tweet, matchingFollowersAndActions.Select(x => x.Value));

            return(matchingTracksEventArgs);
        }