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:CollectionOfMentions.ControllerCollectionOfMentions.java

License:Open Source License

public void run() {

    startDateTime = new DateTime();

    //checks on dates to make sure it's not abobe 7 days
    if (numberOfMinutes < 0) {
        numberOfMinutes = 0;//from   w  w  w . j av  a  2s  .com
    }
    if (numberOfMinutes > 59) {
        numberOfMinutes = 59;
    }
    if (numberOfHours > 24) {
        numberOfHours = 24;
    }
    if (numberOfHours < 0) {
        numberOfHours = 0;
    }
    if (numberOfDays > 7) {
        numberOfDays = 7;
    }
    if (numberOfDays < 0) {
        numberOfDays = 0;
    }

    stopTime = startDateTime.getMillis() + numberOfMinutes * 60000 + numberOfHours * 3600000
            + numberOfDays * 3600000 * 24;
    if (stopTime - startDateTime.getMillis() > 3600000 * 24 * 7) {
        stopTime = startDateTime.getMillis() + 3600000 * 24 * 7;
    }

    //registers actual start time in the status field
    opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("status",
            String.valueOf(startDateTime.getMillis()));
    dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED);

    //registers actual end time in the end field
    opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("end", String.valueOf(stopTime));
    dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED);

    final Object lock = new Object();

    StatusListener listener;
    listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            nbTweets++;

            if (System.currentTimeMillis() > stopTime) {

                //updating the job a last time;
                //**************************************
                //saving statuses to the db.
                //                    if (!twitterStatuses.isEmpty()) {
                //                        opsJob = dsJobs.createUpdateOperations(Job.class).addAll("statuses", statusesIds, true);
                //                        dsJobs.update(updateQueryJob, opsJob);
                //
                //                        dsTweets.save(twitterStatuses);
                //                    }
                // 91 is the code for twitter stream has stopped collecting.
                progress = 91;

                //recording the progress, nbTweets and end time of the job
                opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("progress", progress)
                        .set("nbTweets", nbTweets).set("end", System.currentTimeMillis());
                dsJobsInfo.update(updateQueryJobInfo, opsJobInfo);

                synchronized (lock) {
                    lock.notify();
                }

            } else {

                tweet = new Tweet();
                tweet.setStatus(TwitterObjectFactory.getRawJSON(status));
                tweet.setIdTweet(nbTweets);
                tweet.setJobId(jobUUID);
                ////                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
                //                    twitterStatus = convertStatus.convertOneToTwitterStatus(status);
                //                    twitterStatus.setJobId(jobUUID);
                //                    twitterStatuses.add(twitterStatus);
                //                    
                //                    
                //
                //                    statusesIds.add(status.getId());
                //                    timeSinceLastStatus = System.currentTimeMillis() - timeLastStatus;
                //
                //                    //**************************************
                //                    //adjusting the frequency of saves to DB, function of number of statuses received per second
                //                    if (timeSinceLastStatus < 200) {
                //                        sizeBatch = 100;
                //                    } else {
                //                        sizeBatch = 25;
                //                    }
                //                    timeLastStatus = System.currentTimeMillis();
                //                    progressLong = (Long) ((System.currentTimeMillis() - startDateTime.getMillis()) * 98 / (stopTime - startDateTime.getMillis()));

                //                    if (statusesIds.size() > sizeBatch || progressLong.intValue() > progress) {
                //**************************************
                //saving statuses to the db.
                try {
                    dsTweets.save(tweet, WriteConcern.UNACKNOWLEDGED);
                    opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("nbTweets", nbTweets);
                    dsJobsInfo.update(updateQueryJobInfo, opsJobInfo, false, WriteConcern.UNACKNOWLEDGED);
                } catch (MongoException m) {
                    System.out.println("saving of statuses to the db failed");
                }
                //                        twitterStatuses = new ArrayList();
                //
                //                        //**************************************
                //                        //updating list of status ids of the job.
                //                        opsJob = dsJobs.createUpdateOperations(Job.class).addAll("statuses", statusesIds, true);
                //                        dsJobs.update(updateQueryJob, opsJob);
                //                        statusesIds = new ArrayList();
                //
                //                        //updating progress.
                //                        System.out.println("progress: " + progressLong);
                //                        progress = progressLong.intValue();
                //                        opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("progress", progress).set("nbTweets", nbTweets);
                //                        dsJobsInfo.update(updateQueryJobInfo, opsJobInfo);

                //**************************************
                //                    }
            }
        }

        @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 onException(Exception ex) {
            System.out.println("Exception: " + ex);
        }

        @Override
        public void onStallWarning(StallWarning sw) {
            System.out.println("Got stall warning:" + sw.getMessage());

        }
    };
    twitterStream.addListener(listener);

    FilterQuery fq = new FilterQuery();
    String[] mentions = mention.split(",");
    fq.track(mentions);

    //        twitterStream.filter(new FilterQuery(0, users, keywords));
    twitterStream.filter(fq);

    try {
        synchronized (lock) {
            lock.wait();
        }
    } catch (InterruptedException e) {
    }
    try {
        twitterStream.shutdown();
    } catch (Exception e) {
        System.out.println("exception when shutdown of twitter stream");
        System.out.println("error: " + e.getMessage());
    }
    System.out.println("shutdown of twitter stream was successful");

}

