public void Can_search_with_geo_and_lang() { var italyGeoCode = new TwitterGeoLocationSearch(41.9, 12.5, 10, TwitterGeoLocationSearch.RadiusType.Mi); var service = GetAuthenticatedService(); var results = service.Search(new SearchOptions { Q = "papa", Geocode = italyGeoCode, Lang = "en", Count = 100, }); Assert.IsNotNull(results); if (!results.Statuses.Any()) { Assert.Inconclusive("No tweets to check the location of to match within search radius"); } Assert.IsTrue(results.Statuses.Count() <= 100); var geoTaggedTweets = results.Statuses.Where(x => x.Location != null); if (!geoTaggedTweets.Any()) { Assert.Inconclusive("Unable to find tweets that were geo tagged for this test"); } foreach (var tweet in geoTaggedTweets) { Console.WriteLine("{0} says '{1}' ({2})", tweet.User.ScreenName, tweet.Text, tweet.Id); //Twitter API does not return coordinates in search request Assert.IsTrue(tweet.IsWithinSearchRadius(italyGeoCode)); } }
public static bool IsWithinSearchRadius(this TwitterStatus s, TwitterGeoLocationSearch searchArea) { if (s.Location == null) return false; if (searchArea == null) return false; // (x-center_x)^2 + (y - center_y)^2 < radius^2 var x = s.Location.Coordinates.Latitude; var centerX = searchArea.Coordinates.Latitude; var y = s.Location.Coordinates.Longitude; var centerY = searchArea.Coordinates.Longitude; var radius = searchArea.Radius; return Math.Pow((x - centerX), 2) + Math.Pow((y - centerY), 2) < Math.Pow(radius, 2); }
public IEnumerable <TwitterStatus> Search(string query) { var tweets = new List <TwitterStatus>(); var dayBefore = DateTime.Today.AddDays(-1); if (_service == null) { AuthApi(); } if (!query.StartsWith("#")) { query = "#" + query.ToLower(); } var code = new TwitterGeoLocationSearch(39.50, -98.35, 1500, TwitterGeoLocationSearch.RadiusType.Mi); var geoSearch = _service.Search(new SearchOptions { Q = query, Geocode = code, Count = Convert.ToInt32(100000) }).Statuses; var geoStatuses = geoSearch as TwitterStatus[] ?? geoSearch.ToArray(); tweets.AddRange( geoStatuses.Where( tweet => DateTime.Compare(tweet.CreatedDate, dayBefore) > -1)); var fullSearch = _service.Search(new SearchOptions { Q = query, Count = Convert.ToInt32(100000) }); var fullStatuses = fullSearch.Statuses as IList <TwitterStatus> ?? fullSearch.Statuses.ToList(); tweets.AddRange( fullStatuses.Where( tweet => // !geoStatuses.Contains(tweet) && DateTime.Compare(tweet.CreatedDate, dayBefore) > -1)); tweets.Sort((t1, t2) => DateTime.Compare(t1.CreatedDate, t2.CreatedDate)); tweets.Reverse(); return(tweets.AsEnumerable()); }
public static bool IsWithinSearchRadius(this TwitterStatus s, TwitterGeoLocationSearch searchArea) { if (s.Location == null) { return(false); } if (searchArea == null) { return(false); } // (x-center_x)^2 + (y - center_y)^2 < radius^2 var x = s.Location.Coordinates.Latitude; var centerX = searchArea.Coordinates.Latitude; var y = s.Location.Coordinates.Longitude; var centerY = searchArea.Coordinates.Longitude; var radius = searchArea.Radius; return(Math.Pow((x - centerX), 2) + Math.Pow((y - centerY), 2) < Math.Pow(radius, 2)); }