public TwitterWorker(TweetStorage storage)
        {
            _storage = storage;

            _stream = Stream.CreateFilteredStream();
            _stream.MatchingTweetAndLocationReceived += _stream_MatchingTweetAndLocationReceived;
        }
Пример #2
0
        private static void Stream_SampleStreamExample(int index)
        {
            int count = 0;

            string[] codes = SearchTest.Credentials.Code;
            var      pass  = Auth.SetUserCredentials(codes[5 * index + 3],
                                                     codes[5 * index + 4],
                                                     codes[5 * index + 1],
                                                     codes[5 * index + 2]);

            var stream = Stream.CreateSampleStream();

            stream.TweetReceived += (sender, args) =>
            {
                var tweet = args.Tweet;
                if (tweet.Language == Language.English)
                {
                    StringBuilder b = new StringBuilder();
                    b.Append(tweet.CreatedAt);
                    b.Append("\t");
                    b.Append(tweet.Text.Replace('\n', ' '));
                    b.Append("\t");
                    b.Append(tweet.IsRetweet);
                    b.Append("\t");
                    b.Append(tweet.Retweeted);
                    b.Append("\t");
                    b.Append(tweet.RetweetCount);
                    b.Append("\t");
                    //if (count++ % 10000 == 0)
                    Console.Out.WriteLine(count++);
                    DateTime time     = DateTime.Now;
                    string   fileName = time.ToShortDateString().Replace('/', '-');
                    File.AppendAllText(@"..\..\Data\" + fileName + "_" + index + ".txt", b.ToString());
                }
                //Console.WriteLine(args.Tweet.Text);
            };

            stream.LimitReached += (sender, args) =>
            {
                Console.Out.WriteLine("LimitReached" + DateTime.Now);
            };
            stream.DisconnectMessageReceived += (sender, args) =>
            {
                Console.Out.WriteLine("DisconnectMessageReceived" + DateTime.Now);
            };
            stream.StreamStopped += (sender, args) =>
            {
                if (args.DisconnectMessage != null)
                {
                    Console.Out.WriteLine(args.DisconnectMessage.Reason);
                }
            };

            while (true)
            {
                stream.StartStream();
                Console.Out.WriteLine("End" + DateTime.Now);
            }
        }
Пример #3
0
        public async Task FunctionHandler(State state, ILambdaContext context)
        {
            Auth.SetUserCredentials(
                LambdaConfiguration.Configuration["TwitterConsumerKey"],
                LambdaConfiguration.Configuration["TwitterConsumerSecret"],
                LambdaConfiguration.Configuration["TwitterUserAccessToken"],
                LambdaConfiguration.Configuration["TwitterUserAccessSecret"]
                );

            // Using the sample stream
            var stream = Stream.CreateSampleStream();
            //stream.AddTweetLanguageFilter(LanguageFilter.English);
            //stream.FilterLevel = Tweetinvi.Streaming.Parameters.StreamFilterLevel.Low;

            int counter = 0;

            stream.TweetReceived += async(sender, t) =>
            {
                // Skip retweets
                if (t.Tweet.IsRetweet)
                {
                    return;
                }

                if (counter > state.Count)
                {
                    stream.StopStream();
                }

                //var json = JsonConvert.SerializeObject(new
                //{
                //    avatar = t.Tweet.CreatedBy.ProfileImageUrl400x400,
                //    text = t.Tweet.Text
                //});

                await StreamToKinisis(t.Json).ConfigureAwait(false);

                context.Logger.LogLine(t.Json);
                //Console.WriteLine(t.Tweet.FullText);
                counter++;
            };

            if (state.Status == "Start")
            {
                stream.StartStream();
            }
            else if (state.Status == "Pause")
            {
                stream.PauseStream();
            }
            else if (state.Status == "Stop")
            {
                stream.StopStream();
            }
        }
Пример #4
0
        private void Produce()
        {
            _Stream = Stream.CreateSampleStream();

            _Stream.TweetReceived += (object sender, Tweetinvi.Events.TweetReceivedEventArgs e) =>
            {
                _TargetBuffer.Post(e.Tweet);
            };

            _Stream.AddTweetLanguageFilter(LanguageFilter.English);

            _Stream.StartStreamAsync();
        }
Пример #5
0
        /// <summary>
        /// Start a stream, filtering ony the keyword and only English language tweets.
        /// </summary>
        protected void StartStream(string keyword)
        {
            stream = Stream.CreateFilteredStream();
            stream.AddTrack(keyword);
            stream.MatchingTweetReceived += (sender, args) =>
            {
                if (args.Tweet.Language == Language.English)
                {
                    UpdateFdg(args.Tweet.Text);
                }
            };

            stream.StartStreamMatchingAllConditionsAsync();
        }
Пример #6
0
        //--- Constructors ---
        public TwitterStream()
        {
            var            region = RegionEndpoint.GetBySystemName(AWS_REGION);
            var            chain  = new CredentialProfileStoreChain();
            AWSCredentials awsCredentials;

            if (!chain.TryGetAWSCredentials("lambdasharp", out awsCredentials))
            {
                throw new Exception("AWS Credentials not found!");
            }
            _client = new AmazonCloudWatchLogsClient(awsCredentials, region);
            SetTwitterCredentials();
            _stream = Stream.CreateFilteredStream();
        }
Пример #7
0
    //Open listening connecting to twitter account
    public void listenAndProcess(long vQuestionTweetID, cQuestion vQuestion)
    {
        BlockingCollection <cTweet> correctAnswers = new BlockingCollection <cTweet>(1000);
        cTriggerPyramid             triggerPyramid = new cTriggerPyramid();
        // An action to consume the ConcurrentQueue.
        Action processQueue = () =>
        {
            System.Diagnostics.Debug.WriteLine("before extracting tweet");
            while (true)
            {
                cTweet tempAnswer = correctAnswers.Take();
                if (isTweetReply(tempAnswer, (ulong)vQuestionTweetID))
                {
                    long answerTweetID = tempAnswer.getID();
                    if (tempAnswer.getText().Contains(vQuestion.correctAnswer))
                    {
                        Tweetinvi.Tweet.PublishTweetInReplyTo("@" + tempAnswer.getUserAccount() + " That's Correct! " +
                                                              vQuestion.followUpMessage, answerTweetID);
                    }
                    //trigger pyramid
                }
            }
        };

        // Start 1 concurrent consuming actions.
        Task.Run(processQueue);
        // Parallel.Invoke(action);

        Tweetinvi.Auth.SetUserCredentials(consumerKey, consumerSecret, accessToken, accessTokenSecret);
        var stream = Stream.CreateUserStream();

        stream.TweetCreatedByAnyoneButMe += (sender, args) =>
        {
            //When tweet received, evaluate to determine if it is an answer to a question
            //send return tweet
            //validate answer
            //add validated successful answer

            correctAnswers.TryAdd(new cTweet(args.Tweet.Id, args.Tweet.Text, args.Tweet.CreatedAt, args.Tweet.CreatedBy,
                                             args.Tweet.InReplyToStatusId));
            System.Diagnostics.Debug.WriteLine("A question reply has been found; the tweet is '" + args.Tweet + "'");
        };

        System.Diagnostics.Debug.WriteLine("before starting stream");


        stream.StartStream();
        System.Diagnostics.Debug.WriteLine("after starting stream before while");
    }
Пример #8
0
        public static void StartFilteredTweetStream(IActorRef actor, string track, LanguageFilter langFilter = LanguageFilter.English)
        {
            var stream = Stream.CreateFilteredStream();

            stream.AddTrack(track);
            stream.AddTweetLanguageFilter(langFilter);

            stream.MatchingTweetReceived += (_, arg) =>
            {
                arg.Tweet.Text = arg.Tweet.Text.Replace("\r", " ").Replace("\n", " ");
                SaveTweet(arg.Tweet);
                actor.Tell(arg.Tweet);
            };
            stream.StartStreamMatchingAnyCondition();
        }
        public TtsBot(ILogger <TtsBot> logger, Option option, AdminAccess.AdminAccess.AdminAccessClient grpcClient, DataStore store)
        {
            _option       = option ?? throw new ArgumentNullException(nameof(option));
            _grpcClient   = grpcClient ?? throw new ArgumentNullException(nameof(grpcClient));
            _store        = store ?? throw new ArgumentNullException(nameof(store));
            _logger       = logger ?? throw new ArgumentNullException(nameof(logger));
            _tweetIdRegex = new Regex(@"\/(?<tweetId>\d+)");

            if (option.GoogleUseEnvironmentVariable)
            {
                _ttsClient = TextToSpeechClient.Create();
            }
            else
            {
                if (string.IsNullOrWhiteSpace(_option.GoogleApplicationCredentialsPath))
                {
                    throw new ArgumentNullException(nameof(_option.GoogleApplicationCredentialsPath), "Credential path cannot be empty if not using Enviornment Variable \"GOOGLE_APPLICATION_CREDENTIALS\"");
                }

                TextToSpeechClientBuilder builder = new TextToSpeechClientBuilder();
                builder.CredentialsPath = _option.GoogleApplicationCredentialsPath;

                _ttsClient = builder.Build();
            }

            _discord      = new DiscordSocketClient();
            _discord.Log += obj =>
            {
                _logger.LogDebug("{0}", obj);
                return(Task.CompletedTask);
            };

            _userCredentials = Auth.CreateCredentials(option.TwitterConsumerKey,
                                                      option.TwitterConsumerSecret,
                                                      option.TwitterUserAccessToken,
                                                      option.TwitterUserAccessSecret);
            Auth.SetCredentials(_userCredentials);

            _stream = TwitterStream.CreateFilteredStream(_userCredentials, TweetMode.Extended);
            _stream.MatchingTweetReceived += OnMatchingTweetReceived;

            foreach (var user in _store.Users)
            {
                _stream.AddFollow(user.Id);
            }

            _store.UsersChanged += OnUserAddedToDataStore;
        }
Пример #10
0
        public static void StartTweetStream(IActorRef actor)
        {
            var stream = Stream.CreateSampleStream();

            stream.TweetReceived += (_, arg) =>
            {
                if (arg.Tweet.Coordinates != null)
                {
                    arg.Tweet.Text = arg.Tweet.Text.Replace("\r", " ").Replace("\n", " ");
                    var json = JsonConvert.SerializeObject(arg.Tweet);
                    File.AppendAllText("tweets.txt", $"{json}\r\n");
                    actor.Tell(arg.Tweet);
                }
            };
            stream.StartStream();
        }
Пример #11
0
        //--- Constructors ---
        public TwitterStream()
        {
            var            region = RegionEndpoint.GetBySystemName(AWS_REGION);
            var            chain  = new CredentialProfileStoreChain();
            AWSCredentials awsCredentials;

            if (!chain.TryGetAWSCredentials("lambdasharp", out awsCredentials))
            {
                throw new Exception("AWS Credentials not found!");
            }
            _client = new AmazonCloudWatchLogsClient(awsCredentials, region);
            SetTwitterCredentials();
            _stream = Stream.CreateFilteredStream();

            // Read application vars
            _applicationVars = JsonConvert.DeserializeObject <ApplicationVars>(File.ReadAllText("application_vars.json"));
        }
Пример #12
0
        public static void StartSampleTweetStream(IActorRef actor, LanguageFilter langFilter = LanguageFilter.English)
        {
            var stream = Stream.CreateSampleStream();

            stream.AddTweetLanguageFilter(langFilter);

            stream.TweetReceived += (_, arg) =>
            {
                if (arg.Tweet.Coordinates != null)
                {
                    arg.Tweet.Text = arg.Tweet.Text.Replace("\r", " ").Replace("\n", " ");
                    SaveTweet(arg.Tweet);

                    actor.Tell(arg.Tweet);
                }
            };
            stream.StartStream();
        }
Пример #13
0
        /// <summary>
        /// Producer. Streams tweets from twiter sample stream and post for consumer to process.
        /// </summary>
        /// <param name="target"></param>
        public void Produce(ITargetBlock <ITweet> target)
        {
            // Create Twitter sample stream (1% of all public tweets)
            _Stream = Stream.CreateSampleStream();

            _Stream.TweetReceived += (object sender, Tweetinvi.Events.TweetReceivedEventArgs e) => {
                target.Post(e.Tweet);
            };

            _Stream.AddTweetLanguageFilter(LanguageFilter.English);

            // Begin streaming from twitter sample stream
            _Stream.StartStream();

            // Set the target to the completed state to signal to the consumer
            // that no more data will be available
            target.Complete();
        }
Пример #14
0
        public static void StartFilteredTweetStream(IActorRef actor, string track, LanguageFilter langFilter = LanguageFilter.English)
        {
            var stream = Stream.CreateFilteredStream();

            //var centerOfNewYork = new Location(new Coordinates(-75,38), new Coordinates(-71,43));
            //stream.AddLocation(centerOfNewYork);

            stream.AddTrack(track);
            stream.AddTweetLanguageFilter(langFilter);

            stream.MatchingTweetReceived += (_, arg) =>
            {
                arg.Tweet.Text = arg.Tweet.Text.Replace("\r", " ").Replace("\n", " ");
                SaveTweet(arg.Tweet);
                actor.Tell(arg.Tweet);
            };
            stream.StartStreamMatchingAnyCondition();
        }
Пример #15
0
        static void Main(string[] arg)
        {
            Auth.SetUserCredentials("", "", "", "");//Enter your Twitter Credentials

            ExceptionHandler.SwallowWebExceptions = false;
            _sampleStream = Stream.CreateSampleStream(Auth.Credentials);
            _sampleStream.AddTweetLanguageFilter(Tweetinvi.Core.Enum.Language.English);
            _sampleStream.TweetReceived += (sender, args) =>
            {
                SqlInsert(args.Tweet);
            };
            _sampleStream.StreamStarted += (sender, args) =>
            {  };

            _sampleStream.StreamStopped += (sender, args) =>
            {
                var exception         = args.Exception;
                var disconnectMessage = args.DisconnectMessage;

                if (exception != null)
                {
                    Console.WriteLine(exception.ToString());
                }
                if (disconnectMessage != null)
                {
                    Console.WriteLine(disconnectMessage.ToString());
                }
            };
            while (true)
            {
                try
                {
                    new Program().RunTwitter().Wait();
                }
                catch (AggregateException ex)
                {
                    foreach (var e in ex.InnerExceptions)
                    {
                        Console.WriteLine("Error: " + e.Message);
                    }
                }
            }
        }
Пример #16
0
        public static void GetFilteredStream(string searchTerm)
        {
            var stream = Stream.CreateFilteredStream();

            stream.AddTrack(searchTerm);

            int           tweetCount = 0;
            List <string> tweets     = new List <string>();

            stream.MatchingTweetReceived += (sender, currentTweet) =>
            {
                tweetCount++;
                tweets.Add(currentTweet.Tweet.FullText);
                //Console.WriteLine("A tweet containing 'tweetinvi' has been found; the tweet is '" + currentTweet.Tweet.FullText + "'");
                if (tweetCount >= 5)
                {
                    stream.StopStream();
                }
            };

            stream.StartStreamMatchingAllConditions();

            File.WriteAllLines("FilterTweets.txt", tweets);
        }
Пример #17
0
        public TweetService(DiscordClient client, Config config, IEventLogger logger)
        {
            _client = client;
            _config = config;
            _logger = logger;

            _timer = new Timer {
                Interval = 1000 * 60 * 5
            };
            _timer.Elapsed += MinuteTimerEventHandler;

            var creds = SetCredentials(_config.TwitterUpdates);

            _twitterStream                               = Stream.CreateFilteredStream(creds);
            _twitterStream.Credentials                   = creds;
            _twitterStream.StallWarnings                 = true;
            _twitterStream.FilterLevel                   = StreamFilterLevel.None;
            _twitterStream.StreamStarted                += (sender, e) => _logger.Debug("Successfully started.");
            _twitterStream.StreamStopped                += (sender, e) => _logger.Debug($"Stream stopped.\r\n{e.Exception}\r\n{e.DisconnectMessage}");
            _twitterStream.DisconnectMessageReceived    += (sender, e) => _logger.Debug($"Disconnected.\r\n{e.DisconnectMessage}");
            _twitterStream.WarningFallingBehindDetected += (sender, e) => _logger.Debug($"Warning Falling Behind Detected: {e.WarningMessage}");

            CheckTwitterFollows();
        }
Пример #18
0
    //Open listening connecting to twitter account
    public void listenAndProcess(long vQuestionTweetID, cQuestion vQuestion)
    {
        BlockingCollection <cTweet> correctAnswers = new BlockingCollection <cTweet>(1000);
        cTriggerPyramid             triggerPyramid = new cTriggerPyramid();
        // An action to consume the ConcurrentQueue.
        Action processQueue = () =>
        {
            while (true)
            {
                cTweet tempAnswer = correctAnswers.Take();
                System.Diagnostics.Debug.WriteLine("Evaluating TweetID: " + tempAnswer.getID().ToString());
                //evaluate to determine if it is an answer to a question
                if (isTweetReply(tempAnswer, (ulong)vQuestionTweetID))
                {
                    long answerTweetID = tempAnswer.getID();
                    //validate answer, send return tweet
                    String correctAnswerString = null;
                    switch (vQuestion.correctAnswer)
                    {
                    case 1:
                        correctAnswerString = vQuestion.answer1;
                        break;

                    case 2:
                        correctAnswerString = vQuestion.answer2;
                        break;

                    case 3:
                        correctAnswerString = vQuestion.answer3;
                        break;

                    case 4:
                        correctAnswerString = vQuestion.answer4;
                        break;

                    default:
                        correctAnswerString = "ERROR";
                        break;
                    }
                    if (tempAnswer.getText().ToUpper().Contains(correctAnswerString.ToUpper()))
                    {
                        String correctResponse = null;
                        correctResponse = "@" + tempAnswer.getUserAccount() + " That's Correct! " +
                                          vQuestion.followUpMessage;
                        Tweetinvi.Tweet.PublishTweetInReplyTo(correctResponse, answerTweetID);
                        System.Diagnostics.Debug.WriteLine("Response Sent: " + correctResponse);
                        //trigger pyramid
                        System.Diagnostics.Debug.WriteLine("Triggering Pyramid");
                        cTriggerPyramid triggerPyramidOBJ = new cTriggerPyramid();
                        //for local execution
                        //triggerPyramidOBJ.runPython_cmd("[email protected]", "python activatetrigger.py " + vQuestion.pyramidScene);
                        //for server execution
                        //triggerPyramidOBJ.runPython_cmd("-i /var/pi/pi_privatekey pi@localhost -p 19999 nohup", "python activatetrigger.py " + vQuestion.pyramidScene);
                        //pulling from config file
                        triggerPyramidOBJ.runPython_cmd(configHelper.pyramidTrigger(), configHelper.pyramidScript() + " " + vQuestion.pyramidScene);
                    }
                    else
                    {
                        String incorrectResponse = null;
                        incorrectResponse = "@" + tempAnswer.getUserAccount() + " Oops, that's not it. Try again!";
                        Tweetinvi.Tweet.PublishTweetInReplyTo(incorrectResponse, answerTweetID);
                        System.Diagnostics.Debug.WriteLine("Response Sent: " + incorrectResponse);
                    }
                }
            }
        };

        // Start 1 concurrent consuming actions.
        Task.Run(processQueue);

        Tweetinvi.Auth.SetUserCredentials(configHelper.consumerKey(), configHelper.consumerSecret(), configHelper.accessToken(), configHelper.accessTokenSecret());
        var stream = Stream.CreateUserStream();

        stream.TweetCreatedByAnyoneButMe += (sender, args) =>
        {
            //When tweet received, add to blocking queue
            correctAnswers.TryAdd(new cTweet(args.Tweet.Id, args.Tweet.Text, args.Tweet.CreatedAt, args.Tweet.CreatedBy,
                                             args.Tweet.InReplyToStatusId));
            System.Diagnostics.Debug.WriteLine("Incoming tweet. " + args.Tweet);
        };

        try
        {
            stream.StartStream();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
Пример #19
0
        public static void Main(string[] args)
        {
            string instanceUrl = "https://Hackathon-Sitecore-Tracking-Collection-Service.local.com";
            //string channelId = "6d3d2374-af56-44fe-b99a-20843b440b58";
            string channelId = "6d3d2374-af56-44fe-b99a-20843b440b58";         // Channel Id of the channel: /sitecore/system/Marketing Control Panel/Taxonomies/Channel/Online/Social community/Twitter social community
            //string definitionId = "55555555-d662-4a87-beee-413307055c48";
            string eventDefinitionId = "ff1d9360-8a31-4779-ad1a-d4a8178b865a"; // Goal definitionId of the goal: /sitecore/system/Marketing Control Panel/Goals/Sandbox/Twitter Hackathon

            Auth.SetUserCredentials(TwitterApiKey, TwitterApiSecretKey, TwitterAccessToken, TwitterAccessTokenSecret);
            var user = User.GetAuthenticatedUser();

            // Get all twitter filters and put them in a list
            var filtersFromSitecore = GetFilters();

            // Use the channel from the filter.
            var hashTags               = filtersFromSitecore.Select(p => p.Product_Hashtag.Trim()).Distinct();
            var hashtagString          = string.Join(" OR ", hashTags);
            var accountAgeFilter       = filtersFromSitecore.Select(p => p.Twitter_Account_Age).Distinct();
            var filterOutRetweets      = filtersFromSitecore.FirstOrDefault(p => p.Filter_Out_Retweets);
            var filterMinimumFollowers = filtersFromSitecore.Select(p => p.Minimum_Followers).Distinct();

            System.Console.WriteLine(user);

            var stream = Stream.CreateFilteredStream();

            //stream.AddTrack("\ud83d\udd25"); //fire emoji
            hashTags.ForEach(ht => stream.AddTrack(ht));
            stream.AddTweetLanguageFilter(LanguageFilter.English);

            System.Console.WriteLine("I am listening to Twitter for Product Hashtags:" + hashtagString + "...");

            stream.MatchingTweetReceived += (sender, arguments) =>
            {
                var theTweet = arguments.Tweet;

                //stringify hashtags
                var hashtags      = theTweet.Hashtags.ToList();
                var tweetHashtags = hashtags.Count > 0 ? string.Join(", ", hashtags) : "Not Available";

                // Name
                var name = theTweet.CreatedBy.Name;

                // Name
                var twitterHandle = theTweet.CreatedBy.ScreenName;

                // Number of followers
                var followersCount = theTweet.CreatedBy.FollowersCount;

                // Account Description
                var twitterHandleDescription = theTweet.CreatedBy.Description ?? "Not Available";

                // Created At - Account
                var twitterHandleCreated = theTweet.CreatedBy.CreatedAt;

                // Tweet Full Text
                var tweetFullText = theTweet.FullText;

                // Is Retweet
                var isRetweet = theTweet.IsRetweet;

                // var verified
                var isVerified = theTweet.CreatedBy.Verified;

                System.Console.WriteLine(theTweet.FullText);
                System.Console.WriteLine(hashtagString);
                System.Console.WriteLine(theTweet.CreatedBy);
                System.Console.WriteLine("-------------------------------");
                System.Console.WriteLine();

                if (!theTweet.Retweeted)
                {
                    var defaultInteraction = UTEntitiesBuilder.Interaction()
                                             .ChannelId(channelId)
                                             .Initiator(InteractionInitiator.Contact)
                                             .Contact("twitter", twitterHandle)
                                             .CampaignId("33333333-d662-4a87-beee-413307055c48")
                                             .VenueId("44444444-d662-4a87-beee-413307055c48")
                                             .UserAgent("ConsoleApp not a browser")
                                             .Build();
                    using (var session = SitecoreUTSessionBuilder.SessionWithHost(instanceUrl)
                                         .DefaultInteraction(defaultInteraction)
                                         .TokenValue("mytokenvalue")
                                         .BuildSession()
                           )
                    {
                        var dict = new Dictionary <string, string>();

                        dict.Add("twitterhandle", "@" + twitterHandle);
                        dict.Add("hashtags", tweetHashtags);
                        dict.Add("name", name);
                        dict.Add("followerscount", followersCount.ToString());
                        dict.Add("twitterhandledescription", twitterHandleDescription);
                        dict.Add("twitterhandlecreated", twitterHandleCreated.ToShortDateString());
                        dict.Add("tweetfulltext", tweetFullText);
                        dict.Add("isretweet", isRetweet.ToString());
                        dict.Add("isverified", isVerified.ToString());
                        dict.Add("accountagefilter", accountAgeFilter.ToString());
                        dict.Add("filteroutretweets", filterOutRetweets.ToString());
                        dict.Add("filterminimumfollowers", filterMinimumFollowers.ToString());

                        var eventRequest = UTRequestBuilder.EventWithDefenitionId(eventDefinitionId)
                                           .Timestamp(DateTime.Now)
                                           .AddCustomValues(dict)
                                           .Text(theTweet.FullText)
                                           .Build();
                        var eventResponse = session.TrackEventAsync(eventRequest);
                        System.Console.WriteLine("Track EVENT RESULT: " + eventResponse.Result.StatusCode);
                    }
                }
            };

            stream.StartStreamMatchingAllConditions();

            System.Console.ReadLine();
        }
Пример #20
0
        private static void RunTwitterApplication()
        {
            // BLOCK 1 : Twitter credentials
            // Twitter Credentials: Reading in a json configuration file with both credentials for the twitter integration
            // and the dedicated twitter account name used for "weather sense"
            JObject TwitterConfiguration = JObject.Parse(File.ReadAllText(RootDirectory + @"Package\Config_Interfaces\Twitter_context_profile.json"));
            // Locally reading and storing all credential values
            string consumer_key        = (string)TwitterConfiguration["consumer_key"];
            string consumer_secret     = (string)TwitterConfiguration["consumer_secret"];
            string access_token        = (string)TwitterConfiguration["access_token"];
            string access_token_secret = (string)TwitterConfiguration["access_token_secret"];
            string sensor_account      = (string)TwitterConfiguration["sensor_account"];

            // Setting twitter credentials
            TwitterCredentials.SetCredentials(access_token, access_token_secret, consumer_key, consumer_secret);



            // BLOCK 2: Populating Landmarks
            // Import excel worksheet for landmark initialization (Name, Categories, Hashtag_trackers, Thematic_Metatags)
            // Reading excel worksheet using exceldatareader nuget package
            FileStream       stream      = File.Open(RootDirectory + @"Package\Config_Interfaces\Landmarks_Categories.xlsx", FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            DataSet          result      = excelReader.AsDataSet();
            // This should initialize all the parameters in the Lanmarks object array using its contructor
            int numberoflandmarks = result.Tables[0].Rows.Count - 1;

            Landmark[] Landmarks = new Landmark[numberoflandmarks];
            for (int i = 0; i < numberoflandmarks; i++)
            {
                Landmarks[i] = new Landmark(result.Tables[0].Rows[i]);
            }


            //BLOCK 3: Hook population
            // Reading in datatable which contains hooks and their properties

            int numberofhooks = result.Tables[1].Rows.Count - 1;

            Hook[] Hooks = new Hook[numberofhooks];
            for (int i = 0; i < numberofhooks; i++)
            {
                Hooks[i] = new Hook(result.Tables[1].Rows[i]);
            }


            ////BLOCK 4: Twitter Stream trackers
            //// Creating twitter stream for detecting tweets with landmark names
            var stream_for_landmark = Stream.CreateFilteredStream();

            // Adding trackers for each landmark
            foreach (Landmark obj in Landmarks)
            {
                string[] landmark_hashtags_store = obj.Hashtag_trackers;
                // Loop block to assign trackers to each relevant hashtag element in the twitter stream
                foreach (string hashtag in landmark_hashtags_store)
                {
                    stream_for_landmark.AddTrack(hashtag);
                }
            }

            //var stream_for_landmark = Stream.CreateFilteredStream();
            //stream_for_landmark.AddTrack("volkshotel");

            // BLOCK 5: Landing zone and context aware parameters
            LandingZone landingzone = new LandingZone();

            char MorningOrAfternoon = landingzone.MorningOrAfternoon();
            char Weather            = Sensors.WeatherSense.SunnyOrCloudy(sensor_account);


            //BLOCK 5: Event trigger for tracked tweets

            // Event trigger block for each new tweet on the above location based landmark triggers
            // String array to store all different weather conditions

            // Hook module : Time of the day - categorize as morning, afternoon and evening
            // Hook is overlayed on the video - in the center of the video

            // Video module : Videos are named as landmark and weather
            // Weather detection module : Must read through twitter stream of test account -> Assign a hastag weather -> could filter out the

            List <Hook> HooksforExcel = Hooks.ToList <Hook>();

            //ExcelFileWriter<Hook> myExcel = new ExcelWrite();
            //myExcel.WriteDateToExcel(@"C:\TEMP\myExcel.xls", HooksforExcel, "A1", "D1");

            // Landmark is detected by trackers
            // Landmark is pushed to method for extracting content from DB
            // This can happen either here or in openframeworks
            // Once content is extracted it must be sequenced on the basis of :
            // Time to play each item : Once one item is played for a certain preset amount of time extract the next from DB
            // Time calculation in openframeworks:
            // Update loop and draw loop

            // The sensing modules exist on this side. So Hooks are selected here and published onto the interface between OF and Encounter#
            // Hooks are read by OF and sequenced according to time parameters in OF

            //Two kinds of time parameters in OF:
            // Amount of time video must be played for
            // Amount of time a hook should be shown for

            // How does timing fit into the update and draw cycles?
            // Use a timer, counting seconds until a value is reached
            // Validation for time count can be executed at every loop update

            // Update should contain a timing module where all the timing logic can be placed

            // Objects required in OF
            // OF has to handle video placement, playback and sequencing
            // OF has to handle Hook placement, sequencing and timing
            // OF has to handle visual blocks with animated elements


            Console.WriteLine("Stream detection live now:");
            stream_for_landmark.MatchingTweetReceived += (sender, arg) =>
            {
                // Storing the tweet
                string tweet = arg.Tweet.ToString();

                // Searching through tweet to find relevant landmark
                MatchedLandmark detectedlandmark = Find_tweeted_landmark(Landmarks, tweet);

                // We ignore the landmark-specific categories, hooks are categorized by landmark + wildcard humor category.
                string[] relevantHookCategories = new string[] { detectedlandmark.Landmark.Name, "humor" }; // detectedlandmark.Landmark.Categories;

                // Writing detected landmark to text file interface between here and OF
                using (StreamWriter writer = new StreamWriter(RootDirectory + @"Package\Config_Interfaces\TriggeredLandmark.txt", false))
                {
                    writer.Write("{0}\r\n{1}", detectedlandmark.Landmark.Name, detectedlandmark.Hashtag);
                }

                List <Hook> RelevantHooks = new List <Hook>();

                // Selecting hooks that belong to the relevant categories corresponding to the landmark - Obsolete
                foreach (string category in relevantHookCategories)
                {
                    RelevantHooks.AddRange((from item in Hooks
                                            where item.category == category
                                            select item).ToArray());
                }

                // Storing current sensor values
                MorningOrAfternoon = landingzone.MorningOrAfternoon();
                Weather            = Sensors.WeatherSense.SunnyOrCloudy(sensor_account);

                // Further filtering hooks based on sensor values
                RelevantHooks = (from hook in RelevantHooks
                                 where
                                 (hook.timeofday.Contains(MorningOrAfternoon) || String.IsNullOrEmpty(hook.timeofday)) &&
                                 (hook.weather.Contains(Weather) || String.IsNullOrEmpty(hook.weather))
                                 select hook).ToList();

                // Writing the selected hooks to an XML to be read in by OF
                using (XmlWriter writer = XmlWriter.Create(RootDirectory + @"of_v0.8.4_vs_release\apps\myApps\OpenEncounters\bin\data\Hooks.xml"))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Filtered_hooks");
                    foreach (var item in RelevantHooks)
                    {
                        writer.WriteStartElement("Hook");
                        writer.WriteElementString("text", item.text);
                        writer.WriteElementString("category", item.category);
                        writer.WriteElementString("weather", item.weather);
                        writer.WriteElementString("timeofday", item.timeofday);
                        writer.WriteElementString("font", item.font);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }

                Console.WriteLine(tweet);
                Console.WriteLine();
                Console.Write("Landmark Triggered:");
                Console.Write(detectedlandmark.Landmark.Name);
                Console.WriteLine();

                //System.Threading.Thread.Sleep(
                //(int)System.TimeSpan.FromSeconds(30).TotalMilliseconds);
            };

            // Starting twitter stream detection
            stream_for_landmark.StartStreamMatchingAllConditions();
        }