我将提供一些服务,但我不确定一些事情,所以如果可以的话,请帮忙。如何实时跟踪某个用户的 Twitter 提要,并在用户在他的提要中添加一些标签时在我的服务中执行一些操作?我不想准备好解决方案,我必须知道我必须学习哪些技术和第三方库?
问问题
2084 次
2 回答
1
一种解决方案是使用Twitter 的流 API和 Json 解析器,如Json.Net
编辑
这是一个示例代码
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://stream.twitter.com/1/statuses/sample.json");
webRequest.Credentials = new NetworkCredential("....", "......");
webRequest.Timeout = -1;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader responseStream = new StreamReader(webResponse.GetResponseStream());
while (true)
{
var line = responseStream.ReadLine();
if (String.IsNullOrEmpty(line)) continue;
dynamic obj = JsonConvert.DeserializeObject(line);
if (obj.user != null)
Console.WriteLine(obj.user.screen_name + ": " + obj.text);
}
于 2012-08-31T09:10:13.160 回答
1
LINQ to Twitter, http: //linqtotwitter.codeplex.com/,支持 Twitter 流。这是一个例子:
(from strm in twitterCtx.UserStream
where strm.Type == UserStreamType.User
select strm)
.StreamingCallback(strm =>
{
if (strm.Status == TwitterErrorStatus.RequestProcessingException)
{
WebException wex = strm.Error as WebException;
if (wex != null && wex.Status == WebExceptionStatus.ConnectFailure)
{
Console.WriteLine(wex.Message + " You might want to reconnect.");
}
Console.WriteLine(strm.Error.ToString());
return;
}
Console.WriteLine(strm.Content + "\n");
if (count++ >= 25)
{
strm.CloseStream();
}
})
.SingleOrDefault();
这是一个用户流,但您对过滤器、样本、站点和其他流具有类似的支持。
于 2012-08-31T16:46:26.920 回答