Example #1
0
        public async Task StartAsync()
        {
            _eventHubClient = EventHubClient.CreateFromConnectionString(_targetConfig.ConnectionString, _targetConfig.EventHubName);

            Auth.SetUserCredentials(_sourceConfig.Credentials.ConsumerKey, _sourceConfig.Credentials.ConsumerSecret, _sourceConfig.Credentials.AccessToken, _sourceConfig.Credentials.AccessTokenSecret);

            IFilteredStream stream = Stream.CreateFilteredStream();

            foreach (var language in _sourceConfig.Languages)
            {
                stream.AddTweetLanguageFilter((LanguageFilter)language);
            }

            foreach (string track in _sourceConfig.Tracks)
            {
                stream.AddTrack(track);
            }

            stream.MatchingTweetReceived     += OnMatchingTweetReceived;
            stream.DisconnectMessageReceived += OnDisconnectMessageReceived;
            stream.StreamStopped             += OnStreamStopped;

            stream.StallWarnings = true;

            await stream.StartStreamMatchingAnyConditionAsync();
        }
Example #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Console.WriteLine("What topic would you like to get tweets about?");
            string subject = Console.ReadLine();

            while (!stoppingToken.IsCancellationRequested)
            {
                ITwitterCredentials demo = Auth.SetUserCredentials(_options.ConsumerKey, _options.ConsumerSecret, _options.AccessToken, _options.AccessTokenSecret);

                IFilteredStream stream = Stream.CreateFilteredStream();

                stream.AddTrack(subject);

                stream.AddTweetLanguageFilter(LanguageFilter.Portuguese);

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                _logger.LogInformation("I'm listening to twitter");

                stream.MatchingTweetReceived += async(sender, arguments) =>
                {
                    await SendTweetsByKafka(new Tweet()
                    {
                        TweetedBy = arguments.Tweet.CreatedBy.ScreenName, TweetedAt = arguments.Tweet.CreatedAt, Text = arguments.Tweet.Text
                    });
                };

                stream.StartStreamMatchingAllConditions();

                await Task.Delay(5000, stoppingToken);
            }
        }
        private void ReCreateFilteredStream()
        {
            if (filteredStream != null)
            {
                filteredStream.StopStream();
                filteredStream.MatchingTweetReceived -= FilteredStreamOnMatchingTweetReceived;
            }

            filteredStream = Stream.CreateFilteredStream();
            filteredStream.AddTweetLanguageFilter(Language.Spanish);
            filteredStream.MatchingTweetReceived += FilteredStreamOnMatchingTweetReceived;
        }
