List of usage examples for twitter4j TwitterStream filter
TwitterStream filter(final String... track);
From source file:com.wso2.stream.connector.protocol.TwitterPollingConsumer.java
License:Open Source License
/** * Setting up a connection with Twitter Stream API with the given credentials *///w ww . j a v a 2 s . c o m private void setupConnection() { StatusListener listener = new StatusListenerImpl(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey) .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken) .setOAuthAccessTokenSecret(accessSecret); TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(); twitterStream.addListener(listener); FilterQuery query = new FilterQuery(); query.language(new String[] { "en" }); query.track(filterTags); twitterStream.filter(query); }
From source file:crawling.FoundUsersByStreamHashtag.java
License:Apache License
public static void main(String[] args) throws TwitterException { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { // my shutdown code here // Record the user status idOut.println("Final status -------------------:"); for (Long id : discoveredUsers.keySet()) { idOut.println(id + "," + discoveredUsers.get(id)); }//ww w .j ava 2 s . co m idOut.close(); } }); if (args.length != 1) { System.out.println("Usage: java twitter4j.examples.PrintFilterStreamHashtag hashtag"); System.exit(-1); } /*if(args.length == 2){ // Preload the collected user IDs preloadID(args[1]); }*/ //buildStartTime(); File file = new File(fileName); InputStream is = null; try { if (file.exists()) { is = new FileInputStream(file); prop.load(is); } else { System.out.println(fileName + " doesn't exist!"); System.exit(-1); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } StatusListener listener = new StatusListener() { public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); CheckUser(status); /*storeATweet(status); tweets += 1; if (tweets % 1000 == 0){ System.out.println("We now have tweets: " + tweets); }*/ } private void CheckUser(Status status) { // TODO Auto-generated method stub Long id = status.getUser().getId(); String username = status.getUser().getScreenName(); String realname = status.getUser().getName(); String text = status.getText(); Date date = status.getCreatedAt(); if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(status); } } 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 { FileWriter outFile = new FileWriter("discoveredUser" + args[0] + ".txt", true); idOut = new PrintWriter(outFile); //out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); TwitterStream twitterStream = getOAuthTwitterStream(); twitterStream.addListener(listener); FilterQuery query = new FilterQuery(); String[] track = { args[0] }; //String[] track = {"#BaltimoreRiots"}; query.track(track); twitterStream.filter(query); }
From source file:crawling.PrintFilterStreamByFile.java
License:Apache License
public static void main(String[] args) throws TwitterException { if (args.length < 1) { System.out.println("Usage: java twitter4j.examples.PrintFilterStreamByFile ID_File "); System.exit(-1);/*ww w. j a va2 s . co m*/ } idFile = args[0]; tweetFile = idFile.split("\\.")[0] + "-tweets.txt"; try { FileWriter outFile = new FileWriter(tweetFile, true); out = new PrintWriter(outFile); //out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } StatusListener listener = new StatusListener() { public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); storeTweet(status.getUser().getId() + "::" + 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(); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); // String users = "700896487,701053075,636744543"; // String[] split = users.split(","); buildFollowList(); 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:crawling.PrintFilterStreamGeo.java
License:Apache License
public static void main(String[] args) throws TwitterException { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ // Record the user status idOut.println("Final status -------------------:"); for (Long id : discoveredUsers.keySet()) { idOut.println(id + "," + discoveredUsers.get(id)); }//from ww w. j a va2s .c o m idOut.close(); } }); if (args.length < 5) { System.out.println("Usage: java twitter4j.examples.PrintFilterStreamGeo long1 lati1 long2 lati2 CITY"); System.exit(-1); } if (args.length == 6) { // Preload the collected user IDs preloadID(args[5]); } File file = new File(fileName); InputStream is = null; try { if (file.exists()) { is = new FileInputStream(file); prop.load(is); } else { System.out.println(fileName + " doesn't exist!"); System.exit(-1); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } StatusListener listener = new StatusListener() { public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); CheckUser(status); } private void CheckUser(Status status) { // TODO Auto-generated method stub Long id = status.getUser().getId(); /*String username = status.getUser().getScreenName(); String realname = status.getUser().getName(); String text = status.getText(); Date date = status.getCreatedAt();*/ if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(status); } } 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 { FileWriter outFile = new FileWriter("discoveredUser" + args[4] + ".txt", true); idOut = new PrintWriter(outFile); //out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); TwitterStream twitterStream = getOAuthTwitterStream(); twitterStream.addListener(listener); /*ArrayList<Long> follow = new ArrayList<Long>(); ArrayList<String> track = new ArrayList<String>(); 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()]);*/ // Geographic location double[][] location = new double[2][2]; //location[0] = new double[4]; //location[1] = new double[4]; location[0][0] = Double.parseDouble(args[0]); location[0][1] = Double.parseDouble(args[1]); location[1][0] = Double.parseDouble(args[2]); location[1][1] = Double.parseDouble(args[3]); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. //twitterStream.filter(new FilterQuery(0, followArray, trackArray, location)); FilterQuery query = new FilterQuery(); query.locations(location); twitterStream.filter(query); }
From source file:datasite.DataSite.java
public static void main(String[] args) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);//from w ww . j ava 2 s.c om cb.setOAuthConsumerKey("ConsumerKey"); cb.setOAuthConsumerSecret("ConsumerSecret"); cb.setOAuthAccessToken("AccessToken"); cb.setOAuthAccessTokenSecret("AccessTokenSecret"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener; listener = new StatusListener() { @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { // TODO Auto-generated method stub } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStatus(Status status) { User user = status.getUser(); // gets Username String username = status.getUser().getScreenName(); try (PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter("tweet_input\\tweets.txt", true)))) { //out.println(username); String content = status.getText(); out.println(content + "\n"); //System.out.append( content +"\n"); } catch (IOException ex) { Logger.getLogger(DataSite.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void onTrackLimitationNotice(int arg0) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; FilterQuery fq = new FilterQuery(); String keywords[] = { "africa" }; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); //twitterStream.(); }
From source file:datasite.SortedUnique.java
public static void main(String[] args) { ConfigurationBuilder confb = new ConfigurationBuilder(); confb.setDebugEnabled(true);/*from w w w.j ava 2s.co m*/ confb.setOAuthConsumerKey("Consumer Key"); confb.setOAuthConsumerSecret("Consumer Secret"); confb.setOAuthAccessToken("Access Token"); confb.setOAuthAccessTokenSecret("Access Token Secret"); TwitterStream twitterStream = new TwitterStreamFactory(confb.build()).getInstance(); StatusListener listener; listener = new StatusListener() { @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { // TODO Auto-generated method stub } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStatus(Status status) { PrintStream out = null; try { User user = status.getUser(); // gets Username String username = status.getUser().getScreenName(); String content = status.getText(); Set<String> userWrds = new HashSet<String>(); // HashSet implements Set Scanner sc = new Scanner(content + user); while (sc.hasNext()) { String word = sc.next(); userWrds.add(word); } out = new PrintStream(new FileOutputStream("tweet_output\\unique.txt", true)); Iterator<String> iter = userWrds.iterator(); for (int i = 1; i <= 1 && iter.hasNext(); i++) //System.out.println(iter.next()); out.println(userWrds.size()); } catch (FileNotFoundException ex) { Logger.getLogger(SortedUnique.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } } @Override public void onTrackLimitationNotice(int arg0) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; FilterQuery fq = new FilterQuery(); String keywords[] = { "africa" }; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); //twitterStream.(); }
From source file:de.botshield.CaptureFilterStream.java
License:Apache License
public void execute(long[] followArray, String[] trackArray) { // try to set up new stream, when the listener fails due to HTTP timeout // for example this.initializeStreamListener(); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); try {//w w w . jav a 2s . c o m // filter() method internally creates a thread which manipulates // TwitterStream and calls these adequate listener methods // continuously. twitterStream.filter(new FilterQuery(0, followArray, trackArray)); } catch (Exception ex) { twitterStream.removeListener(listener); twitterStream.cleanUp(); } }
From source file:de.jetwick.tw.NewClass.java
License:Apache License
/** * A thread using the streaming API./* w w w.j a v a 2 s . co m*/ * Maximum tweets/sec == 50 !! we'll lose even infrequent tweets for a lot keywords! */ public Thread streaming(final String token, final String tokenSecret) { return new Thread() { StatusListener listener = new StatusListener() { int counter = 0; public void onStatus(Status status) { if (++counter % 20 == 0) log("async counter=" + counter); asyncMap.put(status.getId(), status.getText()); } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { log("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { log("Got track limitation notice:" + numberOfLimitedStatuses); } public void onScrubGeo(int userId, long upToStatusId) { log("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } public void onException(Exception ex) { ex.printStackTrace(); } }; public void run() { TwitterStream twitterStream = new TwitterStreamFactory() .getInstance(new AccessToken(token, tokenSecret)); twitterStream.addListener(listener); twitterStream.filter(new FilterQuery(0, null, new String[] { queryTerms }, null)); } }; }
From source file:de.jetwick.tw.TwitterSearch.java
License:Apache License
public TwitterStream streamingTwitter(Collection<String> track, final Queue<JTweet> queue) throws TwitterException { String[] trackArray = track.toArray(new String[track.size()]); TwitterStream stream = new TwitterStreamFactory().getInstance(twitter.getAuthorization()); stream.addListener(new StatusListener() { @Override// w w w. ja v a 2s .c o m public void onStatus(Status status) { // ugly twitter ... if (Helper.isEmpty(status.getUser().getScreenName())) return; if (!queue.offer(new JTweet(toTweet(status), new JUser(status.getUser())))) logger.error("Cannot add tweet as input queue for streaming is full:" + queue.size()); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { logger.error("We do not support onDeletionNotice at the moment! Tweet id: " + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { logger.warn("onTrackLimitationNotice:" + numberOfLimitedStatuses); } @Override public void onException(Exception ex) { logger.error("onException", ex); } @Override public void onScrubGeo(long userId, long upToStatusId) { } }); stream.filter(new FilterQuery(0, new long[0], trackArray)); return stream; }
From source file:demo.Investigation.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()); //String[] track = {"#daihyo"}; //#JPNvs#COL/*from w w w . j a va 2 s.co m*/ //String[] track = {"#tvasahi,#daihyo,#jfa,#tvasahi_soccer,#JPN,#COL"};//soccer //String[] track = {"#nhk,#tvtokyo,#etv,#ntv,#tvasahi,#tbs,#fujitv,#tokyomx"};//TV String[] track = { "#nhk" }; FilterQuery query = new FilterQuery(); query.track(track); twStream.filter(query); }