Beispiel #1
0
        static async Task DoControlStreamAsync(TwitterContext twitterCtx)
        {
            var evt = new ManualResetEventSlim(false);
            string streamID = string.Empty;

            Task.Run(async () =>
            {
                Console.WriteLine("\nStreamed Content: \n");
                int count = 0;
                var cancelTokenSrc = new CancellationTokenSource();

                try
                {
                    await
                        (from strm in twitterCtx.Streaming
                         where strm.Type == StreamingType.Site &&
                               strm.Follow == "15411837,16761255"
                         select strm)
                        .WithCancellation(cancelTokenSrc.Token)
                        .StartAsync(async strm =>
                        {
                            if (string.IsNullOrEmpty(strm.Content))
                                Console.WriteLine("Keep-Alive");
                            else
                                HandleStreamResponse(strm);

                            if (strm.EntityType == StreamEntityType.Control)
                            {
                                var control = strm.Entity as Control;
                                streamID = control.URL.Replace("/1.1/site/c/", "");
                                evt.Set();
                            }

                            if (count++ == 25)
                                cancelTokenSrc.Cancel();
                        });
                }
                catch (OperationCanceledException)
                {
                    Console.WriteLine("Stream cancelled.");
                }
            });

            evt.Wait();

            Console.WriteLine("Follower Details:\n");

            var ctrlStrmFollowers =
                await
                (from strm in twitterCtx.ControlStream
                    where strm.Type == ControlStreamType.Followers &&
                        strm.UserID == 15411837 &&
                        strm.StreamID == streamID
                    select strm)
                .SingleOrDefaultAsync();

            if (ctrlStrmFollowers != null)
            {
                ControlStreamFollow follow = ctrlStrmFollowers.Follow;
                ControlStreamUser followUser = follow.User;
                List<ulong> friends = follow.Friends;
                Cursors cursors = follow.Cursors;

                Console.WriteLine("User ID: " + followUser.UserID);
                Console.WriteLine("User Name: " + followUser.Name);
                Console.WriteLine("Can DM: " + followUser.DM);
                friends.ForEach(friend => Console.WriteLine(friend));
                Console.WriteLine("Prev Cursor: " + cursors.Previous);
                Console.WriteLine("Next Cursor: " + cursors.Next);

                Console.WriteLine("Info Details:\n");
            } 
            
            var ctrlStrmInfo =
                (from strm in twitterCtx.ControlStream
                    where strm.Type == ControlStreamType.Info &&
                        strm.StreamID == streamID
                    select strm)
                .SingleOrDefault();

            if (ctrlStrmInfo != null)
            {
                ControlStreamInfo infoUser = ctrlStrmInfo.Info;
                ControlStreamUser user = infoUser.Users.First();
                Console.WriteLine("User ID: " + user.UserID);
                Console.WriteLine("User Name: " + user.Name);
                Console.WriteLine("Can DM: " + user.DM);
                Console.WriteLine("Delimited: " + infoUser.Delimited);
                Console.WriteLine("Include Followings Acitity: " + infoUser.IncludeFollowingsActivity);
                Console.WriteLine("Include User Changes: " + infoUser.IncludeUserChanges);
                Console.WriteLine("Replies: " + infoUser.Replies);
                Console.WriteLine("With: " + infoUser.With);

                Console.WriteLine("\nInitial Stream Users: ");

                await PrintUserInfoAsync(twitterCtx, streamID);

                ControlStream csAdd = await twitterCtx.AddSiteStreamUserAsync(new List<ulong> { 16761255 }, streamID);
                Console.WriteLine("Command Response: " + csAdd.CommandResponse);
                Console.WriteLine("\nAfter Adding a User: "******"Command Response: " + csRemove.CommandResponse);
                Console.WriteLine("\nAfter Removing a User: ");
                await PrintUserInfoAsync(twitterCtx, streamID);
            }
        }