2

I am creating a demo application using wpf with tweetsharp library and I need to get the user acitvity such as TweetDeck activity. How can we achieve this task? Please help me.

Any help will be appreciated.

4

1 回答 1

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();
        }

以上代码行我从这个链接得到

于 2013-08-14T08:37:01.143 回答