如何使用tweetsharp库获取用户活动?

时间:2013-08-13 13:50:12

标签: wpf api twitter tweetsharp

我正在使用带有tweetsharp库的wpf创建一个演示应用程序,我需要获得用户活动,例如TweetDeck活动。 我们怎样才能完成这项任务? 请帮帮我。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您应该使用以下代码行在网站流的帮助下获取tweetsharp library的Twitter活动。

public void Can_stream_from_user_stream()
        {
            const int maxStreamEvents = 5;

            var block = new AutoResetEvent(false);
            var count = 0;

            var service = GetAuthenticatedService();

            service.StreamUser((streamEvent, response) =>
            {
                if (streamEvent is TwitterUserStreamEnd)
                {
                    block.Set();
                }

                if (response.StatusCode == 0)
                {
                    if (streamEvent is TwitterUserStreamFriends)
                    {
                        var friends = (TwitterUserStreamFriends)streamEvent;
                        Assert.IsNotNull(friends);
                        Assert.IsNotNull(friends.RawSource);
                        Assert.IsTrue(friends.Ids.Any());
                    }

                    if (streamEvent is TwitterUserStreamEvent)
                    {
                        var @event = (TwitterUserStreamEvent)streamEvent;
                        Assert.IsNotNull(@event);
                        Assert.IsNotNull(@event.TargetObject);
                        Assert.IsNotNull(@event.RawSource);
                    }

                    if (streamEvent is TwitterUserStreamStatus)
                    {
                        var tweet = ((TwitterUserStreamStatus)streamEvent).Status;
                        Assert.IsNotNull(tweet);
                        Assert.IsNotNull(tweet.Id);
                        Assert.IsNotNull(tweet.User);
                        Assert.IsNotNull(tweet.RawSource);
                        Assert.IsNotNull(tweet.User.ScreenName);
                    }

                    if (streamEvent is TwitterUserStreamDirectMessage)
                    {
                        var dm = ((TwitterUserStreamDirectMessage)streamEvent).DirectMessage;
                        Assert.IsNotNull(dm);
                        Assert.IsNotNull(dm.Id);
                        Assert.IsNotNull(dm.Sender);
                        Assert.IsNotNull(dm.Recipient);
                        Assert.IsNotNull(dm.RawSource);
                    }

                    if (streamEvent is TwitterUserStreamDeleteStatus)
                    {
                        var deleted = (TwitterUserStreamDeleteStatus)streamEvent;
                        Assert.IsNotNull(deleted);
                        Assert.IsTrue(deleted.StatusId > 0);
                        Assert.IsTrue(deleted.UserId > 0);
                    }

                    if (streamEvent is TwitterUserStreamDeleteDirectMessage)
                    {
                        var deleted = (TwitterUserStreamDeleteDirectMessage)streamEvent;
                        Assert.IsNotNull(deleted);
                        Assert.IsTrue(deleted.DirectMessageId > 0);
                        Assert.IsTrue(deleted.UserId > 0);
                    }
                    count++;
                    if (count == maxStreamEvents)
                    {
                        block.Set();
                    }
                }
                else
                {
                    Assert.Ignore("Stream responsed with status code: {0}", response.StatusCode);
                }
            });

            block.WaitOne();
            service.CancelStreaming();
        }

我从this Link

获得的代码行