开发者

TweetSharp 2.0 search and Geocode

开发者 https://www.devze.com 2023-03-01 05:11 出处:网络
I\'m trying to get TweetSharp to perform a search on tweets from within a certain area. Unfortunately this always returns 0 results.

I'm trying to get TweetSharp to perform a search on tweets from within a certain area. Unfortunately this always returns 0 results.

public IEnumerable<TwitterSearchStatus> Search(string terms)
{
  TwitterSearchResult result = ServiceManager.Instance.service.Search(terms,100);
  IEnumerable<TwitterSearchStatus> returnValue = result.Statuses;
  return returnValue;
}

public IEnumerable<TwitterSearchStatus> SearchGeolocation(string terms)
{
  return Search(terms + "&geocode=51.50788772102843,-0.102996826171875,50mi");
}

This seems to generate the query with a & and % characters escaped which the service then does not convert meaning I get the error you must enter a query:

http://search.twitter.com/search.json?test%26geocode%3D51.50788772102843%2C-0.102996826171875%2C50mi

If I run this 开发者_如何学编程query with the characters in place it appears to work fine:

http://search.twitter.com/search.json?test&geocode=51.50788772102843,-0.102996826171875,50mi

Does anyone have any suggestions?


I was able to get TweetSharp's search to support the geocode parameter by following these steps:

  1. Download TweetSharp's latest source code
  2. Unpack the zip file to a directory (let's call this tsSource).
  3. Open the TweetSharp solution under the tsSource\src folder.
  4. In the VS solution explorer, find _TwitterService.Search.json in the .NET 40\TweetSharp\Generated folder.
  5. Insert the following at line 2 in the file: TwitterSearchResult, "search", Search, string q, string geocode
  6. In solution explorer, right click on TwitterService.tt in the .NET 40\TweetSharp\Generated folder and select Run Custom Tool; Confirm the warning dialog.
  7. Build the TweetSharp project after code generation is complete.

The TweetSharp.dll assembly located in the tsSource\bin\lib\4.0 directory will contain the new method. You would call the new method like so:

service.Search("test", "51.50788772102843,-0.102996826171875,50mi");

Alternatively, you could use Twitterizer to support your search queries. Its search feature does not over-escape the querystring parameters, and it should be able to run side by side with TweetSharp. If you go this route, the search query code would look like this:

//reference Twitterizer2.dll

var tokens = new Twitterizer.OAuthTokens {
  ConsumerKey = @"consumerKey",
  ConsumerSecret = @"consumerSecret",
  AccessToken = @"accessToken",
  AccessTokenSecret = @"accessTokenSecret"
};

var response = Twitterizer.TwitterSearch.Search(tokens, "test", 
  new Twitterizer.SearchOptions { 
    GeoCode = "51.50788772102843,-0.102996826171875,50mi" 
  });
if (response.Result != Twitterizer.RequestResult.Success)
  return;

foreach (var status in response.ResponseObject)
{
  Console.WriteLine(status.Text);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号