Ejemplo n.º 1
0
        private async void OnTweetReceived(object sender, MatchedTweetReceivedEventArgs matchedTweetReceivedEventArgs)
        {
            // Skip replies
            if (matchedTweetReceivedEventArgs.Tweet.InReplyToUserId != null && !TwitterToChannels.ContainsKey(matchedTweetReceivedEventArgs.Tweet.InReplyToUserId.GetValueOrDefault()))
            {
                return;
            }

            Log.WriteDebug("Twitter", $"Streamed {matchedTweetReceivedEventArgs.Tweet.Url}: {matchedTweetReceivedEventArgs.Tweet.FullText}");

            if (!TwitterToChannels.ContainsKey(matchedTweetReceivedEventArgs.Tweet.CreatedBy.Id))
            {
                return;
            }

            // TODO: Streaming api does not seem to return extended tweets
            var tweet = await TweetAsync.GetTweet(matchedTweetReceivedEventArgs.Tweet.Id);

            if (tweet?.FullText == null)
            {
                return;
            }

            var text = $"{Color.BLUE}@{tweet.CreatedBy.ScreenName}{Color.NORMAL} tweeted {tweet.CreatedAt.ToRelativeString()}: {FormatTweet(tweet)} -{Color.DARKBLUE} {tweet.Url}";

            foreach (var channel in TwitterToChannels[tweet.CreatedBy.Id])
            {
                Bootstrap.Client.Client.Message(channel, text);
            }
        }
        public MatchedTweetReceivedEventArgs GetMatchingTweetEventArgsAndRaiseMatchingElements(ITweet tweet, string json, MatchOn matchOn)
        {
            var result = new MatchedTweetReceivedEventArgs(tweet, json);

            var trackMatcherConfig     = new FilteredStreamMatcherConfig <string>(matchOn);
            var locationMatcherConfig  = new FilteredStreamMatcherConfig <ILocation>(matchOn);
            var followersMatcherConfig = new FilteredStreamMatcherConfig <long>(matchOn);

            UpdateMatchesBasedOnTweetText(tweet, trackMatcherConfig, result);
            UpdateMatchesBasedOnUrlEntities(tweet, trackMatcherConfig, result);
            UpdateMatchesBasedOnHashTagEntities(tweet, trackMatcherConfig, result);
            UpdateMatchesBasedOnUserMentions(tweet, trackMatcherConfig, result);
            UpdateMatchesBasedOnSymbols(tweet, trackMatcherConfig, result);
            UpdateMatchesBasedOnTweetLocation(tweet, locationMatcherConfig, result);
            UpdateMatchesBasedOnTweetCreator(tweet, followersMatcherConfig, result);
            UpdateMatchesBasedOnTweetInReplyToUser(tweet, followersMatcherConfig, result);

            result.MatchingTracks    = trackMatcherConfig.TweetMatchingTrackAndActions.Select(x => x.Key).ToArray();
            result.MatchingLocations = locationMatcherConfig.TweetMatchingTrackAndActions.Select(x => x.Key).ToArray();
            result.MatchingFollowers = followersMatcherConfig.TweetMatchingTrackAndActions.Select(x => x.Key).ToArray();

            result.RetweetMatchingTracks    = trackMatcherConfig.RetweetMatchingTrackAndActions.Select(x => x.Key).ToArray();
            result.RetweetMatchingLocations = locationMatcherConfig.RetweetMatchingTrackAndActions.Select(x => x.Key).ToArray();
            result.RetweetMatchingFollowers = followersMatcherConfig.RetweetMatchingTrackAndActions.Select(x => x.Key).ToArray();

            result.QuotedTweetMatchingTracks    = trackMatcherConfig.QuotedTweetMatchingTrackAndActions.Select(x => x.Key).ToArray();
            result.QuotedTweetMatchingLocations = locationMatcherConfig.QuotedTweetMatchingTrackAndActions.Select(x => x.Key).ToArray();
            result.QuotedTweetMatchingFollowers = followersMatcherConfig.QuotedTweetMatchingTrackAndActions.Select(x => x.Key).ToArray();

            CallMultipleActions(tweet, trackMatcherConfig.GetAllMatchingTracks().Select(x => x.Value));
            CallMultipleActions(tweet, locationMatcherConfig.GetAllMatchingTracks().Select(x => x.Value));
            CallMultipleActions(tweet, followersMatcherConfig.GetAllMatchingTracks().Select(x => x.Value));

            return(result);
        }
