예제 #1
0
        private static void startTweetStream(string searchString)
        {
            stream.AddTrack(searchString);
            stream.MatchingTweetReceived += (sender, arg) =>
            {
                try
                {
                    var jsonData = JsonConvert.SerializeObject(arg.Tweet.TweetDTO);
                    groupedTweets.Add(arg.Tweet.Text.ToLower());
                    count++;
                }

                catch (Exception exception)
                {
                    Debug.WriteLine("# CAUGHT AN EXCEPTION" + searchString);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                    Console.ResetColor();
                }
            };

            //Prevent crashing at unexpected EOF or 0 bytes
            stream.StreamStopped += (sender, args) =>
            {
                Debug.WriteLine("# STREAM STOPPED");
                if (args.Exception != null)
                {
                    stream.StartStreamMatchingAllConditions();
                }
            };
            stream.StartStreamMatchingAllConditions();
        }
예제 #2
0
        public void Start(string track, MatchCondition condition, params Model.Location[] locations)
        {
            // Filters
            if (!string.IsNullOrEmpty(track))
            {
                _stream.AddTrack(track);
            }

            if (null != locations && locations.Length > 0)
            {
                foreach (var location in locations)
                {
                    _stream.AddLocation(
                        new Coordinates(location.Latitude1, location.Longitude1),
                        new Coordinates(location.Latitude2, location.Longitude2)
                        );
                }
            }

            // Start
            if (condition == MatchCondition.AnyCondition)
            {
                _stream.StartStreamMatchingAnyCondition();
            }
            else
            {
                _stream.StartStreamMatchingAllConditions();
            }
        }
예제 #3
0
        /*Constructor: takes the keys needed, last paramets are filters for the stream*/
        public TwitterStreamProcessor(string consumerKey, string consumerSecret,
                                      string accessToken, string accessTokenSecret, params string[] filters)
        {
            PictureDataQueue    = new Queue <PictureData>();
            PictureMetaDataList = new List <PictureMetaData>();

            //The credentials for the twitter api
            var cred = new TwitterCredentials(consumerKey, consumerSecret,
                                              accessToken, accessTokenSecret);

            stream = Tweetinvi.Stream.CreateFilteredStream(cred);

            //insert the filters
            foreach (var filter in filters)
            {
                stream.AddTrack(filter);
            }

            //Delegate the method responsible for the handling of the matches
            stream.MatchingTweetReceived += handleMatchingTweet;

            //start the stream as a task: will run "forever"
            Task.Factory.StartNew(() => stream.StartStreamMatchingAllConditions());

            StreamStart = DateTime.Now;
        }
예제 #4
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);
            }
        }
        public void CreateStream()
        {
            TwitterCredentials.SetCredentials(
                RoleEnvironment.GetConfigurationSettingValue("UserAccessToken"),
                RoleEnvironment.GetConfigurationSettingValue("UserAccessSecret"),
                RoleEnvironment.GetConfigurationSettingValue("ConsumerKey"),
                RoleEnvironment.GetConfigurationSettingValue("ConsumerSecret"));

            //rateLimit = new ProtectedTimer(TimeSpan.FromSeconds(60), new Action(OnCheckRateRequestLimit));
            //rateLimit.Start();

            string words = RoleEnvironment.GetConfigurationSettingValue("TwitterSearchTerms");

            string[] items = words.Split(';');
            foreach (var item in items)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    stream.AddTrack(item);
                    Trace.Write(string.Concat("Track item -> ", item), typeof(TwitterStreamingEngine).FullName);
                }
            }

            stream.MatchingTweetReceived        += OnMatchingTweetReceived;
            stream.DisconnectMessageReceived    += OnStreamDisconnectMessageReveived;
            stream.WarningFallingBehindDetected += OnWarningFallingBehindDetected;
            stream.LimitReached  += OnLimitReached;
            stream.StreamStarted += OnStreamStarted;
            stream.StreamStopped += OnStreamStopped;
            stream.StartStreamMatchingAllConditions();
        }
        public void StartStream(string keyword)
        {
            Console.WriteLine(
                $"[{DateTime.Now}] - Starting listening for tweets that contains the keyword '{keyword}'...");

            _stream.AddTrack(keyword);
            _stream.MatchingTweetReceived += (sender, args) => { _tweetProcessor.ProcessTweetAsync(keyword, args); };
            _stream.StartStreamMatchingAllConditions();
        }
