如何从使用LINQ的用户那里获得更多的推文

本文关键字:那里 用户 LINQ | 更新日期: 2023-09-27 18:08:58

它清楚地表明它允许每个人最多获得3200条tweet(参见文档)。但是使用下面的代码:

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到推特代码,允许我获取尽可能多的推文?

如何从使用LINQ的用户那里获得更多的推文

Twitter中获取tweets有两种方法:

使用HastagScreenName

我创建了一个自定义方法,您可以在一个请求中获得3200条tweet。

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);
                }
            }
        }

文档中说"获取授权用户最近的20个状态"。

可能必须使用/发送额外的"参数/过滤器"- count如文档中提到的,其中声明"要返回的tweet数;这是一个可选字段,因此在您的情况下只返回前20名。

摘自@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();
        }
    }