C# 如何使用游标通过TweetSharp检索所有Twitter用户?
我正在WPF.NET4.0c#应用程序中使用TweetSharp,并使用经过身份验证的Twitter服务对象我在遍历追随者列表以检索每个个人资料时遇到问题。我使用以下代码:C# 如何使用游标通过TweetSharp检索所有Twitter用户?,c#,.net,twitter,cursor,tweetsharp,C#,.net,Twitter,Cursor,Tweetsharp,我正在WPF.NET4.0c#应用程序中使用TweetSharp,并使用经过身份验证的Twitter服务对象我在遍历追随者列表以检索每个个人资料时遇到问题。我使用以下代码: TwitterCursorList<TwitterUser> followers = twitterService.ListFollowersOf(userID, -1); while (followers != null) { foreach (TwitterUser follower in follo
TwitterCursorList<TwitterUser> followers = twitterService.ListFollowersOf(userID, -1);
while (followers != null)
{
foreach (TwitterUser follower in followers)
{
//Do something with the user profile here
}
followers = twitterService.ListFollowersOf(userID, (long)followers.NextCursor);
}
TwitterCursorList followers=twitterService.ListFollowersOf(userID,-1);
while(followers!=null)
{
foreach(TwitterUser追随者中的追随者)
{
//在这里对用户配置文件执行一些操作
}
followers=twitterService.listflowersof(userID,(long)followers.NextCursor);
}
我看到一个奇怪的行为,当我对我自己的帐户使用这个,它有1271追随者截至本文。第一次运行代码时,我得到了100个followers的列表,在下一次调用ListFollowersOf时,followers为null,循环结束
这里是奇怪的部分:如果我再次运行此代码,无论是在同一个应用程序实例中,还是如果我停止Visual Studio并重新启动,都无所谓,我会获得一个额外的权限,现在我会获得200个追随者。如果我再做一次这个把戏,现在我在得到空值之前得到300个追随者,依此类推。我重复了很多次,直到上面代码的一次调用返回了所有1271个追随者
真正奇怪的是,它最终会重置。我认为这与twitterapi限制重置时间有关,但我还没有证实这一点。我会看看这是否与API重置一致。一旦重置发生,我只有100个追随者,然后是200个,以此类推
我已经查看了TweetSharp单元测试和以下帖子,但它们对我不起作用:
- 我没有这个库,但根据示例,您应该有这样的库:
TwitterCursorList<TwitterUser> followers = twitterService.ListFollowersOf(userID, -1);
while (followers.NextCursor != null)
{
if(followers != null)
{
foreach (TwitterUser follower in followers)
{
//Do something with the user profile here
}
}
followers = twitterService.ListFollowersOf(userID, (long)followers.NextCursor);
}
public static List<TwitterUser> GetFollowers(this TwitterService svc)
{
List<TwitterUser> ret = new List<TwitterUser>();
var followers = svc.ListFollowers(-1);
ret.AddRange(followers);
while (followers.NextCursor != null && followers.NextCursor.Value > 0)
{
followers = svc.ListFollowers(followers.NextCursor.Value);
ret.AddRange(followers);
}
return ret;
}
TwitterCursorList followers=twitterService.ListFollowersOf(userID,-1);
while(followers.NextCursor!=null)
{
if(followers!=null)
{
foreach(TwitterUser追随者中的追随者)
{
//在这里对用户配置文件执行一些操作
}
}
followers=twitterService.listflowersof(userID,(long)followers.NextCursor);
}
同样,我无法运行代码,因为我没有库(也懒得下载它),但请尝试一下,并让我知道它是否有效。我使用类似于以下内容:
TwitterCursorList<TwitterUser> followers = twitterService.ListFollowersOf(userID, -1);
while (followers.NextCursor != null)
{
if(followers != null)
{
foreach (TwitterUser follower in followers)
{
//Do something with the user profile here
}
}
followers = twitterService.ListFollowersOf(userID, (long)followers.NextCursor);
}
public static List<TwitterUser> GetFollowers(this TwitterService svc)
{
List<TwitterUser> ret = new List<TwitterUser>();
var followers = svc.ListFollowers(-1);
ret.AddRange(followers);
while (followers.NextCursor != null && followers.NextCursor.Value > 0)
{
followers = svc.ListFollowers(followers.NextCursor.Value);
ret.AddRange(followers);
}
return ret;
}
如果你得到了什么回报,那是因为你的代码工作得很好 很容易找出问题所在,然后开始相应地解决问题。我怀疑你是对的,你被Twitter限制了 在while循环后将此添加到代码中:
if(followers == null) {
Console.WriteLine("Error " + twitterService.Response.Error);
}
事实上,每当有东西返回
null
时,您都可以检查响应对象的错误,看看它是怎么回事 您是否在(followers.NextCursor!=null)时尝试了?当followers
为null
时,while循环停止,但是当NextCursor
为null
时,您链接的问题中的其他示例停止。我已经完成了。followers.NextCursor返回一个long?,问题是,一旦followers变为null(如上所述,它变为null),我将抛出一个null引用异常。检查NextCursor
是否为null
并且您将其强制转换为long,因此它看起来确实是某种引用(我没有代码来检查它)。这里是检查:Assert.IsNotNull(followers.NextCursor)
您仍然可以检查followers
是否为null
,如果null
,则不引用它,但看起来您的while循环应该一直运行,直到NextCursor
为null
…至少从其他示例来看是这样的。我最初尝试不强制转换为long,但VS抛出了一个compiler错误“无法从'long'转换为'long'”。我还收到另一个编译器错误“与'TweetSharp.TwitterService.ListFollowersOf(int,long)'匹配的最佳重载方法具有一些无效参数”。如果可以检查NextCursor
是否为null
,则它要么是引用类型,要么是可为null的值类型。值类型(如long
)不能为null
,除非字段/属性/变量声明为可为null(例如long?
)。如果该方法需要一个long
,则必须将NextCursor
强制转换为long
,而不管它是引用类型还是可空的long?
。