1

Ok. Each time we get only 100 tweets. I want to show them on a map based on the Geolocation. Is there any way I can get 100 tweets that all of them have Geolocation. Because now only 3-4 out of 100 has Geolocation.

I read some examples with FilterQuery but Processing throws me an error on it, as unknown.

dionysosz
  • 143
  • 1
  • 5
  • 17
  • Please post your code, and name the error it throws. – tnagel Aug 01 '14 at 08:43
  • An way using stream API: http://stackoverflow.com/questions/12540223/twitter4j-how-to-get-only-statuses-with-location-data-from-a-twitter-stream – v.k. Aug 01 '14 at 13:43

4 Answers4

3

Here is an working example. This was tested using twitter4j 4.0.2

The interface StatusListener is where you can do whatever you want with your tweets Kind of... is there that they arrive

Using stream API:

import twitter4j.util.*;
import twitter4j.*;
import twitter4j.management.*;
import twitter4j.api.*;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.auth.*;


TwitterStream twitterStream;


double[][] boundingBox= {
  {
    -180, -90
  }
  , {
    180, 90
  }
}; /// whole world;

void setup() {     
  size(100, 100);    
  background(0); 
  openTwitterStream();
}  


void draw() {     
  background(0);
}  



// Stream it
void openTwitterStream() {  

  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey(FILL_IN);
  cb.setOAuthConsumerSecret(FILL_IN);
  cb.setOAuthAccessToken(FILL_IN);
  cb.setOAuthAccessTokenSecret(FILL_IN);


  TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
  twitterStream.addListener(listener);

  FilterQuery filter = new FilterQuery();
  filter.locations(boundingBox);


  twitterStream.filter(filter);

  println("connected");
} 


// Implementing StatusListener interface
StatusListener listener = new StatusListener() {

  //@Override
  public void onStatus(Status status) {
    GeoLocation loc = status.getGeoLocation();
    System.out.println("@" + status.getUser().getScreenName() + " - " + loc);
  }

  //@Override
  public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
    System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
  }

  //@Override
  public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
    System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
  }

  //@Override
  public void onScrubGeo(long userId, long upToStatusId) {
    System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
  }

  //@Override
  public void onStallWarning(StallWarning warning) {
    System.out.println("Got stall warning:" + warning);
  }

  //@Override
  public void onException(Exception ex) {
    ex.printStackTrace();
  }
};
v.k.
  • 2,826
  • 2
  • 20
  • 29
0
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
//import twitter4j.FilterQuery;
import java.util.*;


Twitter twitter;
String searchString = "shopping";


Query query; 


ArrayList tweets,tweets2;

void setup()
{
  frameRate(0.2);
  tweets = new ArrayList();

  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("*");
  cb.setOAuthConsumerSecret("*");
  cb.setOAuthAccessToken("*");
  cb.setOAuthAccessTokenSecret("*");

  twitter = new TwitterFactory(cb.build()).getInstance();
  query = new Query(searchString);
  getNewTweets();
}

void draw()
{


  try{

    if (current >= tweets.size()) //if u read all the received tweets make a new query 
    {  
       tweets.clear();
       tweets2.clear();
      getNewTweets();
        current = 0;
    }



      Status currentTweet = (Status) tweets.get(current);
      GeoLocation loc = currentTweet.getGeoLocation();
      User currentUser=(User) currentTweet.getUser(); //Get User info



        double latitude = currentTweet.getGeoLocation().getLatitude();
        double longitude = currentTweet.getGeoLocation().getLongitude();


        println( "Longtitude: "+longitude);
        println( "Latitude:  "+latitude);




        String user = currentUser.getScreenName();
        String msg = currentTweet.getText();

        println( "User: "+user);
        println("Message: "+ msg);



}

 catch (Exception te) {
      println(te);
  }




   }

   void getNewTweets()
{


    try 
    {


//     FilterQuery filtro = new FilterQuery();
   /* filtro.locations(boundingBox);
    twitter.addListener(listener);
    twitter.filter(filtro);
      */


    query.setCount(100); //sets the number of tweets to return per page
    QueryResult result = twitter.search(query); 
    tweets2 = (ArrayList) result.getTweets(); 

    for (int i = 0; i < tweets2.size(); i++) { 
     Status currentTweet = (Status) tweets2.get(i);
     GeoLocation loc = currentTweet.getGeoLocation();

      if(loc!=null){ //add to list only tweets with geo location
        tweets.add(tweets2.get(i));         
       }

    }
    } 
    catch (TwitterException te) 
    {
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    } 
}
v.k.
  • 2,826
  • 2
  • 20
  • 29
dionysosz
  • 143
  • 1
  • 5
  • 17
  • 1
    I believe this way you are gettong all tweets and only adding ones with GeoLocation data, so you still getting 100 results with and without GeoLocation data. I don't know though if it is possible to get only ones with GeoLocation using the search API. Only by using stream API as pointed above. I got no success in my tries... – v.k. Aug 01 '14 at 16:22
  • Yeah you are right. I am getting all the tweets and then adding the ones with GeoLocation data. I did this because I could find a way to get 100 tweets with GeoLocation data. I thought it was a good way, but unfortunately not many tweets have GeoLocation data. let's say 5/100 . So that's why I am asking how to get 100 tweets with GeoLocation data. – dionysosz Aug 01 '14 at 16:26
0

Not an answer but a Simple workaround:

I know most of people don't have GPS enabled when they tweet and others would not like to share their location!

But they are still sharing their location!! Guess how? On their profiles! Their hometown, their country is mostly visible, which can give you an approximate location of where the tweet came from! You can query for the user's profile and thus his/her location using the Rest API

twitter.showUser(userScreenName).getLocation();

Sam
  • 822
  • 2
  • 8
  • 30
0

I think if you setGeocode before u search,then the majority of result will has Geolocation(up to 90%). but i dont know why not whole of result with geolocation. maybe it will a little helpful?

   GeoLocation obj = new GeoLocation(35.8007019, -97.3383211);
   query.setGeoCode(obj, 2000, Unit.valueOf("km"));
   result = twitter.search(query);