【问题标题】:LinqToTwitter. Favorites - Obtain allLinqToTwitter。收藏夹 - 获取所有
【发布时间】:2014-10-26 13:10:04
【问题描述】:

我已经编写了用于获取一组用户最爱的代码,但是,我不明白我如何通过这些页面/光标来获取,比如说,用户的第一组最爱。游标值始终为 0,max/since ID 为空。有没有办法使用 LinqToTwitter 实现这一目标?

【问题讨论】:

    标签: c# api twitter linq-to-twitter


    【解决方案1】:

    对于收藏夹,您需要使用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 to Twitter 编写的,但概念保持不变:

    Working with Timelines with LINQ to Twitter

    这是基于 Twitter 的指导,这是一个很好的阅读:

    Twitter's Working with Timelines Documentation

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2017-07-21
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多