Twitter4j - finding users by geolocation

edited November 2013 in Library Questions

Hi everyone, I'm working on a sketch in which I need to find a random user from a specific location and @mention them in a tweet.

-So far I have not been able to do it, mostly because I'm just not an experienced coder and have never dealt with the twitter API before. -But this is the frankestein's monster code what I'm trying:-

I've made some progress, please look at comment below.

I appreciate any help! Thanks in advance!

Answers

  • Here is my new version: import twitter4j.conf.*; import twitter4j.*; import twitter4j.auth.*; import twitter4j.api.*; import java.util.*;

    //package twitter4j.examples.search;
    
    String ConsumerKey = "mykey";
    String ConsumerSecret = "mykey";
    String oauth_token = "mykey";
    String oauth_token_secret = "mykey";
    
    TwitterFactory tf;
    
    String geoURL;
    double lat;
    double lon;
    double res;
    String resUnit;
    
    List<Status> tweets;
    
    import java.util.List;
    
    void setup() {
      println("setup");
      println();
      geoURL = "http://search.twitter.com/search.json?geocode=";
      println("geoURL" + geoURL);
      println();
      ConfigurationBuilder cb = new ConfigurationBuilder(); //ConfigurationBuilder declared cb is a new instance of this
      cb.setDebugEnabled(true) 
        .setOAuthConsumerKey(ConsumerKey) //sets the Consumer Key String
          .setOAuthConsumerSecret(ConsumerSecret) //sets the Consumer Secret String
            .setOAuthAccessToken(oauth_token)
              .setOAuthAccessTokenSecret(oauth_token_secret);
      tf = new TwitterFactory(cb.build());
    }
    
    void draw() {
    
      println("void draw");
      println();
      Twitter twitter = tf.getInstance();
    
      lat = 10.5000;
      lon = 66.9167;
      res = 10;
      resUnit="mi";
    
    
    
      try {
        QueryResult result = twitter.search(new Query().geoCode(new GeoLocation(lat, lon), res, resUnit));
        //println(result);
    
        // printing my tweets
        List <Status> tweets = result.getTweets();
        println("tweets: " + tweets);
    
    
    
        exit();
      }  
    
      catch (TwitterException te) {
        te.printStackTrace();
        println("Failed to search tweets: " + te.getMessage());
        exit();
      }
    }
    

    I've managed to get 15 tweets with all their info from the required location. Now all that I'm missing is a way to isolate and select a screenName from the List. Suggestions?

  • I now have working code that does the first part of what I want. Query twitter and return a single random user name based on geoLocation.

    Here's my code for future reference:

    import twitter4j.conf.*;
    import twitter4j.*;
    import twitter4j.auth.*;
    import twitter4j.api.*;
    import java.util.*;
    
    //package twitter4j.examples.search;
    
    String ConsumerKey = "*********************";
    String ConsumerSecret = "*********************";
    String oauth_token = "*********************";
    String oauth_token_secret = "*********************";
    
    TwitterFactory tf;
    
    String geoURL;
    double lat;
    double lon;
    double res;
    String resUnit;
    
    List<Status> tweets;
    
    import java.util.List;
    
    void setup() {
      println("setup");
      println();
      geoURL = "http://search.twitter.com/search.json?geocode=";
      println("geoURL" + geoURL);
      println();
      ConfigurationBuilder cb = new ConfigurationBuilder(); //ConfigurationBuilder declared cb is a new instance of this
      cb.setDebugEnabled(true) 
        .setOAuthConsumerKey(ConsumerKey) //sets the Consumer Key String
          .setOAuthConsumerSecret(ConsumerSecret) //sets the Consumer Secret String
            .setOAuthAccessToken(oauth_token)
              .setOAuthAccessTokenSecret(oauth_token_secret);
      tf = new TwitterFactory(cb.build());
    }
    
    void draw() {
    
      println("void draw");
      println();
      Twitter twitter = tf.getInstance();
    
      lat = 10.5000;
      lon = 66.9167;
      res = 10;
      resUnit="mi";
    
    
    
      try {
        // Send a geoLocation query to Twitter
        QueryResult result = twitter.search(new Query().geoCode(new GeoLocation(lat, lon), res, resUnit));
        //println(result);
    
        // Get my Tweets
        List <Status> tweets = result.getTweets();
        // Pick a random tweet
        int randomTweet = round(random(0, 14));
        Status status = tweets.get(randomTweet);
    
        String twitterUser = status.getUser().getScreenName();
        println(twitterUser);
    
    
        exit();
      }  
    
      catch (TwitterException te) {
        te.printStackTrace();
        println("Failed to search tweets: " + te.getMessage());
        exit();
      }
    }
    
  • edited November 2013

    So after looking into this, it turns out this code will only return the latest tweets from the location and print the usernames, so if a user tweeted more than once that user will be duplicated in the results. I really need help with this, it's been a lot of work and I just can't figure this out.

    void getTwitterUser() {
      try {
        int tweetCount = 100;
        // Send a geoLocation query to Twitter
        //ResponseList result = twitter.searchUsers("caracas", 5);
        QueryResult result = twitter.search(new Query().geoCode(new GeoLocation(lat, lon), res, resUnit).count(tweetCount));
    
        // Get my Tweets
       List <Status> tweets = result.getTweets();
    
    
        for (int i = 0; i < tweetCount; i++) {
          Status status = tweets.get(i);
    
          twitterUser = status.getUser().getScreenName();
    
          println(twitterUser +" " + i);
        }
      }  
    
      catch (TwitterException te) {
        te.printStackTrace();
        println("Failed to search tweets: " + te.getMessage());
      }
    }
    

    I found the .searchUsers() function, not sure how to create a query for it properly.

    Any help is greatly appreciated

Sign In or Register to comment.