Esempio n. 1
0
        public void OnNext(StreamingMessage value)
        {
            TweetReceived?.Invoke(value.Json);

            // to avoid streaming too much data (try streaming tweets related to Trump lol)
            Thread.Sleep(_interval);
        }
Esempio n. 2
0
        public Task <TweetReceivedSagaData> FindBy(TweetReceived message, SynchronizedStorageSession storageSession, ReadOnlyContextBag context)
        {
            var tweetReceivedSagaData = storageSession.Session().QueryOver <TweetReceivedSagaData>()
                                        .Where(d => d.Hashtag == message.Track)
                                        .SingleOrDefault();

            return(Task.FromResult(tweetReceivedSagaData));
        }
Esempio n. 3
0
 private void OnMatchingTweetReceived(object sender, Tweetinvi.Events.MatchedTweetReceivedEventArgs e)
 {
     logger.LogInformation($"OnMatchingTweetReceived Twitter message received. {JsonConvert.SerializeObject(e)}");
     if (e.Tweet.CreatedBy.Id == _botUser.Id)
     {
         logger.LogInformation("Skip. Tweet is created by bot.");
         return;
     }
     TweetReceived?.Invoke(this, e);
 }
Esempio n. 4
0
 private void OnMatchingTweetReceived(object sender, Tweetinvi.Events.MatchedTweetReceivedEventArgs e)
 {
     Debug.WriteLine("OnMatchingTweetReceived Twitter message received");
     Debug.WriteLine(JsonConvert.SerializeObject(e));
     if (e.Tweet.CreatedBy.Id == _botUser.Id)
     {
         Debug.WriteLine("Skip. Tweet is created by bot.");
         return;
     }
     TweetReceived?.Invoke(this, e);
 }
        public async Task <ActionResult> HandleMentions(
            TweetReceived message,
            [FromServices] DaprClient daprClient)
        {
            logger.LogInformation($"Twitter message from @{message.Tweet.user.screen_name}");
            await daprClient.SaveStateAsync(TweetStoreName, message.Tweet.id_str, message.Tweet);

            ActorId actorId = new ActorId(message.Tag);
            var     proxy   = ActorProxy.Create <ILeaderboardActor>(actorId, "LeaderboardActor");

            var mentions = message.Tweet.entities.user_mentions;

            foreach (UserMention mention in mentions)
            {
                var entry = await daprClient.GetStateEntryAsync <UserMentionCount>(MentionStoreName, mention.id_str, ConsistencyMode.Eventual);

                entry.Value ??= new UserMentionCount {
                    Count = 0, Mention = mention
                };
                entry.Value.Count++;
                await entry.SaveAsync();

                await proxy.SubmitScore(entry.Value.Mention.id_str,
                                        entry.Value.Mention.name, entry.Value.Mention.screen_name,
                                        entry.Value.Count);
            }

            var ranking = await proxy.GetRanking(10);

            var data = new SignalRInvocationData()
            {
                Target    = "leaderboardUpdated",
                Arguments = new List <string>()
            };
            await daprClient.InvokeBindingAsync <SignalRInvocationData>("signalr", "create", data);

            return(Ok());
        }
Esempio n. 6
0
        public async Task Handle(TweetReceived message, IMessageHandlerContext context)
        {
            if (Data.PreviousSessionId != Guid.Empty && Data.PreviousSessionId != message.SessionId)
            {
                ColorConsole.WriteLine(
                    $" {message.Track} ".DarkCyan().OnWhite(),
                    " ",
                    "needs catch up from tweet".Gray(),
                    " ",
                    $"{Data.PreviousTweetId}".White());

                await context.Send(new StartCatchUp
                {
                    TweetId = Data.PreviousTweetId,
                    Track   = message.Track,
                })
                .ConfigureAwait(false);
            }

            Data.PreviousSessionId = message.SessionId;
            Data.Hashtag           = message.Track;
            Data.PreviousTweetId   = message.TweetId;
        }
 private void OnMessageReceived(object sender, Tweetinvi.Events.MessageEventArgs e)
 {
     System.Diagnostics.Debug.WriteLine("Twitter message received");
     TweetReceived?.Invoke(this, e);
 }
Esempio n. 8
0
        public static async Task StartAsync(
            IEndpointInstance endpointInstance,
            string track,
            string consumerKey,
            string consumerSecret,
            string accessToken,
            string accessTokenSecret)
        {
            var credentials = new TwitterCredentials(consumerKey, consumerSecret, accessToken, accessTokenSecret);

            while (true)
            {
                try
                {
                    var stream = Stream.CreateFilteredStream(credentials);
                    stream.AddTrack(track);

                    var sessionId = Guid.NewGuid();
                    stream.StreamStarted += (sender, args) =>
                    {
                        sessionId = Guid.NewGuid();
                        ColorConsole.WriteLine(
                            $"{DateTime.UtcNow.ToLocalTime()}".DarkGray(),
                            " ",
                            $" {track} ".DarkCyan().OnWhite(),
                            " ",
                            "stream started with session ID".Gray(),
                            " ",
                            $"{sessionId}".White());
                    };

                    stream.StreamStopped += (sender, args) => ColorConsole.WriteLine(
                        $"{DateTime.UtcNow.ToLocalTime()} ".DarkGray(),
                        $" {track} ".DarkCyan().OnWhite(),
                        " stream stopped.".Red(),
                        args.Exception == null ? string.Empty : $" {args.Exception.Message}".DarkRed());

                    stream.MatchingTweetReceived += async(sender, e) =>
                    {
                        var analyzeTweet = TweetMapper.Map(e.Tweet, track);
                        Writer.Write(analyzeTweet.Tweet);
                        await endpointInstance.Send(analyzeTweet)
                        .ConfigureAwait(false);

                        var tweetReceived = new TweetReceived()
                        {
                            SessionId = sessionId,
                            Track     = track,
                            TweetId   = e.Tweet.Id
                        };
                        await endpointInstance.Publish(tweetReceived)
                        .ConfigureAwait(false);
                    };

                    await stream.StartStreamMatchingAnyConditionAsync();
                }
                catch (Exception ex)
                {
                    ColorConsole.WriteLine($"{DateTime.UtcNow.ToLocalTime()} ".DarkGray(), "Error listening to Twitter stream.".Red(), $" {ex.Message}".DarkRed());
                    Thread.Sleep(1000);
                }
            }
        }