Twitter4j - 从流API中访问推文信息

5
我的目标是收集所有包含“法国”和“德国”这两个词的推文,并收集相关元数据(例如,附加到推文的地理坐标)。我知道这些元数据是可用的,但我无法弄清如何使用我正在使用的Java库“twitter4j”访问它。
好的,那么我目前所拥有的来自于Twitter4j网站上的代码示例。它会实时打印出包含我选择关键字的所有推文,这些推文由Twitter的流API提供。我在我的TwitterStream对象上调用过滤方法,这提供了流。但我需要更多的控制。即,我想能够:
1)将推文写入文件; 2)只打印前1000条推文; 3)访问附加到推文的其他元数据(过滤方法只打印用户名和推文本身)。
以下是我目前拥有的代码:
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;

public class Stream {
    public static void main(String[] args) throws TwitterException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey("bbb");
    cb.setOAuthConsumerSecret("bbb");
    cb.setOAuthAccessToken("bbb");
    cb.setOAuthAccessTokenSecret("bbb");

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };

    FilterQuery fq = new FilterQuery();
    String keywords[] = {"France", "Germany"};

    fq.track(keywords);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);      
}
}

需要帮忙。我得到了以下错误。'new StatusListener(){} 类型必须实现继承的抽象方法StatusListener.onStallWarning(StallWarning)' - ONE_FE
你使用的是哪个版本的Twitter4J?我的里面好像没有包含TwitterStream类。我正在使用3.0版本。 - Martin Erlic
3个回答

5

仔细观察后,我意识到解决方案其实很显然。只需编辑以下代码部分:

public void onStatus(Status status) {
        System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
    }

允许我访问其他元数据。例如,如果我想访问推文的日期,只需添加以下内容:

System.out.println(status.getCreatedAt());

0

将推文写入文件:

FileWriter file = new FileWriter(....);

public void onStatus(Status status) {
    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " -> "+ status.getCreatedAt());
    try {
        file.write(status.getUser().getScreenName() + " - " + status.getText() + " -> "+ status.getCreatedAt() +"\n");
        file.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

0

当API尝试访问当前无法获取的某些信息时,会出现错误401。因此,您需要检查在Twitter上允许的权限。将其更改为READ、WRITE和...以获得完整的API访问权限。或者可能存在问题,因为您可能正在使用代理服务器。因此,请使用以下命令提供代理详细信息。

 System.getProperties().put("http.proxyHost", "10.3.100.211");
        System.getProperties().put("http.proxyPort", "8080");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接