Example usage for twitter4j FilterQuery track

List of usage examples for twitter4j FilterQuery track

Introduction

In this page you can find the example usage for twitter4j FilterQuery track.

Prototype

String[] track

To view the source code for twitter4j FilterQuery track.

Click Source Link

Usage

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//  w ww . j a  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
 *///from w w  w . j ava2s. co m
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;//w w  w.  j  a v  a 2s.c  om

    // 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  a v a  2 s. com*/
        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;
}

From source file:org.apache.storm.starter.spout.Q2TwitterSampleSpout.java

License:Apache License

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    queue = new LinkedBlockingQueue<Status>(1000);
    _collector = collector;//  w w w. j a  v a 2s  .c  om

    StatusListener listener = new StatusListener() {

        @Override
        public void onStatus(Status status) {

            queue.offer(status);
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice sdn) {
        }

        @Override
        public void onTrackLimitationNotice(int i) {
        }

        @Override
        public void onScrubGeo(long l, long l1) {
        }

        @Override
        public void onException(Exception ex) {
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub

        }

    };

    _twitterStream = new TwitterStreamFactory(new ConfigurationBuilder().setJSONStoreEnabled(true).build())
            .getInstance();

    _twitterStream.addListener(listener);
    _twitterStream.setOAuthConsumer(consumerKey, consumerSecret);
    AccessToken token = new AccessToken(accessToken, accessTokenSecret);
    _twitterStream.setOAuthAccessToken(token);

    if (keyWords.length == 0) {

        _twitterStream.sample();
    }

    else {

        FilterQuery query = new FilterQuery().track(keyWords);
        query.language(new String[] { "en" });
        query.track(new String[] { "job", "vote", "local", "Clinton", "Trump", "debate", "tech",
                "TheWalkingDead", "android", "Election", "USA", "GOT7", "Empire", "Apple", "ISIS", "election",
                "Football", "iPhone", "Amazon", "uber" });
        _twitterStream.filter(query);
    }

}

From source file:org.graylog2.inputs.twitter.TwitterTransport.java

License:Open Source License

@Override
public void launch(final MessageInput input) throws MisfireException {
    final ConfigurationBuilder cb = new ConfigurationBuilder()
            .setOAuthConsumerKey(configuration.getString(CK_OAUTH_CONSUMER_KEY))
            .setOAuthConsumerSecret(configuration.getString(CK_OAUTH_CONSUMER_SECRET))
            .setOAuthAccessToken(configuration.getString(CK_OAUTH_ACCESS_TOKEN))
            .setOAuthAccessTokenSecret(configuration.getString(CK_OAUTH_ACCESS_TOKEN_SECRET))
            .setJSONStoreEnabled(true);//from w  w w  .j av a2s . c  om

    final StatusListener listener = new StatusListener() {
        public void onStatus(final Status status) {
            try {
                input.processRawMessage(createMessageFromStatus(status));
            } catch (IOException e) {
                LOG.debug("Error while processing tweet status", e);
            }
        }

        private RawMessage createMessageFromStatus(final Status status) throws IOException {
            return new RawMessage(TwitterObjectFactory.getRawJSON(status).getBytes(StandardCharsets.UTF_8));
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onException(final Exception ex) {
            LOG.error("Error while reading Twitter stream", ex);
        }

        @Override
        public void onScrubGeo(long lon, long lat) {
        }

        @Override
        public void onStallWarning(StallWarning stallWarning) {
            LOG.info("Stall warning: {} ({}% full)", stallWarning.getMessage(), stallWarning.getPercentFull());
        }
    };

    final String[] track = Iterables.toArray(
            Splitter.on(',').omitEmptyStrings().trimResults().split(configuration.getString(CK_KEYWORDS)),
            String.class);
    final FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(track);

    if (twitterStream == null) {
        twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    }

    twitterStream.addListener(listener);
    twitterStream.filter(filterQuery);
}

From source file:org.hubiquitus.hubotsdk.adapters.HTwitterAdapterInbox.java

License:Open Source License

/**
 * Function for tweet Streaming/*from   w w  w .  ja v a  2s. c om*/
 */
private void stream() {
    /**
     * Configuration for access to twitter account
     */
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setUseSSL(true).setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(twitterAccessToken)
            .setOAuthAccessTokenSecret(twitterAccessTokenSecret);

    //Instantiation of tweet stream
    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener = new StatusListener() {
        public void onStatus(Status tweet) {
            String lang = tweet.getUser().getLang();
            //Set the filter for the language
            if ((langFilter == null) || (lang != null && lang.equalsIgnoreCase(langFilter))) {
                HMessage message = transformtweet(tweet);
                put(message);
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
            log.info("message: ", ex);
        }
    };

    FilterQuery fq = new FilterQuery();
    fq.track(tags.split(","));
    twitterStream.addListener(listener);
    twitterStream.filter(fq);
}

From source file:org.onepercent.utils.twitterstream.agent.src.main.java.com.cloudera.flume.source.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets./*  w ww .  j  av a 2  s .c  o  m*/
 */
@Override
public void start() {
    // The channel is the piece of Flume that sits between the Source and Sink,
    // and is used to process events.
    final ChannelProcessor channel = getChannelProcessor();

    final Map<String, String> headers = new HashMap<String, String>();

    // The StatusListener is a twitter4j API, which can be added to a Twitter
    // stream, and will execute methods every time a message comes in through
    // the stream.
    StatusListener listener = new StatusListener() {
        // The onStatus method is executed every time a new tweet comes in.
        public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the headers and
            // the raw JSON of a tweet
            logger.debug(status.getUser().getScreenName() + ": " + status.getText());

            headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime()));
            Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers);

            channel.processEvent(event);
        }

        // This listener will ignore everything except for new tweets
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
        }

        public void onStallWarning(StallWarning warning) {
        }
    };

    logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}",
            new String[] { consumerKey, accessToken });
    // Set up the stream's listener (defined above),
    twitterStream.addListener(listener);

    // Set up a filter to pull out industry-relevant tweets
    if (keywords.length == 0) {
        logger.debug("Starting up Twitter sampling...");
        twitterStream.sample();
    } else if (keywords.length > 0) {
        if (languages.length == 0) {
            logger.debug("Starting up Twitter Keyword filtering...");

            FilterQuery query = new FilterQuery().track(keywords);
            twitterStream.filter(query);
        } else {
            logger.debug("Starting up Twitter Keyword and Language filtering...");

            FilterQuery query = new FilterQuery();
            query.track(keywords);
            query.language(languages);
            twitterStream.filter(query);
        }
    }

    super.start();
}

