如何获得超过几个月的推文?

时间:2017-01-11 14:15:05

标签: c# twitter linq-to-twitter

我正在使用带有特定主题标签的 LINQ to Twitter 4.1.0 检索推文。但我面临的问题是我只收到600条推文。 我想收到超过几个月的推文。 任何人都可以建议我,怎么做?

 [List<Search> searchResultsList = new List<Search>();
        ulong lastId = 0;
        var context = new TwitterContext(auth);
        int count = 0;
        int maxNumberToFind = 1000;
        var dateFrom = DateTime.Now.AddDays(-20);

        var searchResults =
                         (from search in context.Search
                          where search.Type == SearchType.Search &&
                                search.Query == Query &&
                                search.SinceID == 2016-12-11 &&                                
                                search.Count == 150 &&
                                search.ResultType == ResultType.Recent &&
                                search.IncludeEntities == true
                          select search).ToList();][1]

2 个答案:

答案 0 :(得分:2)

我建议查看documentation看起来您可以指定&#34;直到&#34; date和MaxId:

  

直到:推文至今,YYYY-MM-DD。 (字符串)

     

MaxID:在此ID之前或之前返回推文。 (ULONG)

我会查询&#34;直到&#34;日期并使用LINQ&#34; LastOrDefault()&#34;获取最后一条推文。然后使用该对象中的ID作为下一个查询的MaxID进行另一个查询。这可以在您要求的特定日期之前获取所有推文。

注意这个参数也很重要:

  

计算每页要检索的推文数。最大值为100.(int)

这可能与您收到的推文数量有限有关。

Joe Mayo(LinqToTwitter的创建者)的

编辑 This post也可能对您有所帮助。这是一些代码(免责声明:我还没有测试过这个,但你明白了):

List<Search> searchResultsList = new List<Search>();
    var context = new TwitterContext(auth);
    var maxCount = 100;
    var untilDate = new DateTime(2016,12,11);

    var lastTweetInRange =
                     (from search in context.Search
                      where search.Type == SearchType.Search &&
                            search.Query == Query &&                               
                            search.Count == maxCount &&
                            search.Until == untilDate &&
                            search.IncludeEntities == true
                      select search).LastOrDefault();

    var tweetsInRange = (from search in context.Search
                        where search.Type == SearchType.Search &&
                                search.Query == Query &&                               
                                search.Count == maxCount &&
                                search.MaxId == lastTweetInRange.id &&
                                search.IncludeEntities == true
                        select search).ToList();

答案 1 :(得分:1)

您可能还想考虑分页搜索。这是一个演示:

    static async Task DoPagedSearchAsync(TwitterContext twitterCtx)
    {
        const int MaxSearchEntriesToReturn = 100;

        string searchTerm = "twitter";

        // oldest id you already have for this search term
        ulong sinceID = 1;

        // used after the first query to track current session
        ulong maxID; 

        var combinedSearchResults = new List<Status>();

        List<Status> searchResponse =
            await
            (from search in twitterCtx.Search
             where search.Type == SearchType.Search &&
                   search.Query == searchTerm &&
                   search.Count == MaxSearchEntriesToReturn &&
                   search.SinceID == sinceID
             select search.Statuses)
            .SingleOrDefaultAsync();

        combinedSearchResults.AddRange(searchResponse);
        ulong previousMaxID = ulong.MaxValue;
        do
        {
            // one less than the newest id you've just queried
            maxID = searchResponse.Min(status => status.StatusID) - 1;

            Debug.Assert(maxID < previousMaxID);
            previousMaxID = maxID;

            searchResponse =
                await
                (from search in twitterCtx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == searchTerm &&
                       search.Count == MaxSearchEntriesToReturn &&
                       search.MaxID == maxID &&
                       search.SinceID == sinceID
                 select search.Statuses)
                .SingleOrDefaultAsync();

            combinedSearchResults.AddRange(searchResponse);
        } while (searchResponse.Any());

        combinedSearchResults.ForEach(tweet =>
            Console.WriteLine(
                "\n  User: {0} ({1})\n  Tweet: {2}",
                tweet.User.ScreenNameResponse,
                tweet.User.UserIDResponse,
                tweet.Text));
    }

我在一段时间后写了一篇博文,一般性地解释了它是如何工作的:

Working with Timelines with LINQ to Twitter

它有点陈旧,不包含异步语法,但确实解释了SinceIDMaxID和技术。 Twitter也有很好的文档,解释了他们的分页策略的方式和原因:

Working with Timelines (Twitter)

话虽如此,Twitter API确实限制了您可以在多长时间内进行搜索。在The Search API, Best Practices部分,他们描述了他们只会回到6到9天。