linq-to-twitter Twitter flood get

126 Views Asked by At

I tried the code below but again it wasn't exactly as I wanted. Only 1 pearl flood is coming. There are 90 floods. RT ones should not come and should only come flood by call.

as an example I shared the picture. What do I have to do in this situation.

https://hizliresim.com/bltCKn

  const int MaxSearchEntriesToReturn = 100;
        const int SearchRateLimit = 180;

        string searchTerm = "HANEDANLAR MASASININ YER ALTI EGEMENLİĞİ:RİO TİNTO";

        // 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 ctx.Search
             where search.Type == SearchType.Search &&
                   search.Query == searchTerm &&
                   search.Count == MaxSearchEntriesToReturn &&
                   search.SinceID == sinceID &&
                   search.TweetMode == TweetMode.Extended
             select search.Statuses)
            .SingleOrDefaultAsync();

        if (searchResponse != null)
        {
            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 ctx.Search
                     where search.Type == SearchType.Search &&
                           search.Query == searchTerm &&
                           search.Count == MaxSearchEntriesToReturn &&
                           search.MaxID == maxID &&
                           search.SinceID == sinceID &&
                           search.TweetMode == TweetMode.Extended
                     select search.Statuses)
                    .SingleOrDefaultAsync();

                combinedSearchResults.AddRange(searchResponse);
            } while (searchResponse.Any() && combinedSearchResults.Count < SearchRateLimit);


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

                    tweet.User.ScreenNameResponse,
                    tweet.User.UserIDResponse,
                    tweet.Text ?? tweet.FullText)
                );
        }
        else
        {
            Console.WriteLine("No entries found.");
        }

        ViewBag.Twet = combinedSearchResults.ToList();
1

There are 1 best solutions below

1
Joe Mayo On

I own LINQ to Twitter. A paged search can return more values. Here's an example:

const int MaxSearchEntriesToReturn = 100;
const int SearchRateLimit = 180;

string searchTerm = "Flood Name";

// 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 &&
           search.TweetMode == TweetMode.Extended
     select search.Statuses)
    .SingleOrDefaultAsync();

if (searchResponse != null)
{
    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 &&
                   search.TweetMode == TweetMode.Extended
             select search.Statuses)
            .SingleOrDefaultAsync();

        combinedSearchResults.AddRange(searchResponse);
    } while (searchResponse.Any() && combinedSearchResults.Count < SearchRateLimit);

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

There are a few things to pay attention to:

  • Set Count to MaxSearchEntriesToReturn because it defaults to 15 and you want to minimize the number of queries.
  • The while loop checks SearchRateLimit because there are rate limits that will cause you to get an HTTP 429. The rate limit for App-Only is higher than the 180 I've added here.
  • Notice how I'm using SinceID and MaxID to page through results. See Working with Timelines in the Twitter API docs to understand what those are.
  • Also, please read the Search API docs and notice that the standard search API is focused on relevance and not completeness.