Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
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);
                    }
                }
            }
        }