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.
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();
}
};
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);
}
}
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();
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);