1

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?

Sidney
  • 624
  • 7
  • 20

3 Answers3

1

I don't think you're supposed to combine the wheres in that manner, try:

var searchResponse =
    Twitter.Search
           .Where(x => x.Type == SearchType.Search &&
                       x.Query == "Trump" &&
                       x.GeoCode == geostring)
           .FirstOrDefault();
Nick Coad
  • 3,623
  • 4
  • 30
  • 63
1

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.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
1

If you search for a specific place id, then you will get only tweets with geotags from that place or a narrower place. e.g. tweets for San Francisco will be returned if you search for San Francisco (5a110d312052166f), California or United States. You will also get tweets for neighborhoods of San Francisco.

n.b. This will be a small number of tweets because most tweets are not geotagged.

search for place:ID e.g. place:402e618bff0d9dc1

https://api.twitter.com/1.1/search/tweets.json?q=place%3A5a110d312052166f

You can test this yourself on the website

https://twitter.com/search?q=place%3A5a110d312052166f&src=typd

Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69