2

Anyone know if it's possible to get ONLY the tweet that contain entities (Hashtag and Photos) with twitter4j ?

Here is my code, it works fine but I get all the tweet including the ones that don't have entities.

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
        .setOAuthConsumerKey(TwitterConstantes.APIKEY)
        .setOAuthConsumerSecret(TwitterConstantes.APIKEYSECRET)
        .setOAuthAccessToken(TwitterConstantes.TOKEN)
        .setOAuthAccessTokenSecret(TwitterConstantes.TOKENSECRET);

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

//Query query = new Query(this.text);
Query query = new Query().geoCode(new GeoLocation(latitude,longitude), res, Query.KILOMETERS.toString()); 
query.setResultType(Query.RECENT); // get the recent tweets
query.count(100);
QueryResult result = twitter.search(query);
do {

    for (Status tweet : result.getTweets()) {
        //if (tweet.isRetweet()){continue;}         
        try {
            Status tweetById = twitter.showStatus(tweet.getId());
            String url= "https://twitter.com/" + tweetById.getUser().getScreenName() 
                    + "/status/" + tweetById.getId();

            List<String> hashtags =  new ArrayList<String>();
            HashtagEntity[] hashtagsEntities = tweetById.getHashtagEntities();
            for (HashtagEntity hashtag : hashtagsEntities){
                System.out.println(hashtag.getText());
            }

            ExtendedMediaEntity[] medias = tweetById.getExtendedMediaEntities();
            for (ExtendedMediaEntity m : medias){
                System.out.println(m.getMediaURL());    
            }

        } catch (TwitterException e) {
            System.err.print("Failed to search tweets: " + e.getMessage());
            return;
        }
    }
    query = result.nextQuery();
    if(query!=null){
        result = twitter.search(query);
    }

}while(query!=null);
Leb
  • 15,483
  • 10
  • 56
  • 75
afaraut
  • 192
  • 12
  • As far as I remember entities were inside the tweet itself, so you should filter by the tweet string (check if contains a link) – Joaquin Iurchuk Oct 13 '15 at 18:16
  • Yes you're right ! But with the search api from twitter you can do it. Like that : https://api.twitter.com/1.1/search/tweets.json/ **filter=images&count=100&include_entities=1** http://stackoverflow.com/questions/13920851/get-tweets-with-pictures-using-twitter-search-api – afaraut Oct 13 '15 at 18:19
  • Then process the list of results – Joaquin Iurchuk Oct 13 '15 at 18:22
  • Yes, but the number of queries are limited. So I want to optimise them – afaraut Oct 13 '15 at 18:24

0 Answers0