From source file:com.aremaitch.codestock2010.library.TwitterLib.java

License:Apache License

public void startMonitoringStream(String[] hashTags, long[] userIds, StatusListener statusListener,
        ConnectionLifeCycleListener connectionLifeCycleListener) throws TwitterException {

    if (statusListener != null)
        tStream.addListener(statusListener);
    if (connectionLifeCycleListener != null)
        tStream.addConnectionLifeCycleListener(connectionLifeCycleListener);

    FilterQuery fq = new FilterQuery();
    fq.track(hashTags);
    if (userIds != null)
        fq.follow(userIds);//from   w  w w .j  a va  2  s .  com

    tStream.filter(fq);
}

From source file:com.esri.geoevent.transport.twitter.TwitterInboundTransport.java

License:Apache License

private void receiveData() {
    try {/*from   w w w .jav a 2  s. c om*/
        applyProperties();
        setRunningState(RunningState.STARTED);

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey(consumerKey);
        cb.setOAuthConsumerSecret(consumerSecret);
        cb.setOAuthAccessToken(accessToken);
        cb.setOAuthAccessTokenSecret(accessTokenSecret);
        twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

        RawStreamListener rl = new RawStreamListener() {

            @Override
            public void onException(Exception ex) {
                LOGGER.error("INBOUND_TRANSPORT_RAW_STREAM_LISTERNER_EXCEPTION", ex.getMessage());
            }

            @Override
            public void onMessage(String rawString) {
                receive(rawString);
            }
        };

        FilterQuery fq = new FilterQuery();

        String keywords[] = tracks;

        if (follows != null && follows.length > 0)
            fq.follow(follows);
        else if (keywords != null && keywords.length > 0)
            fq.track(keywords);
        else if (locations != null)
            fq.locations(locations);
        else
            throw new Exception("INBOUND_TRANSPORT_NOFILTER_ERROR");

        fq.count(count);

        LOGGER.info("INBOUND_TRANSPORT_FILTER", filterString);

        twitterStream.addListener(rl);
        twitterStream.filter(fq);

    } catch (Throwable ex) {
        LOGGER.error("UNEXPECTED_ERROR", ex);
        setRunningState(RunningState.ERROR);
    }
}

From source file:com.github.jcustenborder.kafka.connect.twitter.TwitterSourceTask.java

License:Apache License

@Override
public void start(Map<String, String> map) {
    this.config = new TwitterSourceConnectorConfig(map);

    TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(this.config.configuration());
    this.twitterStream = twitterStreamFactory.getInstance();

    String[] keywords = this.config.filterKeywords();

    if (log.isInfoEnabled()) {
        log.info("Setting up filters. Keywords = {}", Joiner.on(", ").join(keywords));
    }//  ww w . jav a 2  s .com

    FilterQuery filterQuery = new FilterQuery();
    filterQuery.track(keywords);

    if (log.isInfoEnabled()) {
        log.info("Starting the twitter stream.");
    }
    twitterStream.addListener(this);
    twitterStream.filter(filterQuery);
}

From source file:com.hortonworks.amuise.cdrstorm.kafka.producers.CDRTestDataProducer.java

