List of usage examples for twitter4j FilterQuery FilterQuery
public FilterQuery()
From source file:Jimbo.Cheerlights.TweetListener.java
License:Open Source License
/** * @param args the command line arguments * @throws twitter4j.TwitterException//w w w. j a v a 2 s.co 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.//from w w w.j a va2 s.c o m * * @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:me.timothy.twittertoreddit.TwitterToRedditMain.java
License:Open Source License
private void begin() throws InterruptedException { loadConfiguration();/*from w w w .j a va 2 s . c o m*/ HttpRestClient rClient = new HttpRestClient(); rClient.setUserAgent("TwitterToReddit Bot by /u/Tjstretchalot"); User user = new User(rClient, username, password); try { user.connect(); } catch (Exception e) { System.err.println("Failed to connect"); return; } System.out.println("Succesfully Authenticated to Reddit"); Thread.sleep(1000); System.out.println(".."); Thread.sleep(1000); System.out.println("Authenticating to Twitter.."); TwitterStream stream = new TwitterStreamFactory().getInstance(); FilterQuery filter = new FilterQuery(); filter.follow(new long[] { twitterId }); TwitterToReddit mBot = new TwitterToReddit(rClient, user, twitterId, subreddit); mBot.beginStreaming(stream); stream.filter(filter); System.out.println("Success! The bot is now active and will post new tweets as well as print out here"); System.out.println(); }
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;//from www . j a v a2 s. c o m 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;/*w ww .jav a 2s.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 www .jav a 2 s. com 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); }
From source file:nlptexthatespeechdetection.dataCollection.TwitterStreamingAnnotator.java
public static void main(String[] args) throws NotDirectoryException { Scanner sc = new Scanner(System.in); System.out.println("Nama Anda (sebagai anotator): "); String namaAnotator = sc.nextLine(); AnnotatedDataFolder annotatedDataFolder = new AnnotatedDataFolder(dataFolderName); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); StatusListener listener = new StatusListener() { @Override/*from www .ja v a 2s. c o m*/ public void onStatus(Status status) { if (status.getLang().equals("in")) { System.out.println(); System.out.println(); System.out.println("=======ANOTASI======="); System.out.println("status: " + status.getText()); System.out.println(); System.out.println("is this a hate speech?(y/n. any other if you do not know)"); String annotatorResponse = sc.nextLine().trim().toLowerCase(); Date date = new Date(); String dateString = dateFormat.format(date); try { if (annotatorResponse.equals("y")) { String filePath = annotatedDataFolder.saveHateSpeechString(namaAnotator, dateString, status.getText()); System.out.println("Saved data to: " + filePath); } else if (annotatorResponse.equals("n")) { String filePath = annotatedDataFolder.saveNotHateSpeechString(namaAnotator, dateString, status.getText()); System.out.println("Saved data to: " + filePath); } System.out.println("thank you!"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { Logger.getLogger(TwitterStreamingAnnotator.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("ignoring non-indonesian tweet"); } // if (status.getGeoLocation() != null) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " $ " + status.getGeoLocation().toString()); // } // if (status.getLang().equals("id")) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString())); // } } @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); }
From source file:nlptexthatespeechdetection.NLPTextHateSpeechDetection.java
/** * @param args the command line arguments */// w ww . jav a 2 s . com public static void main(String[] args) throws TwitterException, NotDirectoryException, IOException { HateSpeechClassifier1 classifier = new HateSpeechClassifier1(); AnnotatedDataFolder data = new AnnotatedDataFolder("data"); boolean overSampling = false; classifier.train(data.getDateSortedLabeledData(overSampling)); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); StatusListener listener = new StatusListener() { int numHateSpeech = 0; int numTweets = 0; @Override public void onStatus(Status status) { if (status.getLang().equals("in")) { numTweets++; if (classifier.isHateSpeech(status.getText(), 0.5)) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString())); System.out.println(); System.out.println("lang: " + status.getLang()); System.out.println("number of detected hate speech: " + numHateSpeech); System.out.println("total number of streamed tweets: " + numTweets); System.out.println(); System.out.println(); numHateSpeech++; } } else { System.out.println("ignoring non-Indonesian tweet"); } // if (status.getGeoLocation() != null) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " $ " + status.getGeoLocation().toString()); // } // if (status.getLang().equals("id")) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString())); // } } @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); twitterStream.sample(); }
From source file:org.apache.asterix.external.util.TwitterUtil.java
License:Apache License
public static FilterQuery getFilterQuery(Map<String, String> configuration) throws AsterixException { String locationValue = null;/*from w ww . ja va 2 s .c o m*/ // For backward compatibility if (configuration.containsKey(ConfigurationConstants.KEY_LOCATIONS)) { locationValue = configuration.get(ConfigurationConstants.KEY_LOCATIONS); } else { locationValue = configuration.get(ConfigurationConstants.KEY_LOCATION); } String langValue = configuration.get(ConfigurationConstants.LANGUAGES); String trackValue = configuration.get(ConfigurationConstants.TRACK); FilterQuery filterQuery = null; // Terms to track if (trackValue != null) { String keywords[] = null; filterQuery = new FilterQuery(); if (trackValue.contains(",")) { keywords = trackValue.trim().split(",\\s*"); } else { keywords = new String[] { trackValue }; } filterQuery = filterQuery.track(keywords); } // Language filtering parameter if (langValue != null) { if (filterQuery == null) { filterQuery = new FilterQuery(); } String languages[]; if (langValue.contains(",")) { languages = langValue.trim().split(",\\s*"); } else { languages = new String[] { langValue }; } filterQuery = filterQuery.language(languages); } // Location filtering parameter if (locationValue != null) { double[][] locations = getBoundingBoxes(locationValue); if (locations != null) { if (filterQuery == null) { filterQuery = new FilterQuery(); } filterQuery = filterQuery.locations(locations); } } // Filtering level: none, low or medium (defaul=none) if (filterQuery != null) { String filterValue = configuration.get(ConfigurationConstants.FILTER_LEVEL); if (filterValue != null) { filterQuery = filterQuery.filterLevel(filterValue); } } return filterQuery; }
From source file:org.apache.camel.component.twitter.consumer.streaming.FilterConsumer.java
License:Apache License
private FilterQuery createFilter(TwitterEndpoint te) { FilterQuery filterQuery = new FilterQuery(); String allLocationsString = te.getProperties().getLocations(); if (allLocationsString != null) { String[] locationStrings = allLocationsString.split(";"); double[][] locations = new double[locationStrings.length][2]; for (int i = 0; i < locationStrings.length; i++) { String[] coords = locationStrings[i].split(","); locations[i][0] = Double.valueOf(coords[0]); locations[i][1] = Double.valueOf(coords[1]); }//from w w w. j av a 2 s .co m filterQuery.locations(locations); } String keywords = te.getProperties().getKeywords(); if (keywords != null && keywords.length() > 0) { filterQuery.track(keywords.split(",")); } String userIds = te.getProperties().getUserIds(); if (userIds != null) { String[] stringUserIds = userIds.split(","); long[] longUserIds = new long[stringUserIds.length]; for (int i = 0; i < stringUserIds.length; i++) { longUserIds[i] = Long.valueOf(stringUserIds[i]); } filterQuery.follow(longUserIds); } if (allLocationsString == null && keywords == null && userIds == null) { throw new IllegalArgumentException("At least one filter parameter is required"); } return filterQuery; }