Example #4
0
        static void Main(string[] args)
        {
            // get and parse json configuration file
            string  jsonConfig        = File.ReadAllText(Directory.GetCurrentDirectory() + "/config.json");
            dynamic jsonValues        = JObject.Parse(jsonConfig);
            string  ingressServiceUrl = jsonValues.endpoint;
            string  producerToken     = jsonValues.producertoken;
            int     delayInterval     = jsonValues.interval;

            consumerKey       = jsonValues.ConsumerKey;
            consumerSecret    = jsonValues.ConsumerSecret;
            accessToken       = jsonValues.AccessToken;
            accessTokenSecret = jsonValues.AccessTokenSecret;

            Auth.SetUserCredentials(consumerKey, consumerSecret, accessToken, accessTokenSecret);

            _stream = Tweetinvi.Stream.CreateFilteredStream();
            _stream.AddTweetLanguageFilter(LanguageFilter.English);

            //_stream.AddTrack("california fire");
            //_stream.AddTrack("green fish");

            List <string> tracks = jsonValues.Tracks.ToObject <List <string> >();

            int id = 1;

            foreach (string track in tracks)
            {
                _stats.Add(new TrackStat
                {
                    Id         = id++,
                    TrackValue = track
                });
                _stream.AddTrack(track);
            }
            _periodStart = QuantizeTime(DateTime.Now);

            _stream.MatchingTweetReceived += (sender, tweetArgs) =>
            {
                //CheckTime();    // need to do this on a timer

                var sanitized = Sanitize(tweetArgs.Tweet.FullText);
                //var sentence = new Sentence(sanitized);

                //Console.WriteLine(tweetArgs.Tweet.CreatedBy.Name);
                Console.WriteLine(tweetArgs.Tweet.Text);
                Console.WriteLine(String.Join(";", tweetArgs.MatchingTracks));

                TrackStat stat;
                foreach (string track in tweetArgs.MatchingTracks)
                {
                    stat = _stats.Where(x => x.TrackValue == track).FirstOrDefault();
                    if (stat != null)
                    {
                        ++stat.TweetCount;
                    }
                }

                //Console.WriteLine(sentence.Sentiment);
            };

            _stream.UnmanagedEventReceived += (sender, eventArgs) => {
                Console.WriteLine("unmanaged event received");
            };
            _stream.DisconnectMessageReceived += (sender, disconnectedArgs) =>
            {
                Console.WriteLine("received disconnected message");
                //Console.WriteLine(disconnectedArgs.DisconnectMessage);
            };

            _stream.JsonObjectReceived += (sender, jsonArgs) =>
            {
                //Console.WriteLine(jsonArgs.Json);
            };

            _stream.KeepAliveReceived += (sender, eventArgs) => {
                Console.WriteLine("received keep alive");
            };

            _stream.LimitReached += (sender, eventArgs) => {
                Console.WriteLine("limit reached");
            };

            _stream.WarningFallingBehindDetected += (sender, eventArgs) => {
                Console.WriteLine("fall behind warning");
            };


            // create Http client to send requests to ingress service
            _client = new IngressClient(ingressServiceUrl, producerToken);

            // Use compression when sending data.
            _client.UseCompression = jsonValues.compressionGzip;

            Setup();


            /*
             *          // Send Type messages (both Static and Dynamic)
             *          _client.CreateTypes(new string[] { FirstStaticType.JsonSchema, SecondStaticType.JsonSchema } );
             *          _client.CreateTypes(new string[] { FirstDynamicType.JsonSchema, SecondDynamicType.JsonSchema, ThirdDynamicType.JsonSchema });
             *
             *          // Send Container messages
             *          Container[] streams = {
             *              new Container() { Id = "Container1", TypeId = "FirstDynamicType" },
             *              new Container() { Id = "Container2", TypeId = "FirstDynamicType" },
             *              new Container() { Id = "Container3", TypeId = "SecondDynamicType"},
             *              new Container() { Id = "Container4", TypeId = "ThirdDynamicType" }
             *          };
             *          _client.CreateContainers(streams);
             *
             *          // Send Assets in Data messages
             *          AssetLinkValues<FirstStaticType> assetParent = new AssetLinkValues<FirstStaticType>()
             *          {
             *              typeid = "FirstStaticType",
             *              Values = new List<FirstStaticType> { new FirstStaticType()
             *                  {  index="Asset1", name="Parent element", StringProperty="Parent element attribute value"  }
             *              }
             *          };
             *          _client.SendValuesAsync(new AssetLinkValues<FirstStaticType>[] { assetParent}).Wait();
             *
             *          AssetLinkValues<SecondStaticType> assetChild = new AssetLinkValues<SecondStaticType>()
             *          {
             *              typeid = "SecondStaticType",
             *              Values = new List<SecondStaticType> { new SecondStaticType()
             *                  { index="Asset2", name="Child element", StringProperty="Child element attribute value"  }
             *              }
             *          };
             *          _client.SendValuesAsync(new AssetLinkValues<SecondStaticType>[] { assetChild }).Wait();
             *
             *          // Send Asset-to-child-Asset Links
             *          AssetLinkValues<AFLink<StaticElement,StaticElement>> dataLink = new AssetLinkValues<AFLink<StaticElement, StaticElement>>()
             *          {
             *              typeid = "__Link",
             *              Values = new List<AFLink<StaticElement, StaticElement>>   {
             *                  new AFLink<StaticElement,StaticElement>() {source = new StaticElement() {typeid = "FirstStaticType", index = "_ROOT" }, target = new StaticElement() {typeid= "FirstStaticType", index= "Asset1" } },
             *                  new AFLink<StaticElement,StaticElement>() {source = new StaticElement() {typeid = "FirstStaticType", index = "Asset1" }, target = new StaticElement() {typeid= "SecondStaticType", index= "Asset2" } }
             *              }
             *          };
             *          _client.SendValuesAsync(new AssetLinkValues<AFLink<StaticElement, StaticElement>>[] { dataLink, }).Wait();
             *
             *          // Send Asset-to-Data (i.e. Dynamic Attribute) Links
             *          AssetLinkValues<AFLink<StaticElement, DynamicElement>> dynamic_dataLink = new AssetLinkValues<AFLink<StaticElement, DynamicElement>>()
             *          {
             *              typeid = "__Link",
             *              Values = new List<AFLink<StaticElement, DynamicElement>>   {
             *                  new AFLink<StaticElement, DynamicElement>(){ source = new StaticElement() { typeid = "FirstStaticType", index= "Asset1" },target= new DynamicElement() { containerid ="Container1" } },
             *                  new AFLink<StaticElement, DynamicElement>(){ source = new StaticElement() { typeid = "SecondStaticType", index= "Asset2" },target= new DynamicElement() { containerid ="Container2" } },
             *                  new AFLink<StaticElement, DynamicElement>(){ source = new StaticElement() { typeid = "SecondStaticType", index= "Asset2" },target= new DynamicElement() { containerid ="Container3" } },
             *                  new AFLink<StaticElement, DynamicElement>(){ source = new StaticElement() { typeid = "SecondStaticType", index= "Asset2" },target= new DynamicElement() { containerid ="Container4" } }
             *              }
             *          };
             *          _client.SendValuesAsync(new AssetLinkValues<AFLink<StaticElement, DynamicElement>>[] { dynamic_dataLink }).Wait();
             */

            // Setting handler for Ctrl+C to exit sending data loop
            bool           continueRunning = true;
            AutoResetEvent exitEvent       = new AutoResetEvent(false);

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                continueRunning = false;
                Console.Write("Stopping... ");
                eventArgs.Cancel = true;
                _stream.StopStream();
                exitEvent.Set();
            };

            _timer = new System.Timers.Timer(5000)
            {
                AutoReset = false
            };

            _timer.Elapsed += (s, e) => CheckTime();
            _timer.Start();

            _stream.StartStreamMatchingAnyCondition();

            exitEvent.WaitOne();

            return;

