Reputation: 8347
I have written the code for obtaining a set of a users favourites, however, what's not apparent is how I can page/cursor through these to obtain, say, a users very first set of favourites. The cursor value is always 0 and max/since ID
are null. Is there a way of acheiving this using LinqToTwitter?
Upvotes: 0
Views: 410
Reputation: 7513
For Favorites, you need to traverse the timeline with SinceID/MaxID. Here's an example:
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);
}
I wrote a blog post that explains how to work with Twitter Timelines. It was written for an earlier non-async version of LINQ to Twitter, but the concepts remain the same:
Working with Timelines with LINQ to Twitter
This is based on Twitter's guidance, which is a good read:
Twitter's Working with Timelines Documentation
Upvotes: 2