From source file:org.richfaces.examples.tweetstream.dataserver.listeners.TweetStreamListener.java

License:Open Source License

public void startTwitterStream() {
    FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(tracks);

    twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(this);
    twitterStream.filter(filterQuery);/* w w w .  j a v  a2 s  .c  om*/
}

From source file:org.selman.tweetamo.TweetamoClient.java

License:Apache License

public static void main(String[] args) throws Exception {

    if (args.length != 2) {
        System.out.println("Usage: [language] [search topic]");
    }/*from  w ww.jav  a  2 s .c om*/

    kinesisClient = new AmazonKinesisClient(new ClasspathPropertiesFileCredentialsProvider());
    waitForStreamToBecomeAvailable(STREAM_NAME);

    LOG.info("Publishing tweets to stream : " + STREAM_NAME);
    StatusListener listener = new StatusListener() {
        public void onStatus(Status status) {
            try {
                PutRecordRequest putRecordRequest = new PutRecordRequest();
                putRecordRequest.setStreamName(STREAM_NAME);
                putRecordRequest.setData(TweetSerializer.toBytes(status));
                putRecordRequest.setPartitionKey(status.getUser().getScreenName());
                PutRecordResult putRecordResult = kinesisClient.putRecord(putRecordRequest);
                LOG.info("Successfully putrecord, partition key : " + putRecordRequest.getPartitionKey()
                        + ", ShardID : " + putRecordResult.getShardId());

            } catch (Exception e) {
                LOG.error("Failed to putrecord", e);
            }
        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onException(Exception ex) {
            ex.printStackTrace();
        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }
    };

    ClasspathTwitterCredentialsProvider provider = new ClasspathTwitterCredentialsProvider();
    TwitterCredentials credentials = provider.getTwitterCredentials();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(credentials.getConsumerKey())
            .setOAuthConsumerSecret(credentials.getConsumerSecret())
            .setOAuthAccessToken(credentials.getAccessToken())
            .setOAuthAccessTokenSecret(credentials.getAccessTokenSecret());
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    twitterStream.addListener(listener);
    FilterQuery filterQuery = new FilterQuery();
    filterQuery.language(new String[] { args[0] });
    filterQuery.track(new String[] { args[1] });
    twitterStream.filter(filterQuery);
}