Search code examples
twittertwitter4jtwitter-search

Twitter4j Search API , tweets based on location and time Interval


I have a specific requirement that i want to collect all the tweets according to the following parameters

1) Im using search API , for example i want to search for "Iphone6"

2) Region wise , ie if i specify the latitude and longitude as per city I get the results, is it possible to fetch all the results country wise( as in the code when i specity the latitude and longitude of india it doesnt work !)

3) At what intervals should I run my application , so that I get the newly updated tweets , without getting the previously fetched tweets.

This is the code that I have written 


  public final class twitterdate {


    public static void main(String[] args)throws Exception {

        double res;
        double lat,lon;

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("MyKEY")
                .setOAuthConsumerSecret("MySecret")
                .setOAuthAccessToken("MyAccesstoken")

                .setOAuthAccessTokenSecret("MyTokenSecret").setHttpConnectionTimeout(100000);




        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        lat=18.9750;  // THis works , but it doenst work when I specify latitude and longitude of India
        lon=72.8258;
        res=1;

        try {





           QueryResult result=twitter.search(new Query("iphone").since("2014-11-19").until("2014-11-22").geoCode(new GeoLocation(lat, lon), res,"1mi"));
           // Since and untill doesnt work as expected sometimes it fetches the date tweets specified on "since" method sometimes fetches the tweets specified on the date of until method
           // Also since and until doesnt work when i specify a time stamp.
           List<Status> qrTweets = result.getTweets();
              System.out.println("hi");

            for (Status tweet : qrTweets )  
            {   
                 System.out.println( tweet.getId() + " " + "@" + tweet.getUser().getScreenName()  + " : " + tweet.getText() + " :::" + tweet.getCreatedAt() );  
            }

        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}

I would be greatful if somebody can help me with the requirement that I have as I have googled a lot but couldnt find any proper solution. Thanks in advance !


Solution

  • Have you tried using FilterQuery? The code snippet below would give you a continuous stream of tweets. As per my understanding you want to fetch tweets from India with content related to iphone6. With Search query you may end up getting same set of tweets over and again.

    You can try something like this below, I am not sure what co-ordinates you have used to fetch tweets from India !, you have to do some trial and error and fine tune the co-ordinates you want to locate your tweets around.

        StatusListener listener = new StatusListener(){
            public void onStatus(Status status) {
                //if (status.getText().contains)
                if(status.getUser().getLang().equalsIgnoreCase("en")
                        || status.getUser().getLang().equalsIgnoreCase("en_US")) {
                    System.out.println(status.getUser().getName() + " :: " + status.getText() + " :: " + status.getGeoLocation());
                }
            }
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
            public void onException(Exception ex) {
                ex.printStackTrace();
            }
            public void onScrubGeo(long arg0, long arg1) {
    
            }
            public void onStallWarning(StallWarning arg0) {
    
            }
        };
    
        ConfigurationBuilder config = new ConfigurationBuilder();
        config.setOAuthConsumerKey("");
        config.setOAuthConsumerSecret("");
        config.setOAuthAccessToken("");
        config.setOAuthAccessTokenSecret("");
    
        TwitterStream twitterStream = new TwitterStreamFactory(config.build()).getInstance();
        twitterStream.addListener(listener);
        FilterQuery query = new FilterQuery();
        // New Delhi India
        double lat = 28.6;
        double lon = 77.2;
    
        double lon1 = lon - .5;
        double lon2 = lon + .5;
        double lat1 = lat - .5;
        double lat2 = lat + .5;
    
        double box[][] = {{lon1, lat1}, {lon2, lat2}};
    
        query.locations(box);
    
        String[] trackArray = {"iphone"}; 
        query.track(trackArray);
        twitterStream.filter(query);
    

    There is however one caveat with FilterQuery that it uses location OR trackList for fetching data. To counter this may be you can put a content filter logic in onStatus() method.

    Hope this helps you.