how to get media related info using twitter4j

允我心安 提交于 2019-12-20 15:34:52

问题


I am new to twitter4j. How i can get media related info using twitter4j library? I want to get info about photos,videos and article summary present in tweet.Thanks in advance.Is getMediaEntities() method is enough for this purpose?


回答1:


Try hitting this request:

https://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter&include_entities=true

Then, look for any media_url elements; those will contain the URL to any photos. It may contain video links, so you'll have to filter for common image types (.png/.jpg/.gif/etc).

Check this as well: https://dev.twitter.com/docs/tweet-entities




回答2:


Yes Status#getMediaEntities() is what you want but you can only get basic information about the media contained in the Tweet. For example if you wanted to search for Tweets matching a criteria and get media entities' types and URLs you could do:

Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query("...");
QueryResult result = twitter.search(query);
for (Status status : result.getTweets()) {
    for (MediaEntity mediaEntity : status.getMediaEntities()) {
        System.out.println(mediaEntity.getType() + ": " + mediaEntity.getMediaURL());
    }
}

There are other methods of retrieving Tweets using Twitter4J, e.g. using the Streaming API, take a look at the code examples on the Twitter4J web site for more information.



来源:https://stackoverflow.com/questions/17294908/how-to-get-media-related-info-using-twitter4j

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