예제 #7
0
        private void ListenerThreadFunction()
        {
            TwitterCredentials.SetCredentials(
                ConfigurationManager.AppSettings["token_AccessToken"],
                ConfigurationManager.AppSettings["token_AccessTokenSecret"],
                ConfigurationManager.AppSettings["token_ConsumerKey"],
                ConfigurationManager.AppSettings["token_ConsumerSecret"]);

            while (threadRunning)
            {
                try
                {
                    //var hbase = new HBaseWriter();
                    stream = Stream.CreateFilteredStream();
                    var location = Geo.GenerateLocation(-180, -90, 180, 90);
                    stream.AddLocation(location);

                    var tweetCount = 0;
                    var timer      = Stopwatch.StartNew();

                    stream.MatchingTweetReceived += (sender, args) =>
                    {
                        tweetCount++;
                        var tweet = args.Tweet;

                        // Store indexItem in the buffer
                        lock (queue)
                        {
                            queue.Enqueue(tweet);
                        }

                        if (timer.ElapsedMilliseconds > 1000)
                        {
                            if (tweet.Coordinates != null)
                            {
                                Context.Logger.Info("{0}: {1} {2}", tweet.Id, tweet.Language.ToString(), tweet.Text);
                                Context.Logger.Info("\tLocation: {0}, {1}", tweet.Coordinates.Longitude, tweet.Coordinates.Latitude);
                            }

                            timer.Restart();
                            Context.Logger.Info("===== Tweets/sec: {0} =====", tweetCount);
                            tweetCount = 0;
                        }
                    };

                    stream.StartStreamMatchingAllConditions();
                }
                catch (Exception ex)
                {
                    Context.Logger.Fatal("Exception: {0}", ex.Message);
                }
            }
        }
예제 #8
0
        public static void MineTwitter(string term)
        {
            TwitterCredentials.SetCredentials(
                ConfigurationManager.AppSettings["AccessToken"],
                ConfigurationManager.AppSettings["AccessSecret"],
                ConfigurationManager.AppSettings["ConsumerKey"],
                ConfigurationManager.AppSettings["ConsumerSecret"]);
            IFilteredStream stream = Tweetinvi.Stream.CreateFilteredStream();

            stream.MatchingTweetReceived += Stream_TweetReceived;
            stream.AddTrack(term);
            stream.StartStreamMatchingAllConditions();
        }
예제 #9
0
        public void StartStream(string keyword)
        {
            Console.WriteLine(
                $"[{DateTime.Now}] - Starting listening for tweets that contains the keyword '{keyword}'...");
            try
            {
                // Disable the exception swallowing to allow exception to be thrown by Tweetinvi
                ExceptionHandler.SwallowWebExceptions = false;
                var authenticatedUser = User.GetAuthenticatedUser(null, new GetAuthenticatedUserParameters());

                if (authenticatedUser != null) // Something went wrong but we don't know what
                {
                    _stream.AddTrack(keyword);
                    _stream.MatchingTweetReceived += (sender, args) =>
                    {
                        // Exclude RTs
                        if (args.Tweet.IsRetweet)
                        {
                            return;
                        }

                        if (_tweetsPerMinute.HasValue && _tweetsPerMinute.Value != default(int))
                        {
                            Throttle();
                        }

                        _tweetProcessor.ProcessTweetAsync(keyword, args);

                        _lastProcessedTime = Environment.TickCount;
                        Console.WriteLine($"[{DateTime.Now}] - Last processed time {_lastProcessedTime} ms.");
                    };
                    _stream.StartStreamMatchingAllConditions();
                }
            }
            catch (TwitterException ex)
            {
                Console.WriteLine($"TwitterException Message : {ex.Message} - description : {ex.TwitterDescription}");
                throw ex;
            }
        }
예제 #10
0
        public void StreamTwitter(Action <TweetDto, string> _Update, string _Connection, List <string> tracks, bool enableLocation)
        {
            cancellationToken = new CancellationTokenSource();
            Connection        = _Connection;
            regex             = new Regex(@"(?<=#)\w+");
            Login();
            Update      = _Update;
            GetLocation = new OpenStreetMapHelper();
            stream      = Stream.CreateFilteredStream();
            tracks.ForEach(x => stream.AddTrack(x));
            if (enableLocation)
            {
                stream.AddLocation(new Tweetinvi.Models.Coordinates(49.246292, -123.116226), new Tweetinvi.Models.Coordinates(-33.865143, 151.209900));
            }

            Task.Factory.StartNew(async() => await TweetAnalysis(), cancellationToken.Token);
            stream.MatchingTweetReceived += (sender, args) =>
            {
                Tweets.Push(args.Tweet);
            };
            stream.StartStreamMatchingAllConditions();
        }