/*
 *          // simulate realtime data
 *          Random rint = new Random();
 *          Random rdouble = new Random();
 *          string string_boolean_value = "True";
 *          int    integer_enum_value = 0;
 *
 *          // Now send simulated relatime data continously
 *          while (continueRunning)
 *          {
 *              // Create set of integers to send to streams
 *              List<FirstDynamicType> values = new List<FirstDynamicType>();
 *              for(int i = 0; i < 3; i++)
 *              {
 *                  values.Add(new FirstDynamicType() { timestamp = DateTime.UtcNow, IntegerProperty = rint.Next() });
 *                  Thread.Sleep(10);  // Offset the time-stamps by 10 ms
 *              }
 *              DataValues vals1 = new DataValues() { ContainerId = streams[0].Id, Values = values };
 *              DataValues vals2 = new DataValues() { ContainerId = streams[1].Id, Values = values };
 *              // Now send them
 *              //client.SendValuesAsync(new DataValues[] { vals1, vals2 }).Wait();
 *
 *              // Create set of SecondDynamicType values to send to streams
 *              List<SecondDynamicType> fnumbers = new List<SecondDynamicType>();
 *              for (int i = 0; i < 3; i++)
 *              {
 *                  string_boolean_value = (string_boolean_value == "True") ? "False" : "True";
 *                  fnumbers.Add(new SecondDynamicType() { timestamp = DateTime.UtcNow, NumberProperty1 = rdouble.NextDouble(), NumberProperty2= rdouble.NextDouble(), StringEnum = string_boolean_value });
 *                  Thread.Sleep(10);  // Offset the time-stamps by 10 ms
 *              }
 *              DataValues nums = new DataValues() { ContainerId = streams[2].Id, Values = fnumbers };
 *              //client.SendValuesAsync(new DataValues[] { nums }).Wait();
 *
 *
 *              // Create set of ThirdDynamicType values to send to streams
 *              List<ThirdDynamicType> enumvalues = new List<ThirdDynamicType>();
 *              for (int i = 0; i < 3; i++)
 *              {
 *                  integer_enum_value = (integer_enum_value == 0) ? 1 : 0;
 *                  enumvalues.Add(new ThirdDynamicType() { timestamp = DateTime.UtcNow,  IntegerEnum = integer_enum_value });
 *                  Thread.Sleep(10);  // Offset the time-stamps by 10 ms
 *              }
 *              DataValues bvals = new DataValues() { ContainerId = streams[3].Id, Values = enumvalues };
 *              //client.SendValuesAsync(new DataValues[] { bvals }).Wait();
 *
 *              Thread.Sleep(delayInterval);
 *          }
 */
        }
        private void ExecuteTwitterFinder(List <MultiBattleDefine> selectedBattles)
        {
            ExceptionHandler.SwallowWebExceptions = false;
            try
            {
                string consumerKey       = ConfigurationManager.AppSettings["ConsumerKey"];
                string consumerSecret    = ConfigurationManager.AppSettings["ConsumerSecret"];
                string accessToken       = ConfigurationManager.AppSettings["AccessToken"];
                string accessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecret"];

                Auth.SetUserCredentials(consumerKey, consumerSecret, accessToken, accessTokenSecret);

                TweetinviEvents.QueryBeforeExecute += (sender, args) =>
                {
                    //Console.WriteLine(args.QueryURL);
                };

                _twitterStream = Stream.CreateFilteredStream();

                //加入等級Filter
                selectedBattles.Select(s => s.Level).Distinct().ForEach(e =>
                {
                    _twitterStream.AddTrack("Lv" + e);
                });
                //加入名字Filter 降低通知頻率(中間名字有符號的會捉不到,放棄)
                //selectedBattles.ForEach(e =>
                //{
                //    _twitterStream.AddTrack("Lv" + e.BattleLevel + " " + e.Value);
                //});
                _twitterStream.AddTweetLanguageFilter(LanguageFilter.Japanese);


                //組合name的pattern
                string namePatternString = $"(?<name>{string.Join("|", selectedBattles.Select(s => $"Lv{s.Level}\\s*{s.Value}"))})";
                Regex  pattern           = new Regex(@"参戦ID:(?<matchid>([a-zA-Z0-9]){8})\s*" + namePatternString);

                //取得ID和名字
                //Regex pattern = new Regex(@"参戦ID:(?<matchid>([a-zA-Z0-9]){8})\s*Lv\d{2,3}\s(?<name>\w*)");

                _twitterStream.MatchingTweetReceived += (sender, args) =>
                {
                    var tweet = args.Tweet;
                    var match = pattern.Match(tweet.FullText);
                    if (match.Success)
                    {
                        OnBattleFound?.Invoke(match.Groups["matchid"].ToString(), match.Groups["name"].ToString(), string.Empty);
                    }
                    //else
                    //{
                    //    WriteLog("Not matched");
                    //}
                };
                _twitterStream.StartStreamMatchingAnyCondition();

                //var SStatus = _twitterStream.StreamState;

                if (_twitterStream.StreamState == StreamState.Stop)
                {
                    WriteLog(" stopped!");
                }
            }
            catch (TwitterException ex)
            {
                WriteLog(ex.ToString());
            }
            finally
            {
                if (null != _twitterStream && _twitterStream.StreamState != StreamState.Stop)
                {
                    _twitterStream.StopStream();
                    WriteLog(" Auto stopped!");
                }
            }
        }