List of usage examples for twitter4j FilterQuery FilterQuery
public FilterQuery()
From source file:org.xmlsh.twitter.stream.java
License:BSD License
@Override public int run(List<XValue> args) throws Exception { Options opts = new Options(sCOMMON_OPTS + ",p=port:,track:,sample,json,sanitize", SerializeOpts.getOptionDefs()); opts.parse(args);//from w w w .java2 s. com mSerializeOpts = this.getSerializeOpts(opts); final boolean bJson = opts.hasOpt("json"); final boolean bSanitize = opts.hasOpt("sanitize"); args = opts.getRemainingArgs(); final OutputPort port = mShell.getEnv().getOutputPort(opts.getOptStringRequired("port"), true); StatusListener listener = new StatusListener() { public void onStatus(Status status) { try { if (bJson) { String json = DataObjectFactory.getRawJSON(status); PrintWriter writer = port.asPrintWriter(mSerializeOpts); writer.println(json); writer.close(); } else { TwitterWriter mWriter = new TwitterWriter(port.asXMLStreamWriter(mSerializeOpts), bSanitize); mWriter.startDocument(); mWriter.startElement(TwitterWriter.kTWITTER_NS, "twitter"); mWriter.writeDefaultNamespace(); mWriter.write("status", status); mWriter.endElement(); mWriter.endDocument(); mWriter.closeWriter(); port.writeSequenceTerminator(mSerializeOpts); } } catch (Exception e) { onException(e); } } 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(); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; try { TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. // FilterQuery filter = new FilterQuery().track(Util.toStringArray(args)); // twitterStream.filter(filter); if (opts.hasOpt("sample")) twitterStream.sample(); else twitterStream.filter(new FilterQuery().track(opts.getOptStringRequired("track").split(","))); } finally { } return 0; }
From source file:public_streaming.GeoStream.java
License:Apache License
public static void main(String[] args) throws Exception { Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build(); TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance(); twStream.addListener(new MyStatusListener()); // /*from w ww. j a v a 2 s . c om*/ FilterQuery filter = new FilterQuery(); double[][] locations = { { -180.0d, -90.0d }, { 180.0d, 90.0d } };//??(???????) filter.locations(locations); twStream.filter(filter); }
From source file:public_streaming.HashtagStream.java
License:Apache License
public static void main(String[] args) throws Exception { Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build(); TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance(); twStream.addListener(new MyStatusListener()); //set filter//w ww.ja va 2 s .c om FilterQuery filter = new FilterQuery(); String[] track = { "#nhk" };//hashtag filter.track(track); twStream.filter(filter); }
From source file:public_streaming.KeywordStream.java
License:Apache License
public static void main(String[] args) throws Exception { Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build(); TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance(); twStream.addListener(new MyStatusListener()); // /*from ww w . j a v a 2s. c o m*/ FilterQuery filter = new FilterQuery(); String[] keywords = { "android", "iphone" };//?public_timeline? filter.track(keywords); twStream.filter(filter); }
From source file:SentimentAnalyses.PrintSampleStream.java
License:Apache License
public static void main(String[] args) throws TwitterException { final PrintSampleStream pr = new PrintSampleStream(); try {//from w w w .ja v a2s . c o m pr.LinkMongodb(); } catch (Exception e) { e.printStackTrace(); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("Em3WTI7jc90HcvKzPkTLQ") .setOAuthConsumerSecret("vg4p6rOF32bmffqRR8m0jAUClrxvtGiMB5PrSr3Zsw") .setOAuthAccessToken("1681973072-1q0zI0VPjHD3ttNuaBOL94frzCI9sXInxAcDK0w") .setOAuthAccessTokenSecret("ZRLkOyjmhHBkU1iNyEVNyIgIBsKrl0DUDKOcOMneYFYEM"); cb.setJSONStoreEnabled(true); TwitterStreamFactory tf = new TwitterStreamFactory(cb.build()); TwitterStream twitterStream = tf.getInstance(); StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); //System.out.println(status); String str = DataObjectFactory.getRawJSON(status); try { //JSONObject nnstr = new JSONObject(newstr); DBObject dbObject = (DBObject) JSON.parse(str); // System.out.println(dbObject); pr.collection.insert(dbObject); //System.out.println(dbObject); pr.count++; if (pr.count % 1000 == 0) System.out.println(pr.count); if (pr.count > 100000) { pr.mongo.close(); System.exit(0); } } catch (Exception e) { e.printStackTrace(); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); String[] trackArray; String[] Track = { "Malaysia Airlines", "Flight MH370", "Boeing-777", "Kuala Lumpur", "Bei jing" }; //trackArray[0] = "Obama"; //trackArray[1] = "Romney"; FilterQuery filter = new FilterQuery(); filter.track(Track); String[] lang = { "en" }; filter.language(lang); twitterStream.filter(filter); //pr.mongo.close(); }
From source file:soporte.GetTweets.java
public void filtrar() { FilterQuery fq = new FilterQuery(); String keywords[] = { "quito" }; fq.track(keywords);/*w w w . j a va2 s . c om*/ this.twitterStream.filter(fq); }
From source file:source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets.//from w w w . j av a 2 s .c o m */ @Override public void start() { // The channel is the piece of Flume that sits between the Source and // Sink, and is used to process events final ChannelProcessor channel = getChannelProcessor(); final Map<String, String> headers = new HashMap<String, String>(); // The StatusListener is a twitter4j API, which can be added to a // Twitter stream, and will execute methods every time a message comes // in through the stream StatusListener listener = new StatusListener() { // The onStatus method is executed every time a new tweet comes in public void onStatus(Status status) { // The EventBuilder is used to build an event using the headers // and the raw JSON of a tweet logger.debug(status.getUser().getScreenName() + ": " + status.getText()); headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime())); Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers); channel.processEvent(event); } // This listener will ignore everything except for new tweets public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onScrubGeo(long userId, long upToStatusId) { } public void onException(Exception ex) { } public void onStallWarning(StallWarning warning) { } }; logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}", new String[] { consumerKey, accessToken }); // Set up the stream's listener (defined above), and set any necessary // security information twitterStream.addListener(listener); // Set up a filter to pull out industry-relevant tweets logger.debug("Starting up Twitter filtering..."); FilterQuery query = new FilterQuery().count(0); if (locations == null) { logger.debug("No locations specified"); } else { String debugString = "Locations specified: "; debugString += "SW={" + locations[0][0] + ", " + locations[0][1] + "}, "; debugString += "NE={" + locations[1][0] + ", " + locations[1][1] + "}"; logger.debug(debugString); query.locations(locations); } if (keywords == null) { logger.debug("No keywords specified"); } else { String debugString = keywords.length + " keywords specified: "; for (int i = 0; i < keywords.length; i++) { debugString += keywords[i]; if (i != keywords.length - 1) { debugString += ", "; } } logger.debug(debugString); query.track(keywords); } twitterStream.filter(query); super.start(); }
From source file:storm.starter.spout.Q2SeqTwitterSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;/*from w w w .j av a2 s .co m*/ StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { queue.offer(status); } @Override public void onDeletionNotice(StatusDeletionNotice sdn) { } @Override public void onTrackLimitationNotice(int i) { } @Override public void onScrubGeo(long l, long l1) { } @Override public void onException(Exception ex) { } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); FilterQuery query = new FilterQuery(); query.track(keyWords); query.language(new String[] { "en" }); twitterStream.filter(query); }
From source file:storm.starter.spout.TwitterKeywordsSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;//from w w w . ja v a 2 s .c o m StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { queue.offer(status); } @Override public void onDeletionNotice(StatusDeletionNotice sdn) { } @Override public void onTrackLimitationNotice(int i) { } @Override public void onScrubGeo(long l, long l1) { } @Override public void onException(Exception ex) { } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); _twitterStream = twitterStream; if (keyWords.length == 0) { twitterStream.sample(); } else { // TODO: Adjust the query below to also track locations and languages. FilterQuery query = new FilterQuery(); query.track(keyWords); query.language(new String[] { "en" }); twitterStream.filter(query); } }
From source file:storm.starter.spout.TwitterNoKeywordSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;/*from w w w . j ava 2 s. c o m*/ StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { queue.offer(status); } @Override public void onDeletionNotice(StatusDeletionNotice sdn) { } @Override public void onTrackLimitationNotice(int i) { } @Override public void onScrubGeo(long l, long l1) { } @Override public void onException(Exception ex) { } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); twitterStream.addListener(listener); twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); twitterStream.setOAuthAccessToken(token); twitterStream.sample(); FilterQuery query = new FilterQuery(); //query.count(1000000); query.language(new String[] { "en" }); twitterStream.filter(query); }