List of usage examples for twitter4j TwitterStream addListener
TwitterStream addListener(StreamListener listener);
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 .j a va 2 s . co m 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()); // //w w w . j a v a2 s. c o m 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/*from w w w . j a v a 2s . co m*/ 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()); // /* ww w . j ava2s. co m*/ FilterQuery filter = new FilterQuery(); String[] keywords = { "android", "iphone" };//?public_timeline? filter.track(keywords); twStream.filter(filter); }
From source file:public_streaming.OAuthStream.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()); twStream.user();//OAuth????public_timeline?? }
From source file:public_streaming.PublicStream.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()); twStream.sample();//public_timeline }
From source file:public_streaming.SampleStream.java
License:Apache License
public static void main(String[] args) { // ?//w w w . java 2 s .co m Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY) .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN) .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build(); // TwitterStream?? TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance(); // Listener twitterStream.addListener(new Listener()); // ???????????? twitterStream.sample(); }
From source file:public_streaming.UserIDStream.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()); //// www . j av a 2 s.c o m long[] follow = { 1598997848 };//anondroid3?id:1598997848 FilterQuery filter = new FilterQuery(follow);//?userID????? 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.j a v a2 s . com 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:site_streaming.SiteStream.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) .setSiteStreamBaseURL("https://sitestream.twitter.com/1.1/site.json").build(); TwitterStream SiteStream = new TwitterStreamFactory(configuration).getInstance(); SiteStream.addListener(new MyStatusListener()); long[] follow = { 1598997848 };//anondroid3?id:1598997848 FilterQuery filter = new FilterQuery(follow);//?userID????? SiteStream.filter(filter);/*from w w w .jav a 2 s . co m*/ }