예제 #11
0
        private void StartStreaming(string sequenceToken, int sampleSize, string filterValue)
        {
            var counter        = 0;
            var logEventsBatch = new List <InputLogEvent>();

            _stream.AddTrack(filterValue);
            // _stream.AddLocation(new Coordinates(32.0, -114.42), new Coordinates(41.96, -124.21));
            _stream.MatchingTweetReceived += (sender, eventArgs) => {
                Console.WriteLine(counter + ": " + eventArgs.Tweet);
                var json      = eventArgs.Tweet.ToJson();
                var tweetInfo = JsonConvert.DeserializeObject <JObject>(json);
                logEventsBatch.Add(new InputLogEvent {
                    Message   = GetLogText(tweetInfo),
                    Timestamp = DateTime.Now
                });
                if (++counter % 50 == 0)
                {
                    sequenceToken  = DispatchLogEvents(logEventsBatch, sequenceToken);
                    logEventsBatch = new List <InputLogEvent>();
                }
                if (--sampleSize == 0)
                {
                    sequenceToken = DispatchLogEvents(logEventsBatch, sequenceToken);
                    Stop();
                }
            };
            _stream.StreamStopped += (sender, args) => {
                if (args.Exception != null)
                {
                    Console.WriteLine($"Stream stopped with exception: {args.Exception}");
                }
                if (args.DisconnectMessage != null)
                {
                    Console.WriteLine($"Disconnect message: {args.DisconnectMessage}");
                }
            };
            _stream.StartStreamMatchingAllConditions();
        }
예제 #12
0
 public void StartListening()
 {
     _stream.StartStreamMatchingAllConditions();
 }
예제 #13
0
        private void ListenerThreadFunction()
        {
            TwitterCredentials.SetCredentials(
                ConfigurationManager.AppSettings["token_AccessToken"],
                ConfigurationManager.AppSettings["token_AccessTokenSecret"],
                ConfigurationManager.AppSettings["token_ConsumerKey"],
                ConfigurationManager.AppSettings["token_ConsumerSecret"]);

            while (threadRunning)
            {
                try
                {
                    //var hbase = new HBaseWriter();
                    stream = Stream.CreateFilteredStream();
                    var location = Geo.GenerateLocation(-180, -90, 180, 90);
                    stream.AddLocation(location);

                    var tweetCount = 0;
                    var timer = Stopwatch.StartNew();

                    stream.MatchingTweetReceived += (sender, args) =>
                    {
                        tweetCount++;
                        var tweet = args.Tweet;

                        // Store indexItem in the buffer
                        lock (queue)
                        {
                            queue.Enqueue(tweet);
                        }

                        if (timer.ElapsedMilliseconds > 1000)
                        {
                            if (tweet.Coordinates != null)
                            {
                                Context.Logger.Info("{0}: {1} {2}", tweet.Id, tweet.Language.ToString(), tweet.Text);
                                Context.Logger.Info("\tLocation: {0}, {1}", tweet.Coordinates.Longitude, tweet.Coordinates.Latitude);
                            }

                            timer.Restart();
                            Context.Logger.Info("===== Tweets/sec: {0} =====", tweetCount);
                            tweetCount = 0;
                        }
                    };

                    stream.StartStreamMatchingAllConditions();
                }
                catch (Exception ex)
                {
                    Context.Logger.Fatal("Exception: {0}", ex.Message);
                }
            }
        }
        private void InitStreams()
        {
            //borrar despues

            List <IQueryConfiguration> configs = main.GetCurrentsConfigurations();



            if (configs.Count > 0 && threadStream == null)
            {
                stream           = Stream.CreateFilteredStream();
                stream.TweetMode = TweetMode.Extended;
                foreach (var config in configs)
                {
                    foreach (var key in config.Keywords)
                    {
                        stream.AddTrack(key);
                    }
                }

                threadStream = new Thread(() =>
                {
                    stream.MatchingTweetReceived += (sender, args) =>
                    {
                        IPublication publication = TwitterSearcher.ParseTweetToPublication(args.Tweet, configs[0]);
                        lock (this)
                        {
                            publications2.Add(publication);
                        }
                    };
                    stream.StartStreamMatchingAllConditions();
                });
                threadStream.Start();



                threadShow = new Thread(() =>
                {
                    while (publications2.Count >= 0)
                    {
                        if (publications2.Count % 10 == 0)
                        {
                            show sho = new show(ShowPublications);
                            this.Invoke(sho, publications2);
                        }
                        else
                        {
                            Thread.Sleep(500);
                        }
                    }
                });

                threadShow.Start();
                //Recordar las privacidades queries, y configurations, quitar static de parse tweet y quitar
                //delegado
            }
            else
            {
                StopStreams();
            }
        }
예제 #15
0
 public void Start()
 {
     _stream?.StartStreamMatchingAllConditions();
 }