0
我已經編寫了獲取一組用戶收藏夾的代碼,但是,我不明白的是如何通過這些頁面/光標來獲取用戶最喜歡的一組用戶。遊標值始終爲0,並且max/since ID
爲空。使用LinqToTwitter有沒有辦法實現這一點?LinqToTwitter。我的收藏夾 - 全部獲取
我已經編寫了獲取一組用戶收藏夾的代碼,但是,我不明白的是如何通過這些頁面/光標來獲取用戶最喜歡的一組用戶。遊標值始終爲0,並且max/since ID
爲空。使用LinqToTwitter有沒有辦法實現這一點?LinqToTwitter。我的收藏夾 - 全部獲取
對於收藏夾,您需要使用SinceID/MaxID遍歷時間軸。以下是一個示例:
static async Task ShowFavoritesAsync(TwitterContext twitterCtx)
{
const int PerQueryFavCount = 200;
// set from a value that you previously saved
ulong sinceID = 1;
var favsResponse =
await
(from fav in twitterCtx.Favorites
where fav.Type == FavoritesType.Favorites &&
fav.Count == PerQueryFavCount
select fav)
.ToListAsync();
if (favsResponse == null)
{
Console.WriteLine("No favorites returned from Twitter.");
return;
}
var favList = new List<Favorites>(favsResponse);
// first tweet processed on current query
ulong maxID = favList.Min(fav => fav.StatusID) - 1;
do
{
favsResponse =
await
(from fav in twitterCtx.Favorites
where fav.Type == FavoritesType.Favorites &&
fav.Count == PerQueryFavCount &&
fav.SinceID == sinceID &&
fav.MaxID == maxID
select fav)
.ToListAsync();
if (favsResponse == null || favsResponse.Count == 0) break;
// reset first tweet to avoid re-querying the
// same list you just received
maxID = favsResponse.Min(fav => fav.StatusID) - 1;
favList.AddRange(favsResponse);
} while (favsResponse.Count > 0);
favList.ForEach(fav =>
{
if (fav != null && fav.User != null)
Console.WriteLine(
"Name: {0}, Tweet: {1}",
fav.User.ScreenNameResponse, fav.Text);
});
// save this in your db for this user so you can set
// sinceID accurately the next time you do a query
// and avoid querying the same tweets again.
ulong newSinceID = favList.Max(fav => fav.SinceID);
}
我寫了一篇博客文章,解釋如何使用Twitter時間軸。它是爲LINQ的早期非異步版本的Twitter寫的,但概念是相同的:
Working with Timelines with LINQ to Twitter
這是基於Twitter的引導,這是一個很好看的: