设置Twitter流以根据关键字从用户获取实时流



如果当特朗普在推特上发推文时我想要一条文字"种族主义者"或"移民",我该怎么做?我不确定这是有效的,更不用说如何在查询中添加多个关键字。

public static void Stream_SampleStreamExample()
{
    var stream = Stream.CreateUserStream();
    stream.AddCustomQueryParameter("realDonaldTrump", "racist");
    stream.MessageReceived += (sender, args) =>
    {
        Console.WriteLine(args.Message.Text);
    };
    stream.AddTweetLanguageFilter(LanguageFilter.English);            
    stream.StartStream();
}

我必须先关注用户吗?

public static void AuthenticatedUser_FollowUser(string userName)
{
    var authenticatedUser = User.GetAuthenticatedUser();
    var userToFollow = User.GetUserFromScreenName(userName);
    if (authenticatedUser.FollowUser(userToFollow))
    {
        Console.WriteLine("You have successfully sent a request to follow {0}", userToFollow.Name);
    }
}

现在有效。首先

string userStr = "realDonaldTrump";
// From a user screen name
user = User.GetUserFromScreenName(userStr);
userIdentifier = new UserIdentifier(user.Id);

添加要匹配的关键字:

fs.AddTrack("immigration");
...

设置过滤的流:

IFilteredStream fs = Stream.CreateFilteredStream();
and so on...

然后,在OnMatchedTweet中,查看Tweet的创建者是否是您想要的用户。

OnMatchedTweet(object sender, MatchedTweetReceivedEventArgs args)
{        
    if (args.Tweet.CreatedBy.Id != user.Id)
        return;
    ...
}

相关内容

  • 没有找到相关文章

最新更新