Get tweets with photos using twitter search api

Can I get tweets containing photos? I am currently using twitter search api and getting all tweets with entity data by setting include_entities = true. I see the details of the image in the media object, but there is still anyway to filter and receive the objects of tweets that have only these media elements. Or anyway on Twitter4j to fulfill this request?

+8
twitter twitter4j twitter-search
source share
3 answers

There is no concrete way to indicate that I only need photos or videos, but you can filter the results based on filter:links or filter:images or filter:videos in your request along with include_entities=true .

Example. To get tweets containing links from 2012-01-31 , you must have the include_entities parameter, as well as filter:links , as shown below:

 https://search.twitter.com/search.json?q=from%3Agoogle%20since%3A2012-01-31%20filter%3Alinks&include_entities=true" 

As you need to filter tweets based on images / photos, I think you should use filter:images .

An example of your case will look like this:

 https://search.twitter.com/search.json?q=from%3Agoogle%20since%3A2012-01-31%20filter%3Aimages&include_entities=true" 

Hope this helps.

+16
source share

With the latest twitter API, I couldn't get the filters to work, and I couldn't find any explanation in my docs. Although you can get all the tweets and then make out only the multimedia ones. You can run this if inside your parsing script:

 if(this.entities.media != null){ //Parse the tweet } 

This is not the best solution, but the worst part is twitter, which gives you more information and uses more of its resources.

+2
source share

In the latest twitter API, you can do this on the ConfigurationBuilder instance before creating the Twitter instance:

 ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(false); cb.setOAuthConsumerKey(API_KEY); cb.setOAuthConsumerSecret(API_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(SECRET_KEY); // enabling include_entities parameters cb.setIncludeEntitiesEnabled(true); Twitter twitterInstance = new TwitterFactory(cb.build()).getInstance(); 

In addition, after including objects in the search bar, you must have the condition "filter: images".

 List<String> keywords = new ArrayList<String>(); keywords.add("#pet"); keywords.add("cat"); // String.join for Java 8 String twitterSearchString = "((" + String.join(" OR ", keywords) + ")"; // adding the filter condition twitterSearchString += " AND filter:images)"; Query q = new Query(twitterSearchString); 

And you will only get results with images (verified using twitter4j-core 4.0.4).

0
source share

All Articles