如何使用twitter4j检索tweets中的图像?

qkf9rpyu  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(478)

我想发出一个关键字或hashtag的查询,并检索包含关键字的所有tweet中的所有图像。我可以使用twitter4j和java轻松地发出查询并检索结果tweets。我知道 http://t.co/x 我可以在浏览器中访问链接并查看相关图像。那张照片在 https://pbs.twimg.com/xx . 看来我要做的就是在我的代码中加入这个过程!
我可以解析 http://t.co/x 链接在每个推特很容易。但是,当我从该链接检索所有html时,我没有看到任何 https://pbs.twimg.com/x 图像:(。我认为twitter正在通过javascript加载这些图像。
有没有什么方法可以让我很容易地检索到每一条推特上的图片??
到目前为止,我的情况是:

package com.company;

import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) throws Exception {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("xxxxxxxxxx")
                .setOAuthConsumerSecret("xxxxxxxxxxxx")
                .setOAuthAccessToken("xxxxxxxxx-xxx-xxxxxxxx")
                .setOAuthAccessTokenSecret("xxxxxxxxxxxxxxxxxxx");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        Query query = new Query("#hashtag");
        QueryResult result = twitter.search(query);
        Pattern pattern = Pattern.compile("http://t.co/\\w{10}");
        Pattern imagePattern = Pattern.compile("https\\:\\/\\/pbs\\.twimg\\.com/media/\\w+\\.(png | jpg | gif)(:large)?");
        for (Status status : result.getTweets()) {
            if (status.isRetweet())
                continue;
            System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
            Matcher matcher = pattern.matcher(status.getText());

                if (matcher.find()) {
                    System.out.println("found a t.co url");
                    URL oracle = new URL(matcher.group());
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(oracle.openStream()));

                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        matcher = imagePattern.matcher(inputLine);

                        if (matcher.find())
                            System.out.println("YAYAAYAYAYYAYAYAYAYAYAYAYAYAAYAYYAYAAYYAYAYAYA: " + matcher.group());
                    }

                    in.close();

            }

        }
    }
}
bvjxkvbb

bvjxkvbb1#

下载所有处于twitter4j状态的媒体

for (MediaEntity m : medias) {
            try {
                URL url = new URL(m.getMediaURL());
                InputStream in = new BufferedInputStream(url.openStream());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int n = 0;
                while (-1 != (n = in.read(buf))) {
                    out.write(buf, 0, n);
                }
                out.close();
                in.close();
                byte[] response = out.toByteArray();
                FileOutputStream fos = new FileOutputStream(file.getAbsolutePath() + "\\" + m.getId() + "." + getExtension(m.getType()));
                fos.write(response);
                fos.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

获取文件扩展名

private String getExtension(String type) {
        if (type.equals("photo")) {
            return "jpg";
        } else if (type.equals("video")) {
            return "mp4";
        } else if (type.equals("animated_gif")) {
            return "gif";
        } else {
            return "err";
        }
    }
p8h8hvxi

p8h8hvxi2#

有一种更简单的方法来检索tweet中的图像。
如果tweet中插入了图像,则可以使用 getMediaEntities() 获取媒体的数据,然后使用 getMediaURL() 你应该这样做

MediaEntity[] media = status.getMediaEntities(); //get the media entities from the status
for(MediaEntity m : media){ //search trough your entities
    System.out.println(m.getMediaURL()); //get your url!
}

相关问题