Get Tweets with Pictures using twitter search api

*爱你&永不变心* 提交于 2019-11-27 12:56:40
Abhishek Sivasubramanian

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

For Example: To get the tweets that contain links since 2012-01-31, you query should have include_entities parameter as well as filter:links as shown as follows:

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

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

An example of your case would look like:

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

Hope this helps.

With Latest twitter API I couldn't get filters work and I couldn't find either any explanation in their docs. Althought you can get all the tweets and then parse only the media ones. You can fire this if inside your parsing script:

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

This is not the best solution but the worst part comes to twitter who's giving you more information and using more of its own resources.

In the lastest twitter API you can do it in 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();

Also, after enabling the entities, in the search string you have to had 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 get just results with images (tested with twitter4j-core 4.0.4).

For filter in twitter API you can check official document for latest version as on date Apr 02 2019

https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!