如何使用 LINQ to Twitter 从用户那里获取更多推文

How to get more tweet from a user using LINQ to Twitter

它清楚地表明它允许每人最多发送 3200 条推文(参见 documentation)。但是使用这个代码:

var statusTweets =
      from tweet in twitterCtx.Status
      where tweet.Type == StatusType.User
      && tweet.ScreenName == "JoeMayo"
      select tweet;

statusTweets.ToList().ForEach(
      tweet => Console.WriteLine(
      "Name: {0}, Tweet: {1}\n",
      tweet.User.Name, tweet.Text));

它只给我前 20 条推文,而且您不能使用此代码应用光标移动。有谁有 LINQ To Twitter 代码可以让我获取尽可能多的推文吗?

documentation 表示“获取授权用户的 20 个最新状态”。

可能 必须 use/send 额外的 "Parameters/Filters" - count 如文档中所述,其中指出"Number of tweets to return; max is 200." 并且这是一个可选字段,因此在您的案例中只返回前 20 个。

要从Twitter获取tweets,我们可以通过两种方式获取:

使用 HastagScreenName

我创建了一种自定义方法,您可以在一次请求中获得 3200 条推文。

public void GetTwitterFeeds(dynamic settings, bool ishashtag, DateTime startDate, DateTime endDate)
        {
            string screenname = settings.socialFetchTerm.ToString();
            var auth = new SingleUserAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = AppSettings.ConsumerKey,
                    ConsumerSecret = AppSettings.ConsumerSecret,
                    OAuthToken = AppSettings.AccessToken,
                    AccessToken = AppSettings.AccessTokenSecret
                }
            };
            var twitterCtx = new TwitterContext(auth);

            var ownTweets = new List<Status>();
            ulong sinceId = 0;
            ulong maxID = 0;
            int lastStatusCount = 0;
            bool flag = true;
            var statusResponse = new List<Status>();
            if (!ishashtag)
            {

                statusResponse = (from tweet in twitterCtx.Status
                                  where tweet.Type == StatusType.User
                                        && tweet.ScreenName == screenname
                                        && tweet.Count == 200
                                        && (tweet.CreatedAt >= startDate && tweet.CreatedAt <= endDate)
                                  select tweet).ToList();
            }
            else
            {
                statusResponse = (from search in twitterCtx.Search
                                  where search.Type == SearchType.Search &&
                                        search.Query == screenname
                                        && search.Count == 200
                                  from Status status in search.Statuses
                                  where (status.CreatedAt >= startDate && status.CreatedAt <= endDate)
                                  select status
                    ).ToList();
            }
            if (statusResponse.Count > 0)
            {
                maxID = ulong.Parse(statusResponse.First().StatusID);
                ownTweets.AddRange(statusResponse);
            }
            do
            {
                int rateLimitStatus = twitterCtx.RateLimitRemaining;

                if (rateLimitStatus != 0)
                {
                    if (ishashtag)
                    {

                            statusResponse = (from search in twitterCtx.Search
                                              where search.Type == SearchType.Search &&
                                                    search.Query == screenname
                                                    && search.Count ==200
                                              from Status status in search.Statuses
                                              where
                                                  (status.CreatedAt >= startDate && status.CreatedAt <= endDate) &&
                                                  status.SinceID == sinceId && status.MaxID == maxID
                                              select status
                                ).ToList();



                            lastStatusCount = statusResponse.Count;

                            if (lastStatusCount != 0)
                            {
                                maxID = ulong.Parse(statusResponse.Last().StatusID) - 1;

                                ownTweets.AddRange(statusResponse);
                            }
                            else
                            {
                                flag = false;
                            }
                    }

                    else
                    {

                            statusResponse = (from tweet in twitterCtx.Status
                                              where tweet.Type == StatusType.User
                                                    && tweet.ScreenName == screenname
                                                    && tweet.SinceID == sinceId && tweet.MaxID == maxID
                                                    && tweet.Count == 200 
                                                    && (tweet.CreatedAt >= startDate && tweet.CreatedAt <= endDate)
                                              select tweet).ToList();


                            lastStatusCount = statusResponse.Count;

                            if (lastStatusCount != 0)
                            {
                                maxID = ulong.Parse(statusResponse.Last().StatusID) - 1;

                                ownTweets.AddRange(statusResponse);
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                }
                else
                {
                    flag = false;
                }

            } while (flag);

            foreach (var tweetStatus in ownTweets)
            {
                if (tweetStatus != null)
                {
                    var socialSiteData = new SocialSitesData
                    {
                        //  SocialType = SocialType.Twitter,

                        SocialType = settings.socialType,
                        SocialSubType = settings.socialSubType,
                        SocialFetchTerm = settings.socialFetchTerm,
                        PostId = tweetStatus.StatusID,
                        Post = tweetStatus.Text,
                        PostUrl = "https://twitter.com/" + tweetStatus.ScreenName + "/status/" + tweetStatus.StatusID,
                        ImageSource =
                            tweetStatus.Entities.MediaEntities.Count > 0
                                ? tweetStatus.Entities.MediaEntities[0].MediaUrl
                                : "",
                        VideoSource =
                            tweetStatus.Entities.UrlEntities.Count > 0
                                ? tweetStatus.Entities.UrlEntities[0].ExpandedUrl
                                : "",
                        PostTime = tweetStatus.CreatedAt,
                    };
                    if (!_socialHubCrudDal.IsSocialSiteDataExists(tweetStatus.StatusID))
                        _socialHubCrudDal.AddSocialSiteData(socialSiteData);
                }
            }
        }

重新编码自@Rakesh Sadhula

    public void GetTwitterFeeds(string screenName, DateTime startDate, DateTime endDate)
    {

        var twitterCtx = new TwitterContext(authorizer);

        var ownTweets = new List<Status>();
        ulong maxID = 0;
        int lastStatusCount = 0;
        bool flag = true;
        var statusResponse = new List<Status>();

        statusResponse = (from tweet in twitterCtx.Status
                            where tweet.Type == StatusType.User
                                && tweet.ScreenName == screenName
                                && tweet.Count == 200
                                && (tweet.CreatedAt >= startDate && tweet.CreatedAt <= endDate)
                            select tweet).ToList();

        if (statusResponse.Count > 0)
        {
            maxID = ulong.Parse(statusResponse.Last().StatusID.ToString());
            ownTweets.AddRange(statusResponse);
        }
        if (ownTweets.Count == 200)
        {
            do
            {
                int rateLimitStatus = twitterCtx.RateLimitRemaining;

                if (rateLimitStatus != 0)
                {
                    statusResponse = (from tweet in twitterCtx.Status
                                      where tweet.Type == StatusType.User
                                          && tweet.ScreenName == screenName
                                          && tweet.MaxID == maxID
                                          && tweet.Count == 200
                                          && (tweet.CreatedAt >= startDate && tweet.CreatedAt <= endDate)
                                      select tweet).ToList();

                    lastStatusCount = statusResponse.Count;

                    if (lastStatusCount != 0)
                    {
                        maxID = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;
                        ownTweets.AddRange(statusResponse);
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    flag = false;
                }
            } while (flag);
        }
        if (statusResponse.Count > 0)
        {
            foreach (Status tweet in ownTweets)
            {
                listTweets.Items.Add(tweet.Text);
            }
            lb_tUser.Text = "@" + screenName;
            lb_tweeted.Text = statusResponse.Count.ToString();
        }
    }