public twitteranalyzed parseToObject(string value)

        {
            var parsedObject             = JObject.Parse(value);
            var id                       = parsedObject["id"].ToString();
            var timestampString          = parsedObject["created_at"].ToString();
            var created_at               = DateTime.ParseExact(timestampString, "ddd MMM dd HH:mm:ss +0000 yyyy", CultureInfo.InvariantCulture);
            var text                     = parsedObject["text"].ToString();
            var screenName               = parsedObject["user"]["screen_name"].ToString();
            var tweet                    = parsedObject["sentiment"]["tweet"].ToString();
            var prediction               = parsedObject["sentiment"]["prediction"].ToString();
            var positiveConfidenceString = parsedObject["sentiment"]["positiveConfidence"].ToString();
            var positiveConfidence       = Double.Parse(positiveConfidenceString);
            var negativeConfidenceString = parsedObject["sentiment"]["negativeConfidence"].ToString();
            var negativeConfidence       = Double.Parse(negativeConfidenceString);
            var coordinates              = parsedObject["place"]["bounding_box"]["coordinates"].ToObject <List <List <List <Double> > > >();

            twitteranalyzed twitteranalyzed = new twitteranalyzed();

            twitteranalyzed.longitude          = (coordinates[0][0][0] + coordinates[0][2][0]) / 2;
            twitteranalyzed.latitude           = (coordinates[0][0][1] + coordinates[0][1][1]) / 2;
            twitteranalyzed.created_at         = created_at;
            twitteranalyzed.id                 = id;
            twitteranalyzed.prediction         = prediction;
            twitteranalyzed.screen_name        = screenName;
            twitteranalyzed.tweet              = tweet;
            twitteranalyzed.negativeConfidence = negativeConfidence;
            twitteranalyzed.positiveConfidence = positiveConfidence;
            return(twitteranalyzed);
        }
示例#2
0
        public async Task <List <twitteranalyzed> > GetAllCorona()
        {
            List <twitteranalyzed> historicTweets = new List <twitteranalyzed>();


            var rs = await _session.ExecuteAsync(new SimpleStatement("Select * from tweet"));

            foreach (var row in rs)
            {
                twitteranalyzed hisTweet = new twitteranalyzed();
                hisTweet.id                 = row.GetValue <string>("id");
                hisTweet.created_at         = row.GetValue <DateTime>("created_at");
                hisTweet.latitude           = row.GetValue <Double>("latitude");
                hisTweet.longitude          = row.GetValue <Double>("longitude");
                hisTweet.negativeConfidence = row.GetValue <Double>("negativeconfidence");
                hisTweet.positiveConfidence = row.GetValue <Double>("positiveconfidence");
                hisTweet.prediction         = row.GetValue <string>("prediction");
                hisTweet.screen_name        = row.GetValue <string>("screen_name");
                hisTweet.tweet              = row.GetValue <string>("tweet");

                historicTweets.Add(hisTweet);
            }


            return(historicTweets);
        }
示例#3
0
        public async Task <List <twitteranalyzed> > GetAllBetweenCorona(int @from, int to, int size)
        {
            DateTime newFrom = DateTime.Now.AddHours(-from);;
            DateTime newTo   = DateTime.Now.AddHours(-to);

            Console.WriteLine();
            List <twitteranalyzed> historicTweets = new List <twitteranalyzed>();

            //var ps = _session.Prepare(" INSERT INTO tweet(id , created_at , negativeconfidence , positiveconfidence , prediction , screen_name ,  tweet , latitude , longitude) VALUES (? , ? , ? , ? , ? , ? , ? , ? , ?)");
            var ps = await _session.PrepareAsync("SELECT * FROM covid  WHERE created_at >= ? AND  created_at <= ? and typeof = ? limit ? ;");

            var statement = ps.Bind(newFrom, newTo, "corona", size);
            var rs        = await _session.ExecuteAsync(statement);

            foreach (var row in rs)
            {
                twitteranalyzed hisTweet = new twitteranalyzed();
                hisTweet.id                 = row.GetValue <string>("id");
                hisTweet.created_at         = row.GetValue <DateTime>("created_at");
                hisTweet.latitude           = row.GetValue <Double>("latitude");
                hisTweet.longitude          = row.GetValue <Double>("longitude");
                hisTweet.negativeConfidence = row.GetValue <Double>("negativeconfidence");
                hisTweet.positiveConfidence = row.GetValue <Double>("positiveconfidence");
                hisTweet.prediction         = row.GetValue <string>("prediction");
                hisTweet.screen_name        = row.GetValue <string>("screen_name");
                hisTweet.tweet              = row.GetValue <string>("tweet");

                historicTweets.Add(hisTweet);
            }


            return(historicTweets);
        }
        public async Task SubscribeCoronaStream()
        {
            var conf = new ConsumerConfig
            {
                GroupId          = "test-consumer-group2",
                BootstrapServers = "nodemaster:9092,node1:19092,node2:29092",

                // Note: The AutoOffsetReset property determines the start offset in the event
                // there are not yet any committed offsets for the consumer group for the
                // topic/partitions of interest. By default, offsets are committed
                // automatically, so in this example, consumption will only start from the
                // earliest message in the topic 'my-topic' the first time you run the program.
                AutoOffsetReset = AutoOffsetReset.Latest
            };

            using (var c = new ConsumerBuilder <Ignore, string>(conf).Build())
            {
                // c.Assign(new TopicPartitionOffset("twitterraw", 0, new Offset(30155)));
                c.Subscribe("corona");
                CancellationTokenSource cts = new CancellationTokenSource();
                Console.CancelKeyPress += (_, e) => {
                    e.Cancel = true; // prevent the process from terminating.
                    cts.Cancel();
                };
                try
                { while (true)
                  {
                      await Task.Delay(100);

                      try
                      {
                          var cr = c.Consume(cts.Token);
                          Console.WriteLine("Push corona event: " + cr.Message);
                          await _hub.Clients.All.SendAsync("corona", cr.Message);

                          twitteranalyzed tweet = parseToObject(cr.Message.Value);
                          _cassandraService.AddCorona(tweet);
                      }
                      catch (ArgumentOutOfRangeException e)
                      {
                          Console.WriteLine($"Error occured: {e}");
                      }
                  }
                }
                catch (OperationCanceledException)
                {
                    // Ensure the consumer leaves the group cleanly and final offsets are committed.
                    c.Close();
                    _cassandraService.CleanUp();
                }
            }
            _cassandraService.CleanUp();
        }
示例#5
0
        public void AddNewscorrelated(twitteranalyzed tweet)
        {
            //string query = " INSERT INTO tweet(id , created_at , negativeconfidence , positiveconfidence , prediction , screen_name ,  tweet ) VALUES ('" + tweet.id + "'," + tweet.created_at.Second + "," + tweet.negativeConfidence + "," + tweet.positiveConfidence + ",'" + tweet.prediction + "','" + tweet.screen_name + "','" + tweet.tweet + "'"+ ")";

            var ps = _session.Prepare(" INSERT INTO covid(id , created_at , negativeconfidence , positiveconfidence , prediction , screen_name ,  tweet , latitude , longitude , typeof) VALUES (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)");

// ...bind different parameters every time you need to execute
            var statement = ps.Bind(tweet.id, tweet.created_at, tweet.negativeConfidence, tweet.positiveConfidence, tweet.prediction, tweet.screen_name, tweet.tweet, tweet.latitude, tweet.longitude, "news");

// Execute the bound statement with the provided parameters
            _session.Execute(statement);
        }