List of usage examples for twitter4j FilterQuery track
String[] track
To view the source code for twitter4j FilterQuery track.
Click Source Link
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// w ww . j a v a 2s. c o 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); }
From source file:edu.csupomona.nlp.tool.crawler.Twitter.java
/** * Query with given keywords. Crawling will start immediately. * @param filename File name for the query result * @param keywords Array of keywords *//* ww w . j av a 2s .c om*/ public void query(String filename, String[] keywords) { // prepare for the new query queryDone_ = false; // construct file name filename_ = filename; // init tweet list tweet_ = new ArrayList<>(); // init id set idSet_ = loadSet(); // calculate ETA time Calendar cal = Calendar.getInstance(); etaTime.setTimeInMillis(cal.getTimeInMillis() + hourLimit_ * 3600 * 1000); // debug info System.out.println("Querying for => " + filename_); // construct FilterQuery FilterQuery fQuery = new FilterQuery(); fQuery.track(keywords); // track specified keywords String[] languages = { lang_ }; fQuery.language(languages); // track specified language // start streaming with FilterQuery ts_.filter(fQuery); }
From source file:example.justids.java
License:Apache License
/** * Usage: java twitter4j.examples.search.SearchTweets [query] * * @param args//from www . j ava 2 s. c o m */ public static void main(String[] args) { StatusListener listener = new StatusListener() { public Double count = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onStatus(Status status) { try { File file = new File("Filtered_over1percent_lab_pc_obama_all.txt"); File file2 = new File("Filtered_over1percent_lab_pc_obama.txt"); // if file doesnt exists, then create it FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); FileWriter fw2 = new FileWriter(file2.getAbsoluteFile(), true); BufferedWriter bw2 = new BufferedWriter(fw2); if (this.count % 1000 == 0) { Date finished10k = new Date(); System.out.println("\n\n\n\n AVERAGE RATE OF TWEETS is " + (this.count * 1000 / (finished10k.getTime() - this.started.getTime()))); System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime())); System.out.println(this.count); System.out.println(finished10k.getTime() + " " + this.started.getTime() + " " + (finished10k.getTime() - this.started.getTime())); System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " " + (finished10k.getTime() - this.previous.getTime())); System.out.println(status.getSource()); System.out.println("\n\n\n\n"); this.previous = finished10k; } this.count++; // System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count); bw.write(status.getId() + "\n"); bw.close(); if (status.getText().contains("obama")) { bw2.write(status.getId() + "\n"); bw2.close(); } } catch (IOException e) { e.printStackTrace(); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet"); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println(numberOfLimitedStatuses + " are missing from here"); } @Override public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); RawStreamListener rawst = new RawStreamListener() { public Double count = 0d; public Double lengthsum = 0d; Date started = new Date(); Date previous = new Date(); @Override public void onMessage(String message) { if (!message.startsWith("{\"delete")) { count++; lengthsum += message.length(); if (count % 1000 == 0) { System.out.println(lengthsum / count); // lengthsum=0d; // count=0d; } } // TODO Auto-generated method stub } @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } }; // twitterStream.addListener(rawst); String[] searchfor = { "language", "people", "problem", "microsoft", "epidemic", "obama", "zoo" }; FilterQuery query = new FilterQuery(); query.track(searchfor); twitterStream.addListener(listener); twitterStream.filter(query); // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. // twitterStream.sample(); }
From source file:flight_ranker.Flight_colllector.java
public static void main(String[] args) throws FileNotFoundException, IOException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);/*from w w w . j a va2 s .c o m*/ cb.setOAuthConsumerKey("Oa6WAzH0j3sgVrP0CNGvxnWA2"); cb.setOAuthConsumerSecret("sLdoFybvJvVFz7Lxbbv9KWQDFeKcVeZAkWDC4QMHnx5lV2OmGE"); cb.setOAuthAccessToken("2691889945-5NOBWKUgT9FiAoyOQSCFg8CLlPRlDMbWcUrJBdK"); cb.setOAuthAccessTokenSecret("J6tA8Sxrtz2JNSFdQwAonbGxNfLNuD9I54Zfvomku3p5t"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener 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(); long followers = user.getFollowersCount(); long retweets = status.getRetweetCount(); long favs = status.getFavoriteCount(); System.out.println("USERNAME--> " + username); System.out.println("FOLLOWERS--> " + followers); String profileLocation = user.getLocation(); // System.out.println("RETWEETS--> "+retweets); // System.out.println("FAVOURITES--> "+favs); System.out.println("LOCATION--> " + profileLocation); long tweetId = status.getId(); System.out.println("TWEET ID--> " + tweetId); String content = status.getText(); System.out.println("TWEET--> " + content + "\n"); BufferedWriter b1 = null, b2, b3, b4, b5, b6, b7 = null; try { //output_file = new BufferedReader(new FileReader("G:\\Sentiwords.txt")); FileWriter f1 = new FileWriter("G:\\flights_data.txt", true); b1 = new BufferedWriter(f1); b1.write("#USERNAME- " + username); b1.newLine(); b1.write("#Followers- " + followers); b1.newLine(); b1.write("#Location- " + profileLocation); b1.newLine(); b1.write("#ID- " + tweetId); b1.newLine(); b1.write("#Tweet- " + content); b1.newLine(); b1.newLine(); tweet_editor modified_tweet = new tweet_editor(content); //tweet_tagger tagged_tweet = new tweet_tagger(modified_tweet.edited_tweet); //System.out.println(tagged_tweet.tagged); sentiment_calculator senti_value = new sentiment_calculator(modified_tweet.edited_tweet); if (content.contains("Indigo")) { System.out.println("indigo"); FileWriter f2 = new FileWriter("G:\\Indigo.txt", true); b2 = new BufferedWriter(f2); b2.write(Double.toString(senti_value.senti_rate)); b2.newLine(); } if (content.contains("Jet")) { System.out.println("jet"); FileWriter f3 = new FileWriter("G:\\jet.txt", true); b3 = new BufferedWriter(f3); b3.write(Double.toString(senti_value.senti_rate)); b3.newLine(); } if (content.contains("Indian")) { System.out.println("indian"); FileWriter f4 = new FileWriter("G:\\Indian.txt", true); b4 = new BufferedWriter(f4); b4.write(Double.toString(senti_value.senti_rate)); b4.newLine(); } if (content.contains("Spicejet")) { System.out.println("spicejet"); FileWriter f5 = new FileWriter("G:\\spicejet.txt", true); b5 = new BufferedWriter(f5); b5.write(Double.toString(senti_value.senti_rate)); b5.newLine(); } if (content.contains("AirAsia")) { System.out.println("airasia"); FileWriter f6 = new FileWriter("G:\\airasia.txt", true); b6 = new BufferedWriter(f6); b6.write(Double.toString(senti_value.senti_rate)); b6.newLine(); } try { //output_file = new BufferedReader(new FileReader("G:\\Sentiwords.txt")); FileWriter f7 = new FileWriter("G:\\flight_senti.txt", true); b7 = new BufferedWriter(f7); b7.write(String.valueOf(senti_value.senti_rate)); b7.newLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { b7.close(); } catch (IOException ex) { Logger.getLogger(Flight_colllector.class.getName()).log(Level.SEVERE, null, ex); } } } catch (IOException e) { e.printStackTrace(); } finally { try { b1.close(); } catch (IOException ex) { Logger.getLogger(Flight_colllector.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[] = { "Indian Airlines, Indigo Airlines, Indigo Airline , Indian Airline , Spicejet , jetAirways , Jet Airways, Jet Airlines , airasia" }; //we will pass stock related keyword here fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); }
From source file:it.unipr.aotlab.TwitterMiner.twitter.client.TwitterMain.java
License:Open Source License
/** * The args[] are used to filter twitter stream (example: * "#twitter #facebook #social #redis"). If no filter is specified, the * default "#twitter" filter is used/*from ww w .j a va 2 s .c o m*/ * * @param args * @throws FileNotFoundException * @throws TwitterException */ public static void main(String[] args) throws FileNotFoundException { redisDB = new RedisBackend(); listener = new TwitterStreamListener(redisDB); /** Number of old tweets to catch before starting to listen live stream */ int count = 0; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.count(count); if (args.length == 0) { args = new String[1]; args[0] = "#twitter"; } filterQuery.track(args); twitterStream.filter(filterQuery); }
From source file:Jimbo.Cheerlights.TweetListener.java
License:Open Source License
/** * @param args the command line arguments * @throws twitter4j.TwitterException/*from ww w.j a va 2 s. c o m*/ * @throws java.io.IOException * @throws org.apache.commons.cli.ParseException In case of command line error */ public static void main(String[] args) throws TwitterException, IOException, ParseException { // Set up simpler logging to stdout Jimbo.Logging.Logging.useStdout(); LOG.log(Level.INFO, "Starting twitter listener"); Options options = new Options(); options.addOption("b", Listener.MQTT_BROKER_KEY, true, "URL of the broker") .addOption("c", Listener.MQTT_CLIENT_KEY, true, "The MQTT client name to use") .addOption("t", Listener.MQTT_TOPIC_KEY, true, "The MQTT topic to use"); CommandLineParser parser = new DefaultParser(); CommandLine command = parser.parse(options, args); MQTTClient mqtt = null; String mqtt_topic = Listener.DEFAULT_MQTT_TOPIC; if (command.hasOption(Listener.MQTT_BROKER_KEY)) { if (!command.hasOption(Listener.MQTT_CLIENT_KEY)) throw new ParseException("MQTT without client name"); if (command.hasOption(Listener.MQTT_TOPIC_KEY)) mqtt_topic = command.getOptionValue(Listener.MQTT_TOPIC_KEY); try { mqtt = new MQTTClient(command.getOptionValue(Listener.MQTT_BROKER_KEY), command.getOptionValue(Listener.MQTT_CLIENT_KEY)); mqtt.run(); } catch (MqttException e) { LOG.log(Level.WARNING, "Failed to create MQTT client: {0}", e.toString()); } } else { if (command.hasOption(Listener.MQTT_TOPIC_KEY)) LOG.warning("MQTT topic supplied but no broker"); if (command.hasOption(Listener.MQTT_CLIENT_KEY)) LOG.warning("MQTT client name but no broker"); } Twitter twitter = new TwitterFactory().getInstance(); StatusListener listener = new listener("224.1.1.1", (short) 5123, mqtt, mqtt_topic); FilterQuery fq = new FilterQuery(); String keywords[] = { "#cheerlights" }; fq.track(keywords); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); twitterStream.filter(fq); LOG.log(Level.INFO, "Up and running...."); }
From source file:kafka.producer.PrintGeoStream.java
License:Apache License
/** * Main entry of this application.// w ww . ja va 2s .c om * * @param args <kafka host> <kafka topic> * @throws twitter4j.TwitterException */ public static void main(String[] args) throws TwitterException { // if (args.length != 2) { // System.out.println("Usage: java twitter4j.examples.PrintFilterStream <kafka host> <kafka topic>"); // System.exit(-1); // } // final String kafkaZKHost = args[0]; // final String kafkaTopic = args[1]; final String kafkaZKHost = "localhost"; final String kafkaTopic = "flightDelay"; BasicConfigurator.configure(); StatusListener listener = new StatusListener() { Gson gson = new Gson(); Properties kafkaProps = new Properties(); //kafkaProps.put("serializer.class", "kafka.serializer.StringEncoder"); //kafkaProps.put("zk.connect", "localhost:2181"); Producer<Integer, String> kafkaProducer = null; int tweetCount = 0; int interimCount = 0; @Override public void onStatus(Status status) { if (kafkaProducer == null) { kafkaProps.put("serializer.class", "kafka.serializer.StringEncoder"); //kafkaProps.put("zk.connect", "localhost:2181"); kafkaProps.put("zk.connect", kafkaZKHost.concat(":2181")); kafkaProducer = new Producer<Integer, String>(new ProducerConfig(kafkaProps)); } tweetCount++; interimCount++; if (interimCount == 100) { LOG.info(" total tweets received: " + tweetCount); interimCount = 0; } //System.out.println("@" + status.getUser().getScreenName() + " - "); // + status.getText()); //if ( status.getPlace() != null ) { // System.out.println("coordinates = " + status.getPlace().getGeometryCoordinates()); //} TweetInfo tweetInfo = new TweetInfo(); tweetInfo.populate(status); String message = gson.toJson(tweetInfo, TweetInfo.class); kafkaProducer.send(new ProducerData<Integer, String>(kafkaTopic, message)); //kafkaProducer.send(new ProducerData<Integer, String>("live_tweets", message)); } @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(); } }; /* * - use Redis to pass filter messages in - or ZK? * - Read in filter JSON with parms for: * - lat/lon boxj * - screen names to follow * - hashtags to track * - Convert screen names to userIds * - Twitter twitter = new TwitterFactory().getInstance(); * ResponseList<User> users = twitter.lookupUsers(args[0].split(",")); * - Kick off filter with new FilterQuery * - periodically check, and if filter updated, restart the TwitterStream: * .shutdown() * new * .addListener * * */ TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); // twitterStream.setOAuthAccessToken(new AccessToken("14098069-IgutahzmbaCubdoMU0MxsfsJT2iNVpRnoohCbV67k", "d1kFKqRSnrlA4bP1XWWLiHNsHfYDZQPWDHRVpGP4hM")); // twitterStream.setOAuthConsumer("G0SKTmMXjfMsunBh3Furg", "iVTYNLqVaVdUZz4xOn7H8ywylTaTuW3kH1dT21NUy4"); twitterStream.addListener(listener); /* * For CONUS only: * "lowerLat": "22.0", * "lowerLon": "-127.0", * "upperLat": "47.5", * "upperLon": "-67.0" */ FilterQuery fQuery = new FilterQuery(); String[] topics = new String[2]; topics[0] = "delayed flight"; topics[1] = "flight delay"; // double[][] us = {{-180, 24}, {-66, 50}}; double[][] lax = { { -118.5, 33.8 }, { -118.3, 34.01 } }; fQuery.locations(lax); fQuery.track(topics); twitterStream.filter(fQuery); }
From source file:monitoring.tools.TwitterAPI.java
License:Apache License
private void resetStream() { //logger.debug("Initialising kafka producer..."); //kafka.initProducer(confParams.getKafkaEndpoint()); logger.debug("Initialising proxy..."); kafka.initProxy(confParams.getKafkaEndpoint()); logger.debug("Initialising streaming..."); firstConnection = true;/* w ww.j a v a2 s . c om*/ tweetInfo = new ArrayList<>(); stream = new TwitterStreamFactory().getInstance(); timer = new Timer(); stream.onStatus(new Consumer<Status>() { @Override public void accept(Status arg0) { if (confParams.getAccounts() != null && !confParams.getAccounts().isEmpty()) { if (confParams.getAccounts().contains(arg0.getUser().getScreenName())) { tweetInfo.add(arg0); } } else tweetInfo.add(arg0); } }); stream.addConnectionLifeCycleListener(new ConnectionLifeCycleListener() { @Override public void onCleanUp() { } @Override public void onConnect() { logger.debug("Connection established successfully"); timer = new Timer(); timer.schedule(new TimerTask() { public void run() { if (firstConnection) { firstConnection = false; } else { generateData((new Timestamp((new Date()).getTime()).toString())); } } }, 0, Integer.parseInt(confParams.getTimeSlot()) * 1000); } @Override public void onDisconnect() { logger.debug("Connection closed"); } }); FilterQuery filterQuery = new FilterQuery(); if (this.confParams.getKeywordExpression() != null) filterQuery.track(Utils.generateKeywordExp(this.confParams.getKeywordExpression())); stream.filter(filterQuery); }
From source file:my.twittergui.TwitterUI.java
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed String text;//from w w w . ja v a2 s. co m text = jTextField1.getText(); //jTextArea1.append(text+"\n"); jTextArea1.append("Searching for: "); String[] strarray = text.split(" "); for (int i = 0; i < strarray.length; i++) { jTextArea1.append(strarray[i] + "\n"); } File filed = null; filed = new File("C:\\Results"); if (!filed.exists()) { filed.mkdir(); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret("").setOAuthAccessToken("") .setOAuthAccessTokenSecret(""); StatusListener listener = new StatusListener() { public void onStatus(Status status) { try { int fileno = 0; String strI = Integer.toString(fileno); String fname = "C:\\results\\DataCol" + strI + ".csv"; File file = new File(fname); FileWriter bw = new FileWriter(file, true); if (file.length() == 0) { bw.write("\"Screen Name\", text, \"created at\", geolocation, Retweet? \r\n"); bw.flush(); } while (file.length() > 10485760) { fileno += 1; strI = Integer.toString(fileno); fname = "DataCol" + strI + ".txt"; file = new File(fname); bw = new FileWriter(file, true); bw.write("\"Screen Name\", \"text\", \"created_at\", \"geolocation\" \r\n"); bw.flush(); } // if(!status.isRetweet()){ bw.write("\r\n"); bw.write("\"" + status.getUser().getScreenName() + "\","); String tweettxt = status.getText(); tweettxt = tweettxt.replace("\n", ""); tweettxt = tweettxt.replace(",", ""); tweettxt = tweettxt.replace("\"", ""); bw.write("\"" + tweettxt + "\","); bw.write("\"" + status.getCreatedAt() + "\","); if (status.getGeoLocation() != null) { bw.write("\"" + status.getGeoLocation() + "\""); } else bw.write("N/A,"); if (status.isRetweet()) bw.write("Yes"); else bw.write("No"); bw.flush(); bw.close(); //System.out.print("\n"); String str = "@" + status.getUser().getScreenName() + " " + status.getText() + " " + status.getCreatedAt() + " "; if (status.getGeoLocation() != null) str += status.getGeoLocation(); //System.out.print(str); jTextArea1.append(str + "\n"); // } bw.close(); } catch (IOException e) { //System.out.print("EXCEPTION"); e.printStackTrace(); } } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onStallWarning(StallWarning stall) { } public void onScrubGeo(long a, long b) { } public void onException(Exception ex) { ex.printStackTrace(); } }; TwitterStreamFactory tf = new TwitterStreamFactory(cb.build()); TwitterStream twitterStream = tf.getInstance(); twitterStream.addListener(listener); // Filter FilterQuery filtre = new FilterQuery(); Scanner in = new Scanner(System.in); filtre.track(strarray); twitterStream.filter(filtre); }
From source file:nlptexthatespeechdetection.dataCollection.GetTwitterDoc2VecTrainingData.java
public static void main(String[] args) throws FileNotFoundException, IOException { File dir = new File(folderName); if (!dir.exists()) dir.mkdir();//from ww w. ja va 2 s . c o m if (!dir.isDirectory()) { System.out.println(folderName + " is not a directory"); return; } System.out.println("number of tweets required: "); int numTweetsRequired = (new Scanner(System.in)).nextInt(); String path = folderName + "/" + fileName; File file = new File(path); if (!file.exists()) file.createNewFile(); FileWriter writer = new FileWriter(path, true); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); StatusListener listener = new StatusListener() { int numTweets = 0; @Override public void onStatus(Status status) { if (status.getLang().equals("in")) { try { String statusText = status.getText(); writer.write("\n"); writer.write(statusText); numTweets++; System.out.println("numTweets: " + numTweets); if (numTweets >= numTweetsRequired) { writer.close(); System.exit(0); } } catch (IOException ex) { Logger.getLogger(GetTwitterDoc2VecTrainingData.class.getName()).log(Level.SEVERE, null, ex); } } } @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); FilterQuery filterQuery = new FilterQuery(); filterQuery.track(new String[] { "a", "i", "u", "e", "o" }); filterQuery.language("in"); twitterStream.filter(filterQuery); }