private void start() {

    /**// w  ww .ja v  a2  s . com
     * Kafka Twitter Producer properties *
     */
    Properties twitterconprops = new Properties();
    twitterconprops.put("metadata.broker.list", globalconfigs.getProperty("twitter4j.brokerlist"));
    twitterconprops.put("serializer.class", globalconfigs.getProperty("twitter4j.serializer"));
    twitterconprops.put("request.required.acks", globalconfigs.getProperty("twitter4j.requiredacks"));
    ProducerConfig twitterproducerconfig = new ProducerConfig(twitterconprops);
    final Producer<String, String> twitterproducer = new Producer<String, String>(twitterproducerconfig);

    /**
     * Kafka CDR Producer properties *
     */
    Properties cdrconprops = new Properties();
    cdrconprops.put("metadata.broker.list", globalconfigs.getProperty("cdr.brokerlist"));
    cdrconprops.put("serializer.class", globalconfigs.getProperty("cdr.serializer"));
    cdrconprops.put("request.required.acks", globalconfigs.getProperty("cdr.requiredacks"));
    ProducerConfig cdrproducerconfig = new ProducerConfig(cdrconprops);
    final Producer<String, String> cdrproducer = new Producer<String, String>(cdrproducerconfig);

    /**
     * Twitter4j properties *
     */
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(globalconfigs.getProperty("twitter4j.consumerkey"));
    cb.setOAuthConsumerSecret(globalconfigs.getProperty("twitter4j.consumersecretkey"));
    cb.setOAuthAccessToken(globalconfigs.getProperty("twitter4j.accesstokenkey"));
    cb.setOAuthAccessTokenSecret(globalconfigs.getProperty("twitter4j.accesstokensecretkey"));
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    /**
     * Twitter listener *
     */
    StatusListener listener = new StatusListener() {
        // The onStatus method is executed every time a new tweet comes
        // in.
        public void onStatus(Status status) {

            StringBuilder sb = new StringBuilder();
            sb.append(status.getUser().getScreenName());
            sb.append("|");
            sb.append(status.getCreatedAt());
            sb.append("|");
            sb.append(status.getRetweetCount());
            sb.append("|");
            sb.append(status.getSource());
            sb.append("|");
            sb.append(status.getText());

            //call CDR create message
            String cdrmessage = createCDRMessage(status.getText());

            //Debug output
            System.out.println("_________________________________________________________");
            System.out.println(sb.toString());
            System.out.println("cdr message: " + cdrmessage);
            System.out.println("_________________________________________________________");

            //call producer for tweet
            KeyedMessage<String, String> twitterdata = new KeyedMessage<String, String>(
                    globalconfigs.getProperty("twitter4j.kafkatopic"), sb.toString());
            twitterproducer.send(twitterdata);

            //call producer for cdr
            KeyedMessage<String, String> cdrmessagedata = new KeyedMessage<String, String>(
                    globalconfigs.getProperty("cdr.kafkatopic"), cdrmessage);
            cdrproducer.send(cdrmessagedata);

        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

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

        public void onException(Exception ex) {
            System.out.println("General Exception: shutting down Twitter sample stream...");
            System.out.println(ex.getMessage());
            ex.printStackTrace();
            twitterStream.shutdown();
        }

        public void onStallWarning(StallWarning warning) {
        }
    };

    twitterStream.addListener(listener);

    // Filter stream with targeted words
    String filterstring = globalconfigs.getProperty("twitter4j.filterwords");
    FilterQuery filterq = new FilterQuery();
    filterq.track(filterstring.split(","));
    twitterStream.filter(filterq);

    //twitterStream.sample();
}

From source file:com.left8.evs.utilities.dsretriever.TweetsRetriever.java

License:Open Source License

/**
 * Method that handles the Twitter streaming API. <br>
 * <b>WARNING:</b> Method does not terminate by itself, due to the fact that
 * the streamer runs in a different thread.
 * @param keywords The keywords for which the streamer searches for tweets.
 * @param mongoDB A handler for the MongoDB database.
 * @param config A configuration object.
 *//*  w w  w  .  j a v a2  s.  c o  m*/
public final void retrieveTweetsWithStreamingAPI(String[] keywords, MongoHandler mongoDB, Config config) {

    ConfigurationBuilder cb = getAuthorization();
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    final StatusListener listener;
    listener = new StatusListener() {

        @Override
        public final void onStatus(Status status) {
            //Insert tweet to MongoDB
            mongoDB.insertSingleTweetIntoMongoDB(status, "NULL");
        }

        @Override
        public final void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            Utilities.printMessageln("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public final void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            Utilities.printMessageln("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public final void onScrubGeo(long userId, long upToStatusId) {
            Utilities.printMessageln("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

        @Override
        public final void onStallWarning(StallWarning warning) {
            Utilities.printMessageln("Got stall warning:" + warning);
        }

        @Override
        public final void onException(Exception ex) {
            ex.printStackTrace(System.out);
        }
    };

    FilterQuery fq = new FilterQuery();
    fq.language("en"); //Set language of tweets to "English"
    fq.track(keywords); //Load the search terms

    twitterStream.addListener(listener); //Start listening to the stream
    twitterStream.filter(fq); //Apply the search filters
}

From source file:com.mycompany.omnomtweets.Search.java

public static void main(String[] args) throws TwitterException {
    System.setProperty("twitter4j.loggerFactory", "twitter4j.internal.logging.NullLoggerFactory");
    if (args.length >= 1) {
        candidate = Candidate.valueOf(args[0]);
        System.out.println("Getting tweets on " + candidate.name);
    } else {/*from w w w.  ja va 2s. com*/
        //System.out.println("You didn't give me a candidate, so you get TRUMP");
    }
    //System.out.println("Starting at: " + sdf.format(new Date()));
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(candidate.consumerKey)
            .setOAuthConsumerSecret(candidate.consumerSecret).setOAuthAccessToken(candidate.accessToken)
            .setOAuthAccessTokenSecret(candidate.accessTokenSecret);

    final ArrayList<Status> statuses = new ArrayList<>();

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {
            statuses.add(status);
            writeToStdErr(status);
            //Removed because writing to STD OUT instead.
            //System.out.println("statuses: " + statuses.size());
            if (statuses.size() >= 100) {
                /*System.out.println("Writing " + statuses.size() + " to file " + 
                            "at: " + sdf.format(new Date()));*/
                writeTweetsToFile(statuses, candidate.name + "StreamTweets.txt");
                statuses.clear();
            }
        }

        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 sw) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    };

    FilterQuery fq = new FilterQuery();
    String keywords[] = candidate.aliases;

    fq.track(keywords);

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

From source file:com.project.shlok.TopicsAnalyzer.TwitterReader.java

/**
   * getTweets// ww  w .j av a2s  .  c  o  m
   * Reads tweets based on a specific query
   * @param String - query to be tracked
   * @return None
   * 
   */
public void getTweets(String query) {
    FilterQuery fq = new FilterQuery();
    fq.track(query);
    twitterStreamListener.setQuery(query);
    twitterStream.addListener(twitterStreamListener);
    twitterStream.filter(fq);
}

From source file:com.telefonica.iot.cygnus.sources.TwitterSource.java

License:Open Source License

@Override
public synchronized void start() {
    LOGGER.info("Starting twitter source {} ...", this);
    documentCount = 0;/*from w  ww .  jav a 2  s  .  c o  m*/
    startTime = System.currentTimeMillis();
    exceptionCount = 0;
    totalTextIndexed = 0;
    skippedDocs = 0;
    batchEndTime = System.currentTimeMillis() + maxBatchDurationMillis;

    final ChannelProcessor channel = getChannelProcessor();

    StatusListener listener = new StatusListener() {
        public void onStatus(Status status) {
            String jsonTweet = getStringJSONTweet(status);
            Event event = EventBuilder.withBody(jsonTweet, Charset.forName("UTF8"));

            eventBatch.add(event);

            if (eventBatch.size() >= maxBatchSize || System.currentTimeMillis() >= batchEndTime) {
                batchEndTime = System.currentTimeMillis() + maxBatchDurationMillis;

                channel.processEventBatch(eventBatch); // send batch of events (one per tweet) to the flume sink
                eventBatch.clear();
            }

            documentCount++;
            if ((documentCount % REPORT_INTERVAL) == 0) {
                LOGGER.info(String.format("Processed %s docs", numFormatter.format(documentCount)));
            }
            if ((documentCount % STATS_INTERVAL) == 0) {
                logStats();
            }
        }

        // 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 onStallWarning(StallWarning warning) {
        }

        public void onException(Exception e) {
            LOGGER.error("Exception while streaming tweets", e);
        }
    };

    twitterStream.addListener(listener);

    if (haveFilters) {
        FilterQuery filterQuery = new FilterQuery();

        if (haveCoordinateFilter) {
            filterQuery.locations(boundingBox);
            LOGGER.info("Coordinates added to filter query: {}", boundingBox[0][0] + " " + boundingBox[0][1]
                    + " " + boundingBox[1][0] + " " + boundingBox[1][1]);
        }
        if (haveKeywordFilter) {
            filterQuery.track(splitKeywords);
            LOGGER.info("Keywords added to filter query: {}", Arrays.toString(splitKeywords));
        }
        twitterStream.filter(filterQuery);
        LOGGER.info("Filter Query created: {}", filterQuery.toString());
    }

    LOGGER.info("Twitter source {} started.", getName());

    super.start();
}

From source file:com.tilab.ca.sda.ctw.connector.TwitterReceiver.java

@Override
public void onStart() {
    try {/*from w w w .  ja  v  a 2 s .  c o m*/
        log.info(String.format("[%s] CALLED ONSTART on Twitter Receiver...", Constants.TW_RECEIVER_LOG_TAG));
        log.debug(String.format("[%s] Init twitterStreamFactory...", Constants.TW_RECEIVER_LOG_TAG));

        TwitterStream twStream = new TwitterStreamFactory(twStreamConf).getInstance();

        log.debug(String.format("[%s] twitterStreamFactory initialized!", Constants.TW_RECEIVER_LOG_TAG));
        log.debug(String.format("[%s] adding status listener..", Constants.TW_RECEIVER_LOG_TAG));

        twStream.addListener(new StatusListener() {

            @Override
            public void onException(Exception e) {
                log.error(String.format("[%s] Exception on twitterStream", Constants.TW_RECEIVER_LOG_TAG), e);
                restart("Error on receiving tweets!", e);
            }

            @Override
            public void onTrackLimitationNotice(int notDelivered) {
                log.warn(String.format(
                        "[%s] WARNING:number of statuses matching keywords but not delivered => %d",
                        Constants.TW_RECEIVER_LOG_TAG, notDelivered));
            }

            @Override
            public void onStatus(Status status) {
                log.debug(String.format("[%s] new status received! storing on spark..",
                        Constants.TW_RECEIVER_LOG_TAG));
                store(status);
            }

            @Override
            public void onStallWarning(StallWarning stallW) {
                log.warn(String.format("[%s] WARNING: Received Stall Warning message: %s",
                        Constants.TW_RECEIVER_LOG_TAG, stallW.getMessage()));
            }

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

            @Override
            public void onDeletionNotice(StatusDeletionNotice arg0) {
            }
        });

        log.debug(String.format("[%s] Creating filterquery", Constants.TW_RECEIVER_LOG_TAG));
        FilterQuery fq = new FilterQuery();
        if (trackArray != null) {
            log.debug(String.format("[%s] added keys to track", Constants.TW_RECEIVER_LOG_TAG));
            fq.track(trackArray);
        }
        if (locationFilterArray != null) {
            log.info(String.format("[%s] added geolocations to track", Constants.TW_RECEIVER_LOG_TAG));
            fq.locations(locationFilterArray);
        }
        if (user2FollowArray != null) {
            log.debug(String.format("[%s] Added users to track", Constants.TW_RECEIVER_LOG_TAG));
            fq.follow(user2FollowArray);
        }
        twStream.filter(fq);
        log.debug(String.format("[%s] Setting twitterStream..", Constants.TW_RECEIVER_LOG_TAG));

        setTwitterStream(twStream);

        log.info(String.format("[%s] Twitter Stream started", Constants.TW_RECEIVER_LOG_TAG));

    } catch (Throwable t) {
        log.error(String.format("[%s] Error starting twitter stream", Constants.TW_RECEIVER_LOG_TAG), t);
        restart("Error starting twitter stream", t);
    }
}