【问题标题】:linqtotwitter finding more than 100 searcheslinqtotwitter 发现超过 100 个搜索
【发布时间】:2015-10-14 17:26:23
【问题描述】:

我正在尝试在 twitter 上查找特定关键字的所有可能条目,但使用下面的代码仅返回第 100 个。搜索在第二次迭代时返回 null。谁能告诉我我做错了什么?

    public async void SearchForTweets()
    {
        const int maxNumberToFind = 100;
        const string whatToFind = "iphone";

        ulong lastId = 0;
        var total = 0;
        int count;
        do
        {
            var id = lastId;

            // Get the first 100 records, or the first 100 whose ID is less than the previous set
            var searchResponse =
                await
                    (from search in _twitterCtx.Search
                     where search.Type == SearchType.Search &&
                           search.Query == whatToFind &&
                           search.Count == maxNumberToFind &&
                           (id == 0 || search.MaxID < id)
                     select search)
                        .SingleOrDefaultAsync();

            // Only if we find something
            if (searchResponse != null && searchResponse.Statuses != null)
            {
                // Out put each tweet found
                searchResponse.Statuses.ForEach(tweet =>
                    Console.WriteLine(
                        "{4} ID: {3} Created: {2} User: {0}, Tweet: {1}",
                        tweet.User.ScreenNameResponse,
                        tweet.Text,
                        tweet.CreatedAt,
                        tweet.StatusID,
                        DateTime.Now.ToLongTimeString()));

                // Take a note of how many we found, and keep a running total
                count = searchResponse.Statuses.Count;
                total += count;

                // What is the ID of the oldest found (Used to limit the next search)
                lastId = searchResponse.Statuses.Min(x => x.StatusID);
            }
            else
            {
                count = 0;
            }
        } while (count == maxNumberToFind); // Until we find less than 100
        Console.Out.WriteLine("total = {0}", total);
    }

【问题讨论】:

标签: c# twitter


【解决方案1】:

解决了,

search.MaxID < id

需要

search.MaxID == id

【讨论】:

    【解决方案2】:

    这是一个完整的功能

    public static List<Status> search(string searchTerms, int maxPagination)
    {
        var twitterCtx = new TwitterContext(authorizer);
        List<Status> searchResults = new List<Status>();
        int maxNumberToFind = 100;
        int pagination = 0;
        ulong lastId = 0;
        int count = 0;
    
        do
        {
            var id = lastId;
            var tweets = Enumerable.FirstOrDefault(
                            from tweet in twitterCtx.Search
                            where tweet.Type == SearchType.Search &&
                                tweet.Query == searchTerms &&
                                tweet.Count == maxNumberToFind && (tweet.MaxID == id)
                            select tweet);
    
            searchResults.AddRange(tweets.Statuses.ToList());
            lastId = tweets.Statuses.Min(x => x.StatusID); // What is the ID of the oldest found (Used to get the next pagination(results)
            pagination++;
    
            count = (pagination > maxPagination) ? 0 : tweets.Count; // Limit amount of search results
        } while (count == maxNumberToFind);
    
        return searchResults;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 2020-01-16
      • 2018-11-06
      相关资源
      最近更新 更多