LinqToTwitter。收藏夹 - 获取所有

时间:2014-09-02 09:50:09

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

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

1 个答案:

答案 0 :(得分: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 to Twitter编写的,但概念保持不变:

Working with Timelines with LINQ to Twitter

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

Twitter's Working with Timelines Documentation