List of usage examples for twitter4j TwitterStream filter
TwitterStream filter(final String... track);
From source file:twitter4j.examples.stream.PrintFilterStream.java
License:Apache License
/** * Main entry of this application.//from w w w . j a v a 2 s . c o m * * @param args follow(comma separated user ids) track(comma separated filter terms) * @throws TwitterException when Twitter service or network is unavailable */ public static void main(String[] args) throws TwitterException { if (args.length < 1) { System.out.println( "Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]"); System.exit(-1); } TwitterStream twitterStream = new TwitterStreamFactory().getInstance().addListener(new StatusListener() { @Override public void onStatus(Status status) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); } @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(); } }); ArrayList<Long> follow = new ArrayList<Long>(); ArrayList<String> track = new ArrayList<String>(); for (String arg : args) { if (isNumericalArgument(arg)) { for (String id : arg.split(",")) { follow.add(Long.parseLong(id)); } } else { track.addAll(Arrays.asList(arg.split(","))); } } long[] followArray = new long[follow.size()]; for (int i = 0; i < follow.size(); i++) { followArray[i] = follow.get(i); } String[] trackArray = track.toArray(new String[track.size()]); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. twitterStream.filter(new FilterQuery(0, followArray, trackArray)); }
From source file:twitterapp.TwitterApp.java
public static void streamTweets() throws TwitterException { /*getting the trends */ ConfigurationBuilder cb2 = new ConfigurationBuilder(); cb2.setDebugEnabled(true).setOAuthConsumerKey("S01GsVwuCAwZFp5BLg5C4k8PT") .setOAuthConsumerSecret("6jo0jo4b05Ec5ZJcf74v5yGUQu5v8DryUwypOBjPD6jaItRNzd") .setOAuthAccessToken("794259549297446912-Z3AWruBmLa7QmCO6BnybCSj1tZXNqbB") .setOAuthAccessTokenSecret("6ezMQPQVziW9yxyTITZA8Wc2RJWjcBKvbXZU4dOjo4bge"); TwitterFactory tf = new TwitterFactory(cb2.build()); Twitter twitter = tf.getInstance();//from w ww .j a v a2s .c o m Trends trends = twitter.getPlaceTrends(23424977); String top_trend = ""; int top = 0; for (Trend trend : trends.getTrends()) { if (top < 1) { top_trend = trend.getName(); top++; } } System.out.println("top trend : " + top_trend); //Using the Streaming API to get real time tweets based on the trending topics as keywords /* configurating twiter4j */ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("S01GsVwuCAwZFp5BLg5C4k8PT") .setOAuthConsumerSecret("6jo0jo4b05Ec5ZJcf74v5yGUQu5v8DryUwypOBjPD6jaItRNzd") .setOAuthAccessToken("794259549297446912-Z3AWruBmLa7QmCO6BnybCSj1tZXNqbB") .setOAuthAccessTokenSecret("6ezMQPQVziW9yxyTITZA8Wc2RJWjcBKvbXZU4dOjo4bge") .setJSONStoreEnabled(true); /* end of configuration */ MongoClient mongo = new MongoClient("localhost", 27017); MongoDatabase database = mongo.getDatabase("myTweetdb2"); MongoCollection<Document> collection = database.getCollection("myTweetCol5"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener; listener = new StatusListener() { @Override public void onStatus(Status status) { String rawJSON = TwitterObjectFactory.getRawJSON(status); Document doc = Document.parse(rawJSON); collection.insertOne(doc); } @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.sample(); twitterStream.addListener(listener); FilterQuery fq = new FilterQuery(); String keywords[] = { top_trend }; fq.track(keywords); twitterStream.filter(fq); }
From source file:TwitterCrawler.PrintSampleStream.java
License:Apache License
public static void main(String[] args) throws TwitterException { System.getProperties().put("http.proxyHost", "127.0.0.1"); System.getProperties().put("http.proxyPort", "8580"); //final PrintSampleStream pr = new PrintSampleStream(); try {/*from w ww . j av a 2 s.c o m*/ pr.LinkMongodb(); } catch (Exception e) { e.printStackTrace(); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("7ZVgfKiOvBDcDFpytRWSA") .setOAuthConsumerSecret("JmeJVeym78arzmGthrDUshQyhkq6nWA9tWLUKxc") .setOAuthAccessToken("321341780-Zy7LptVYBZBVvAeQ5GFJ4aKFw8sdqhWBnvA3pDuO") .setOAuthAccessTokenSecret("foi8FnQCeN0J5cdwad05Q6d7dbytFayQn1ZOvmhF6Qc"); cb.setJSONStoreEnabled(true); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onException(Exception ex) { // TODO Auto-generated method stub ex.printStackTrace(); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // TODO Auto-generated method stub System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onScrubGeo(long userId, long upToStatusId) { // TODO Auto-generated method stub System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @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); pr.collection.insert(dbObject); System.out.println(dbObject); pr.count++; /* if(pr.count>300000) { pr.mongo.close(); System.exit(0); } */ } catch (Exception e) { e.printStackTrace(); } } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { // TODO Auto-generated method stub System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; twitterStream.addListener(listener); //String[] Track = {"why Obama look like he about to drop the best album of 2013?"}; String[] trackArray = { "Mac", "App", "Store" }; //trackArray[1] = ; // Create a FilterQuery object and then set the items to track FilterQuery filter = new FilterQuery(); // Create an array of items to track //String[] itemsToTrack = {""}; // Set the items to track using FilterQuerys' track method. //filter.track(Track); filter.track(trackArray); // Assuming you have already created Twitter/TwitterStream object, // use the filter method to start streaming using the FilterQuery object just created. twitterStream.filter(filter); /* String[] username = {"katyperry"}; twitterStream.addListener(userlistener); twitterStream.user();*/ //pr.mongo.close(); }
From source file:twittermongodbapp.CollectTweets.java
static public void getTopTrends(twitter4j.Twitter twitter, TwitterStream twitterStream, StatusListener listener) {/*from w w w . j a v a 2s . c om*/ FilterQuery filterQuery = new FilterQuery(); Timer t = new Timer(); t.scheduleAtFixedRate(new TimerTask() { @Override public void run() { Trends trends; try { trends = twitter.getPlaceTrends(23424833);//trends = twitter.getPlaceTrends(1);(23424833) String[] keywords = new String[trends.getTrends().length]; System.out.println("Top Trends in Greece"); for (int i = 0; i < trends.getTrends().length; i++) { keywords[i] = trends.getTrends()[i].getName(); System.out.println(keywords[i]); } filterQuery.track(keywords); filterQuery.language(new String[] { "el" }); twitterStream.addListener(listener); twitterStream.filter(filterQuery); } catch (TwitterException ex) { Logger.getLogger(TwitterMongoDBApp.class.getName()).log(Level.SEVERE, null, ex); } } }, 1000, 360000); }
From source file:user_streaming.UserStream.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) .setUserStreamBaseURL("https://userstream.twitter.com/2/").build(); TwitterStream UserStream = new TwitterStreamFactory(configuration).getInstance(); UserStream.addListener(new MyStreamAdapter()); long[] follow = { 1038644269 };//ex)????????anondroid3?id:1598997848 FilterQuery filter = new FilterQuery(follow);//?userID????? UserStream.filter(filter); }
From source file:wordgame.WordGame.java
public static void initGame(String file) throws FileNotFoundException { Scanner s = new Scanner(new File(file)); //open the file ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) //populate the twitter details with the proper API informatin .setOAuthConsumerKey(s.nextLine()).setOAuthConsumerSecret(s.nextLine()) .setOAuthAccessToken(s.nextLine()).setOAuthAccessTokenSecret(s.nextLine()); TwitterFactory tf = new TwitterFactory(cb.build()); t = tf.getInstance();// ww w. j a v a 2s. c o m TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(t.getConfiguration()); TwitterStream twitterStream = twitterStreamFactory.getInstance(); FilterQuery filterQuery = new FilterQuery(); filterQuery.follow(new long[] { 731852008030916608L }); //Track our tweets twitterStream.addListener(new MentionListener()); //Set the listener to our MentionListener class twitterStream.filter(filterQuery); //begin listening }