Ejemplo n.º 3
0
        /*private functions*/

        //Function responsible for handling the match to a tweet from the stream
        void handleMatchingTweet(object sender, MatchedTweetReceivedEventArgs args)
        {
            Matches++;
            foreach (var media in args.Tweet.Media)
            {
                //If the media is a photo
                //right now the only mediatype twitter gives us, but can change in the future
                if (media.MediaType == "photo")
                {
                    var url = media.MediaURL ?? media.MediaURLHttps;

                    //Filename is taken from the last section of the url
                    var fileName = url.Substring(url.LastIndexOf("/") + 1);

                    //download picture to image
                    var image = GetDataByHttp(url);

                    //TODO: Find male and female faces in image and generate a Gender[]

                    //Store Metadata for the processed picture(Todo: pass Gender[] instead of null
                    PictureMetaData meta = new PictureMetaData(DateTime.Now, null);
                    PictureMetaDataList.Add(meta);

                    //save the picture in the pictureQueue(treadsafe)
                    EnqueuePicture(new PictureData(image, fileName, meta));

                    DownloadedPictures++;
                }
            }
        }
        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;
                        }
                    });
                }
            }
        }
 private void OnMatchingTweetReceived(object sender, MatchedTweetReceivedEventArgs e)
 {
     Trace.WriteLine(string.Concat("Twitter | ", e.Tweet.Text));
     twitterSender.SendMessage(e.Tweet);
     twitterLike.SendMessage(e.Tweet);
     twitterDislike.SendMessage(e.Tweet);
 }
