2014-09-02 70 views
0

我已经编写了获取一组用户收藏夹的代码,但是,我不明白的是如何通过这些页面/光标来获取用户最喜欢的一组用户。游标值始终为0,并且max/since ID为空。使用LinqToTwitter有没有办法实现这一点?LinqToTwitter。我的收藏夹 - 全部获取

回答

2

对于收藏夹,您需要使用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的引导,这是一个很好看的:

Twitter's Working with Timelines Documentation