I'm trying to search twitter by a specific location. I've managed to get it to exclude any tweets geotagged outside of my radius, but I'm also getting a vast majority of tweets with the geodata set to null.
var Twitter = new LinqToTwitter.TwitterContext(auth);
var GeoCode = new LinqToTwitter.Geo()
{
Latitude = 37.68,
Longitude = -97.33,
Accuracy = "20mi"
};
var geostring = $"{GeoCode.Latitude},{GeoCode.Longitude},{GeoCode.Accuracy}";
var searchResponse =
Twitter.Search
.Where(x => x.Type == SearchType.Search)
.Where(x => x.Query == "Trump") //I know people are tweeting this right now...
.Where(x => x.GeoCode == geostring)
.FirstOrDefault();
Now, as said, this excludes tweets specifically tagged as outside of my location & radius, but I would also like to exclude tweets that do not have location data set. Am I doing something wrong here, or am I just going to have to pull the unwanted data and then filter it after the fact?
The reason you're seeing a lot of coordinates that are zero is because a lot of people turn off location on their profile. On the Twitter Search API, there isn't an option to exclude empty locations. What you can do though is perform your query and then do a LINQ to Objects query to filter the result, like this:
var searchResponse =
await
Twitter.Search
.Where(x => x.Type == SearchType.Search)
.Where(x => x.Query == "Trump") //I know people are tweeting this right now...
.Where(x => x.GeoCode == geostring)
.FirstOrDefaultAsync();
var withGeoCode =
(from resp in searchResponse.Statuses
where resp.Coordinates.Latitude != 0 && resp.Coordinates.Longitude != 0
select resp)
.ToList();
BTW, notice that LINQ to Twitter is async
, which is why I added the async
keyword with the FirstOrDefaultAsync
operator. Otherwise, you run the chance of errors or not receiving results because of race conditions. Sometimes, I'm surprised to see that the non-async operators still work.
If the tweet isn't geo-tagged, the Coordinates
Latitude
/Longitude
properties will be 0, which you can see in the code above.