Ejemplo n.º 6
0
        private static void Stream_TweetReceived(object sender, MatchedTweetReceivedEventArgs e)
        {
            if (e.Tweet.InReplyToStatusId == null)
            {
                return;
            }
            ITweet r1 = Tweetinvi.Tweet.GetTweet((long)e.Tweet.InReplyToStatusId);

            if (r1 == null)
            {
                return;
            }
            Conversation c = new Conversation();

            c.Tweets.Add(InviTweetToPTTweet(e.Tweet));
            c.Tweets.Add(InviTweetToPTTweet(r1));
            using (var model = new AnnotatorModel())
            {
                Console.WriteLine(r1.Text);
                Console.WriteLine(e.Tweet.Text);
                model.Tweets.Add(InviTweetToPTTweet(r1));
                model.Tweets.Add(InviTweetToPTTweet(e.Tweet));
                model.Conversations.Add(c);
                model.SaveChanges();
            }
        }
        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 UpdateMatchesBasedOnTweetCreator(ITweet tweet, MatchOn matchOn, Dictionary <long, Action <ITweet> > matchingFollowersAndActions,
                                                      MatchedTweetReceivedEventArgs matchingTracksEventArgs, Dictionary <long, Action <ITweet> > matchingQuotedFollowersAndActions)
        {
            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 (tweet.QuotedTweet != null)
                {
                    var             quotedTweetCreatorId = tweet.QuotedTweet.CreatedBy?.Id;
                    Action <ITweet> actionToExecuteWhenMatchingFollowerFromQuotedTweet;

                    if (quotedTweetCreatorId != null && _followingUserIds.TryGetValue(quotedTweetCreatorId, out actionToExecuteWhenMatchingFollowerFromQuotedTweet))
                    {
                        matchingQuotedFollowersAndActions.TryAdd(quotedTweetCreatorId.Value, actionToExecuteWhenMatchingFollowerFromQuotedTweet);
                        matchingTracksEventArgs.QuotedTweetMatchOn |= MatchOn.Follower;
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public static void TweetRecieved(object sender, MatchedTweetReceivedEventArgs args)
        {
            if (args.Tweet == null)
            {
                return;
            }

            Console.WriteLine($"Tweet recieved {args.Tweet.FullText}");

            var report = new FireReport()
            {
                TimeStamp    = args.Tweet.CreatedAt,
                FireSeverity = EFireSeverity.Unkown,
                Description  = args.Tweet.FullText
            };

            if (report.Description != null && (report.Description.ToLower().Contains("severe") || report.Description.ToLower().Contains("urgent")))
            {
                report.FireSeverity = EFireSeverity.LargerThan100LessThan500Meters;
            }

            if (args.Tweet.Place?.BoundingBox?.Coordinates != null)
            {
                report.BoundingBox = args.Tweet.Place.BoundingBox.Coordinates.Select(x => x.ToGeoCoordinate()).ToList();
                report.Coordinates = new GeoCoordinate(args.Tweet.Place.BoundingBox.Coordinates.Average(x => x.Latitude), args.Tweet.Place.BoundingBox.Coordinates.Average(x => x.Longitude));
            }

            Console.WriteLine($"Trying to store tweet {JsonConvert.SerializeObject(report)}");

            _serviceProvider.GetService <IFireReport>()?.AddReport(report);
        }
Ejemplo n.º 10
0
        private static Gof.Twitter.Tweet formatTweet(MatchedTweetReceivedEventArgs e)
        {
            var author = e.Tweet.RetweetedTweet != null ? e.Tweet.RetweetedTweet.CreatedBy : e.Tweet.CreatedBy;
            var user   = e.Tweet.CreatedBy;

            var tweet = new Gof.Twitter.Tweet
            {
                Id           = e.Tweet.Id,
                TimeLineUser = new Gof.Twitter.User {
                    Alias = user.ScreenName, Name = user.Name, Id = user.Id
                },
                Author = new Gof.Twitter.User {
                    Alias = author.ScreenName, Name = author.Name, Id = author.Id
                },
                Content   = e.Tweet.Text,
                Date      = e.Tweet.TweetLocalCreationDate,
                IsRetweet = e.Tweet.IsRetweet,
                Language  = e.Tweet.Language.ToString(),
                Hashtags  = e.Tweet.Hashtags.Select(s => new Tuple <int[], string>(s.Indices, s.Text)),
                Raw       = Newtonsoft.Json.JsonConvert.SerializeObject(e.Tweet),
                Url       = e.Tweet.Url
            };

            return(tweet);
        }
Ejemplo n.º 11
0
        private async Task saveTweet(MatchedTweetReceivedEventArgs e, string keyword)
        {
            var tweet = formatTweet(e);

            tweet.Keyword = keyword;

            //we use keyword hashcode as partition key, it is assumed to be sparse
            IPersistence persistence = ServiceExtensions.GetPersistenceService(keyword);

            try
            {
                if (e.Tweet.IsRetweet)
                {
                    await persistence.SaveReTweetAsync(tweet);
                }
                else
                {
                    await persistence.SaveTweetAsync(tweet);
                }
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceMessage(this, "Tweet store failed: {0}\r\n\t{1}", e.Tweet.Text, ex.Message);
                throw;
            }
        }
Ejemplo n.º 12
0
        private Task saveTweet(MatchedTweetReceivedEventArgs e)
        {
            Gof.Twitter.Tweet tweet       = formatTweet(e);
            IPersistence      persistence = ServiceExtensions.GetPersistenceService(e.Tweet.CreatedBy.Name);

            return(persistence.SaveRawTweetAsync(tweet));
        }
Ejemplo n.º 13
0
        private void C_ProcessTweet(object sender, MatchedTweetReceivedEventArgs args)
        {
            var message = BuildQueueMessage(args.Tweet);

            _rabbitPublisher.Publish <QueueMessage>(message, _routingKey);
            CheckTimeToUpdateTickers();
        }
        private void OnMatchingTweetReceived(object sender, MatchedTweetReceivedEventArgs args)
        {
            if (_store.UserTracked(args.Tweet.CreatedBy.Id))
            {
                _logger.LogInformation("Received Tweet {0} by user {1}", args.Tweet.Id, args.Tweet.CreatedBy.Id);

                _ = AddAndReadTweet(args.Tweet.Id);
            }
        }
Ejemplo n.º 15
0
 private async void ChangeTextMethodAsync(object sender, MatchedTweetReceivedEventArgs e)
 {
     tweet = "sgdfhj";
     await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                                 () =>
     {
         tweetText.Text = e.Tweet.Text;
         // Update UI
     });
 }
Ejemplo n.º 16
0
        private async void OnTweetReceivedAsync(object sender, MatchedTweetReceivedEventArgs messageEventArgs)
        {
            // ProcessAsync へバイパスするリクエストを行う。
            var res = await botTwitterApiClient.PostAsync("",
                                                          new StringContent(JsonConvert.SerializeObject(messageEventArgs.Tweet.TweetDTO)));

            if ((int)res.StatusCode >= 300)
            {
                throw new IOException($"Tweet process request failed. {res.StatusCode}");
            }
        }
Ejemplo n.º 17
0
        public static void onTweetReceived(Object sebder, MatchedTweetReceivedEventArgs args)
        {
            //if (args.Tweet.Coordinates != null)
            {
                Console.WriteLine("tweet with coords!");
                Console.WriteLine(args.Tweet.Text);
                return;
            }

            Console.WriteLine("no coords");
        }
Ejemplo n.º 18
0
        private static void OnMatchedTweet(object sender, MatchedTweetReceivedEventArgs args)
        {
            var sanitized = sanitize(args.Tweet.FullText); //Sanitize Tweet

            var sentence = new Sentence(sanitized);

            //Output Tweet and Sentiment
            Console.WriteLine(sentence.Sentiment + "|" + args.Tweet);

            //Dispose of Sentence object
            sentence = null;
        }
Ejemplo n.º 19
0
        private IReadOnlyDictionary <string, object> GetBindingData(MatchedTweetReceivedEventArgs value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            Dictionary <string, object> bindingData = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            bindingData.Add("TwitterTrigger", value);

            return(bindingData);
        }
        public async Task ProcessTweetAsync(string track, MatchedTweetReceivedEventArgs args)
        {
            var responseDocument = await MakeRequest(args.Tweet.FullText);

            var tweetSentiment = new TweetSentiment
            {
                FullText = args.Tweet.FullText,
                Score    = responseDocument.Score
            };

            _tweetsRepository.SaveTweet(args);
            _tweetsRepository.SaveSentiment(tweetSentiment);
        }
Ejemplo n.º 21
0
        public async Task StartAsync(string url)
        {
            ITwitterRequest createTwitterRequest()
            {
                var queryBuilder = new StringBuilder(url);

                AddBaseParametersToQuery(queryBuilder);

                var request = _client.CreateRequest();

                request.Query.Url        = queryBuilder.ToString();
                request.Query.HttpMethod = HttpMethod.GET;
                return(request);
            }

            void onJsonReceived(string json)
            {
                RaiseJsonObjectReceived(json);

                if (IsEvent(json))
                {
                    TryInvokeGlobalStreamMessages(json);
                    return;
                }

                var tweet = _factories.CreateTweet(json);

                var detectedTracksAndActions = _streamTrackManager.GetMatchingTracksAndActions(tweet.FullText);
                var detectedTracks           = detectedTracksAndActions.Select(x => x.Item1);

                var eventArgs = new MatchedTweetReceivedEventArgs(tweet, json)
                {
                    MatchingTracks = detectedTracks.ToArray(),
                };

                if (detectedTracksAndActions.Any())
                {
                    eventArgs.MatchOn = MatchOn.TweetText;

                    RaiseTweetReceived(eventArgs);
                    RaiseMatchingTweetReceived(eventArgs);
                }
                else
                {
                    RaiseTweetReceived(eventArgs);
                    RaiseNonMatchingTweetReceived(new TweetEventArgs(tweet, json));
                }
            }

            await _streamResultGenerator.StartAsync(onJsonReceived, createTwitterRequest).ConfigureAwait(false);
        }
        private static void OnMatchedTweet_AnalyseUsing_SimpleNetNlp(object sender, MatchedTweetReceivedEventArgs args)
        {
            Console.WriteLine("**********************************New match tweet received*********************");
            var sanitized = sanitize(args.Tweet.FullText);
            var sentence  = new Sentence(sanitized);

            Console.WriteLine("---------------------------------------");
            Console.WriteLine($"sentence :- {sentence.ToString()}");
            Console.WriteLine("---------------------------------------");
            Console.WriteLine($"tweet :- {args.Tweet}");
            Console.WriteLine("---------------------------------------");
            Console.WriteLine($"Statement's sentiments are :----- {sentence.Sentiment}");
            Console.WriteLine("***********************************************************************");
        }
Ejemplo n.º 23
0
        public Task <ITriggerData> BindAsync(object value, ValueBindingContext context)
        {
            MatchedTweetReceivedEventArgs tweetEvent = value as MatchedTweetReceivedEventArgs;

            if (tweetEvent == null)
            {
                string tweetInfo = value as string;
                tweetEvent = GetEventArgsFromString(tweetInfo);
            }

            IReadOnlyDictionary <string, object> bindingData = GetBindingData(tweetEvent);

            return(Task.FromResult <ITriggerData>(new TriggerData(null, bindingData)));
        }
Ejemplo n.º 24
0
        public async Task ProcessTweetAsync(string keyword, MatchedTweetReceivedEventArgs args)
        {
            var responseDocument = await MakeRequest(args.Tweet.FullText);

            var tweetSentiment = new TweetSentiment
            {
                FullText = args.Tweet.FullText,
                Score    = responseDocument.Score
            };

            Console.WriteLine($"[{DateTime.Now}] - Tweet: {tweetSentiment.FullText}. Sentiment: {tweetSentiment.Score}");

            await _tweetsRepository.SaveTweetAsync(args, keyword, tweetSentiment);
        }
        public void SaveTweet(MatchedTweetReceivedEventArgs args)
        {
            var fields = new Dictionary <string, object>
            {
                { "text", args.Tweet.Text },
                { "screen_name", args.Tweet.CreatedBy.UserIdentifier.ScreenName },
                { "isRetweet", args.Tweet.IsRetweet },
                { "retweetCount", args.Tweet.RetweetCount },
                { "favorited", args.Tweet.Favorited },
                { "favoriteCount", args.Tweet.FavoriteCount },
                { "created_at", args.Tweet.CreatedAt }
            };

            _metricsCollectorWrapper.Write("tweet", fields);
        }
Ejemplo n.º 26
0
        public MatchedTweetReceivedEventArgs GetMatchingTweetEventArgsAndRaiseMatchingElements(ITweet tweet, string json, MatchOn matchOn)
        {
            var matchingTracksEventArgs = new MatchedTweetReceivedEventArgs(tweet, json);

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

            var matchingQuotedTrackAndActions     = new Dictionary <string, Action <ITweet> >();
            var matchingQuotedLocationsAndActions = new Dictionary <ILocation, Action <ITweet> >();
            var matchingQuotedFollowersAndActions = new Dictionary <long, Action <ITweet> >();

            UpdateMatchesBasedOnTweetText(tweet, matchOn, matchingTrackAndActions, matchingTracksEventArgs, matchingQuotedTrackAndActions);
            UpdateMatchesBasedOnUrlEntities(tweet, matchOn, matchingTrackAndActions, matchingTracksEventArgs, matchingQuotedTrackAndActions);
            UpdateMatchesBasedOnHashTagEntities(tweet, matchOn, matchingTrackAndActions, matchingTracksEventArgs, matchingQuotedTrackAndActions);
            UpdateMatchesBasedOnUserMentions(tweet, matchOn, matchingTrackAndActions, matchingTracksEventArgs, matchingQuotedTrackAndActions);
            UpdateMatchesBasedOnSymbols(tweet, matchOn, matchingTrackAndActions, matchingTracksEventArgs, matchingQuotedTrackAndActions);
            UpdateMatchesBasedOnTweetLocation(tweet, matchOn, matchingLocationsAndActions, matchingTracksEventArgs, matchingQuotedLocationsAndActions);
            UpdateMatchesBasedOnTweetCreator(tweet, matchOn, matchingFollowersAndActions, matchingTracksEventArgs, matchingQuotedFollowersAndActions);
            UpdateMatchesBasedOnTweetInReplyToUser(tweet, matchOn, matchingFollowersAndActions, matchingTracksEventArgs, matchingQuotedFollowersAndActions);

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

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

            var matchingQuotedTracks    = matchingQuotedTrackAndActions.Select(x => x.Key).ToArray();
            var matchingQuotedLocations = matchingQuotedLocationsAndActions.Select(x => x.Key).ToArray();
            var matchingQuotedFollowers = matchingQuotedFollowersAndActions.Select(x => x.Key).ToArray();

            matchingTracksEventArgs.QuotedTweetMatchingTracks    = matchingQuotedTracks;
            matchingTracksEventArgs.QuotedTweetMatchingLocations = matchingQuotedLocations;
            matchingTracksEventArgs.QuotedTweetMatchingFollowers = matchingQuotedFollowers;

            var allMatchingTracks    = matchingTrackAndActions.MergeWith(matchingQuotedTrackAndActions);
            var allMatchingLocations = matchingLocationsAndActions.MergeWith(matchingQuotedLocationsAndActions);
            var allMatchingFollowers = matchingFollowersAndActions.MergeWith(matchingQuotedFollowersAndActions);

            CallMultipleActions(tweet, allMatchingTracks.Select(x => x.Value));
            CallMultipleActions(tweet, allMatchingLocations.Select(x => x.Value));
            CallMultipleActions(tweet, allMatchingFollowers.Select(x => x.Value));

            return(matchingTracksEventArgs);
        }
Ejemplo n.º 27
0
        public async Task StartStreamAsync(string url)
        {
            Func <ITwitterQuery> generateTwitterQuery = delegate
            {
                var queryBuilder = new StringBuilder(url);
                AddBaseParametersToQuery(queryBuilder);

                return(_twitterQueryFactory.Create(queryBuilder.ToString(), HttpMethod.GET, Credentials));
            };

            Action <string> generateTweetDelegate = json =>
            {
                RaiseJsonObjectReceived(json);

                var tweet = _tweetFactory.GenerateTweetFromJson(json);
                if (tweet == null)
                {
                    TryInvokeGlobalStreamMessages(json);
                    return;
                }

                var detectedTracksAndActions = _streamTrackManager.GetMatchingTracksAndActions(tweet.Text);
                var detectedTracks           = detectedTracksAndActions.Select(x => x.Item1);

                var eventArgs = new MatchedTweetReceivedEventArgs(tweet)
                {
                    MatchingTracks = detectedTracks.ToArray(),
                };

                if (detectedTracksAndActions.Any())
                {
                    eventArgs.MatchOn = MatchOn.TweetText;

                    RaiseTweetReceived(eventArgs);
                    RaiseMatchingTweetReceived(eventArgs);
                }
                else
                {
                    RaiseTweetReceived(eventArgs);
                    RaiseNonMatchingTweetReceived(new TweetEventArgs(tweet));
                }
            };

            await _streamResultGenerator.StartStreamAsync(generateTweetDelegate, generateTwitterQuery);
        }
Ejemplo n.º 28
0
        public async Task FilteredStream_UrlMatching()
        {
            if (!EndToEndTestConfig.ShouldRunEndToEndTests)
            {
                return;
            }

            var stream = _tweetinviTestClient.Streams.CreateFilteredStream();

            stream.AddTrack("twitter.com");
            stream.AddTrack("facebook.com");
            stream.AddTrack("amazon.com");
            stream.AddTrack("apple.com");

            MatchedTweetReceivedEventArgs matchedTweetEvent = null;

            stream.MatchingTweetReceived += (sender, args) =>
            {
                matchedTweetEvent = args;
                _logger.WriteLine($"Tweet matched via {args.MatchOn.ToString()}");
                _logger.WriteLine(matchedTweetEvent.ToString());
                stream.Stop();
            };

            var runStreamTask = Task.Run(async() =>
            {
                _logger.WriteLine("Before starting stream");
                await stream.StartMatchingAllConditionsAsync();
                _logger.WriteLine("Stream completed");
            });

            // it can take a while before receiving matching tweets
            var delayTask = Task.Delay(TimeSpan.FromMinutes(2));
            var task      = await Task.WhenAny(runStreamTask, delayTask);

            if (task != runStreamTask)
            {
                throw new TimeoutException();
            }

            Assert.Equal(matchedTweetEvent.MatchOn, MatchOn.URLEntities);
            Assert.True(matchedTweetEvent.MatchOn == MatchOn.URLEntities || matchedTweetEvent.QuotedTweetMatchOn == MatchOn.URLEntities);

            await Task.Delay(TimeSpan.FromSeconds(10)); // this is for preventing Enhance Your Calm message from Twitter
        }
Ejemplo n.º 29
0
        public static void printTweet(object sender, MatchedTweetReceivedEventArgs arguments)
        {
            if (arguments.Tweet.Language != Tweetinvi.Core.Enum.Language.English)
            {
                Console.Write(arguments.Tweet.Language.ToString().Substring(0, 3) + "  ");
                return;
            }

            if (arguments.Tweet.Coordinates == null)
            {
                Console.Write("CN  ");
                return;
            }

            // create an object of the data we want to keep
            FileLine current = new FileLine()
            {
                Id = total, TweetId = arguments.Tweet.Id, Latitude = arguments.Tweet.Coordinates.Latitude, Longitude = arguments.Tweet.Coordinates.Longitude, Text = arguments.Tweet.Text
            };

            // url encode the text of the tweet to avoid messy characters in files
            current.Text = HttpUtility.UrlEncode(current.Text);

            // convert to json
            var json = JsonConvert.SerializeObject(current);

            // add to our collection of lines
            lines.Add(json);

            // time to write a file
            if (count > linesPerFile)
            {
                string path = filePath + "\\TweetFile" + fileCount++ + ".tweet";
                File.WriteAllLines(path, lines);
                lines.Clear();
                count = 0;
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("current file count: " + count++ + " total tweet count: " + total++);
            Console.WriteLine();
            Console.WriteLine();
        }
        public void ProcessTweetAsyncTest()
        {
            // Arrange
            const string keyword          = "I am happy!";
            var          tweetsRepository = new Mock <ITweetsRepository>();
            var          configuration    = new Mock <ITextAnalyticsConfiguration>();
            var          handlerMock      = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(
                    "{\r\n\"documents\":[\r\n{\r\n\"score\":0.9999237060546875,\r\n\"id\":\"1\"\r\n}\r\n],\r\n\"errors\":[]\r\n}")
            })
            .Verifiable();

            // use real http client with mocked handler here
            var httpClient = new HttpClient(handlerMock.Object);

            var tweetProcessor = new TweetProcessor(tweetsRepository.Object, configuration.Object, httpClient);
            var tweet          = new Mock <ITweet>();

            tweet.SetupGet(x => x.FullText).Returns(keyword);

            var matchedTweetReceivedEventArgs = new MatchedTweetReceivedEventArgs(tweet.Object, "");

            // Act
            tweetProcessor.ProcessTweetAsync(keyword, matchedTweetReceivedEventArgs).Wait();

            // Assert
            tweetsRepository.Verify(x => x.SaveTweet(matchedTweetReceivedEventArgs));
            tweetsRepository.Verify(x => x.SaveSentiment(It.Is <TweetSentiment>(t =>
                                                                                t.FullText == "I am happy!" && Math.Abs(t.Score - 0.9999237060546875) < 0.01)));
        }