search fails with maxid (ulong.max) and sinceid set, but separately it works

北城以北 提交于 2019-12-11 05:39:51

问题


the following query works ok if you comment out either the SinceID or the MaxID clause, but when both are included a "bad url" exception is generated.

var maxId = ulong.MaxValue;
var sinceId = (ulong)341350918903701507;

var searchResult =
(
   from search in ctx.Search
      where search.Type == SearchType.Search &&
      search.ResultType == ResultType.Mixed &&
      search.Query == "red wedding" &&
      search.SinceID == sinceId &&
      search.MaxID == maxId &&
      search.IncludeEntities == false &&
      search.Count == 200
    select search).SingleOrDefault();

回答1:


If you look at the query result in Fiddler, you'll see that the response is:

{"errors":[{"code":195,"message":"Missing or invalid url parameter"}]}

I can't respond to why Twitter wouldn't accept the query with both SinceID and MaxID. However, the query is formed correctly and there isn't any documentation describing constraints on the relationship between these two parameters for this particular scenario. The purpose of the MaxID is to be the id of the highest tweet to return on the next query. Both MaxID and SinceID are intended to help you page through data. I wrote a blog post on how to do this:

Working with Timelines with LINQ to Twitter




回答2:


I seem to have the same problem as you are, so the only solution I have was to do it manually, so first I retrieved the the first list setting the sinceId value to the one I have like this:

var searchResult =
            (
                from search in TwitterCtx.Search
                where search.Type == SearchType.Search &&
                      search.Query == query &&
                      search.Count == pageSize &&
                      search.IncludeEntities == true &&
                      search.ResultType == ResultType.Recent &&
                      search.SinceID == sinceId
                select search
            ).SingleOrDefault<Search>();

resultList = searchResult.Statuses;

Then I have to search for other tweets (the case when new tweets count is more the pageSize) so I had a while loop like this:

ulong minId = ulong.Parse(resultList.Last<Status>().StatusID) - 1;
List<Status> newList = new List<Status>();

 while (minId > sinceId)
 {
    resultList.AddRange(newList);

    searchResult =
        (
            from search in TwitterCtx.Search
            where search.Type == SearchType.Search &&
                search.Query == query &&
                search.Count == pageSize &&
                search.IncludeEntities == true &&
                search.ResultType == ResultType.Recent &&
                search.MaxID == minId &&
                search.SinceID == sinceId
             select search
        ).SingleOrDefault<Search>();

    newList = searchResult.Statuses;
    if (newList.Count == 0)
         break;

    minId = ulong.Parse(newList.Last<Status>().StatusID) - 1;
}

Now for some reason here you can use both sinceId and maxId.




回答3:


Just in case anyone else comes across this, I encountered this issue when the MaxId was an invalid Tweet Id.

I started off using zero but ulong.MaxValue has the same issue. Switch it out with a valid value and it works fine. If you're not using SinceId as well it seems to work fine.




回答4:


I used to get same error "Missing or invalid url parameter", but as per Joe Mayo's solution, I have additionally added if(sinceID < maxID) condition before do while loop, because the query throws above error whenever maxID is less than sinceID, I think which is incorrect.

if (sinceID < maxID) 
{
    do
    {
        // now add sinceID and maxID
        searchResponse =
             await
             (from search in twitterCtx.Search
             where search.Type == SearchType.Search &&
                   search.Query == "from:@" + twitterAccountToDisplay + " -retweets" && 
                   search.Count == count &&
                   search.SinceID == sinceID &&
                   search.MaxID == maxID
                   select search)
              .SingleOrDefaultAsync(); 

              if (searchResponse == null)
                 break;

              if (searchResponse.Count > 0 && searchResponse.Statuses.Count > 0)
              {
                 newStatuses = searchResponse.Statuses;
                 // first tweet processed on current query
                 maxID = newStatuses.Min(status => status.StatusID) - 1;
                 statusList.AddRange(newStatuses);

                 lastStatusCount = newStatuses.Count;
              }
              if (searchResponse.Count > 0 && searchResponse.Statuses.Count == 0)
                 {
                    lastStatusCount = 0;
                 }
         }
         while (lastStatusCount != 0 && statusList.Count < maxStatuses);    
         //(searchResponse.Count != 0 && statusList.Count < 30);
}


来源:https://stackoverflow.com/questions/16889209/search-fails-with-maxid-ulong-max-and-sinceid-set-but-separately-it-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!