我试图让所有推文(计数总推文数)都属于主题标签。我的功能在这里,如何使用maxID和sinceID获取所有推文。什么是"计数"?我不知道。
if (maxid != null)
{
var searchResponse =
await
(from search in ctx.Search
where search.Type == SearchType.Search &&
search.Query == "#karne" &&
search.Count == Convert.ToInt32(count)
select search)
.SingleOrDefaultAsync();
maxid = Convert.ToString(searchResponse.SearchMetaData.MaxID);
foreach (var tweet in searchResponse.Statuses)
{
try
{
ResultSearch.Add(new KeyValuePair<String, String>(tweet.ID.ToString(), tweet.Text));
tweetcount++;
}
catch {}
}
while (maxid != null && tweetcount < Convert.ToInt32(count))
{
maxid = Convert.ToString(searchResponse.SearchMetaData.MaxID);
searchResponse =
await
(from search in ctx.Search
where search.Type == SearchType.Search &&
search.Query == "#karne" &&
search.Count == Convert.ToInt32(count) &&
search.MaxID == Convert.ToUInt64(maxid)
select search)
.SingleOrDefaultAsync();
foreach (var tweet in searchResponse.Statuses)
{
try
{
ResultSearch.Add(new KeyValuePair<String, String>(tweet.ID.ToString(), tweet.Text));
tweetcount++;
}
catch { }
}
}
}
答案 0 :(得分:9)
这是一个例子。请记住,MaxID
适用于当前会话,并阻止重新读取您在当前会话中已处理过的推文。 SinceID
是您在此搜索字词中收到的最早的推文,可帮助您避免重新阅读您之前会话期间已针对此搜索字词处理过的推文。从本质上讲,您正在创建一个窗口,其中MaxID
是下一个最新的推文,而SinceID
是您不想读过的最早的推文。在给定搜索字词的第一个会话中,您可以将SinceID
设置为1
,因为您还没有最早的推文。会话结束后,请保存SinceID
,以免意外重新阅读推文。
static async Task DoPagedSearchAsync(TwitterContext twitterCtx)
{
const int MaxSearchEntriesToReturn = 100;
string searchTerm = "twitter";
// oldest id you already have for this search term
ulong sinceID = 1;
// used after the first query to track current session
ulong maxID;
var combinedSearchResults = new List<Status>();
List<Status> searchResponse =
await
(from search in twitterCtx.Search
where search.Type == SearchType.Search &&
search.Query == searchTerm &&
search.Count == MaxSearchEntriesToReturn &&
search.SinceID == sinceID
select search.Statuses)
.SingleOrDefaultAsync();
combinedSearchResults.AddRange(searchResponse);
ulong previousMaxID = ulong.MaxValue;
do
{
// one less than the newest id you've just queried
maxID = searchResponse.Min(status => status.StatusID) - 1;
Debug.Assert(maxID < previousMaxID);
previousMaxID = maxID;
searchResponse =
await
(from search in twitterCtx.Search
where search.Type == SearchType.Search &&
search.Query == searchTerm &&
search.Count == MaxSearchEntriesToReturn &&
search.MaxID == maxID &&
search.SinceID == sinceID
select search.Statuses)
.SingleOrDefaultAsync();
combinedSearchResults.AddRange(searchResponse);
} while (searchResponse.Any());
combinedSearchResults.ForEach(tweet =>
Console.WriteLine(
"\n User: {0} ({1})\n Tweet: {2}",
tweet.User.ScreenNameResponse,
tweet.User.UserIDResponse,
tweet.Text));
}
这种方法看起来像很多代码,但实际上可以让您更好地控制搜索。例如您可以检查推文并根据推文的内容(如CreatedAt
)确定要查询的次数。您可以将查询包装在try/catch
块中,以便在超出速率限制或Twitter出现问题时监视HTTP 429,从而让您记住您的位置并恢复。您还可以监视twitterContext
RateLimit
属性,看看您是否正在接近并提前避免HTTP 429的异常。盲目阅读N条推文的任何其他技术都可能会迫使您浪费速率限制并降低应用程序的可扩展性。
SinceID
保存推文,以防止下次重读相同的推文
您使用该搜索字词进行搜索。有关此机制的更多信息,请阅读Twitter文档中的Working with Timelines。
答案 1 :(得分:0)
只想用Tweetinvi说它就像下面这样简单:
// If you want to handle RateLimits
RateLimit.RateLimitTrackerOption = RateLimitTrackerOptions.TrackAndAwait;
var tweets = Search.SearchTweets(new TweetSearchParameters("#karne")
{
MaximumNumberOfResults = 10000
MaxId = 243982 // If you want to start at a specific point
});
答案 2 :(得分:0)
TweetInvi现在更简单了。您所需要做的就是:
var matchingTweets = Search.SearchTweets("#AutismAwareness");