Example usage for twitter4j FilterQuery FilterQuery

List of usage examples for twitter4j FilterQuery FilterQuery

Introduction

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

Prototype

public FilterQuery() 

Source Link

Document

Creates a new FilterQuery

Usage

From source file:at.storm.spout.TwitterStreamSpout.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*www  .jav a2  s . com*/
public void open(Map config, TopologyContext context, SpoutOutputCollector collector) {
    m_collector = collector;
    m_tweetsQueue = new LinkedBlockingQueue<Status>(1000);

    // Optional startup sleep to finish bolt preparation
    // before spout starts emitting
    if (config.get(CONF_STARTUP_SLEEP_MS) != null) {
        long startupSleepMillis = (Long) config.get(CONF_STARTUP_SLEEP_MS);
        TimeUtils.sleepMillis(startupSleepMillis);
    }

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

    // Set Listener
    twitterStream.addListener(new StatusListener() {
        @Override
        public void onStatus(Status status) {
            m_tweetsQueue.offer(status); // add tweet into queue
        }

        @Override
        public void onException(Exception arg0) {
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }

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

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
        }
    });

    // Set credentials
    twitterStream.setOAuthConsumer(m_consumerKey, m_consumerSecret);
    AccessToken token = new AccessToken(m_accessToken, m_accessTokenSecret);
    twitterStream.setOAuthAccessToken(token);

    // Filter twitter stream
    FilterQuery tweetFilterQuery = new FilterQuery();
    if (m_keyWords != null) {
        tweetFilterQuery.track(m_keyWords);
    }

    // Filter location
    // https://dev.twitter.com/docs/streaming-apis/parameters#locations
    tweetFilterQuery.locations(new double[][] { new double[] { -180, -90, }, new double[] { 180, 90 } }); // any
    // geotagged
    // tweet

    // Filter language
    tweetFilterQuery.language(new String[] { m_filterLanguage });

    twitterStream.filter(tweetFilterQuery);
}

From source file:au.net.moon.tStreamingArchiver.Archiver.java

License:Open Source License

/**
 * Setup log files and then open a Twitter streaming api connection to
 * collect data.//from  w ww  .j  a v  a 2 s  . c om
 * 
 * @param args
 *            none
 * @throws TwitterException
 */
public static void main(String[] args) throws TwitterException {
    debug = false;
    if (!debug) {
        new RedirectSystemLogs("TwitterArchiverLog.%g.log");
    } else {
        System.out.println("TwitterStreamingArchiver: Logging to system log files");
    }

    System.out.println("TwitterStreamingArchiver: Program Starting... (v1.02)");

    SearchFilter searchFilter = new SearchFilter();
    Archiver archiver = new Archiver();
    twitterStream.addListener(archiver);
    final int historyCount = 0;
    FilterQuery query = new FilterQuery();
    query.follow(searchFilter.followArray());
    query.track(searchFilter.trackArray());
    query.count(historyCount);
    twitterStream.filter(query);
    // TODO: work out how to update the filter when it has changed
    // searchFilter.update() checks and returns true if searches have
    // changed. See my notes about how to do it manually.
}

From source file:bditac.TwitterCaptura.java

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

    int criid, apiid, capid;

    if (args.length == 3) {
        criid = Integer.parseInt(args[0]);
        apiid = Integer.parseInt(args[1]);
        capid = Integer.parseInt(args[2]);
    } else {//from   www  .  j  av  a 2  s .  c  o m
        criid = 1;
        apiid = 1;
        capid = 1;
    }

    CriseJpaController crijpa = new CriseJpaController(factory);

    Crise crise = crijpa.findCrise(criid);

    CriseApiJpaController criapijpa = new CriseApiJpaController(factory);

    CriseApi criapi = criapijpa.findCriseApi(capid);

    ApiJpaController apijpa = new ApiJpaController(factory);

    Api api = apijpa.findApi(apiid);

    CidadeJpaController cidjpa = new CidadeJpaController(factory);

    Cidade cidade = cidjpa.findCidade(crise.getCidId());

    String coords[] = crise.getCriRegiao().split(",");

    TwitterGeo geo = new TwitterGeo(coords, cidade.getCidNome(), crise.getCriGeotipo());

    Thread threadGeo = new Thread(geo);

    TwitterMens mens = new TwitterMens(crise.getCrtId());

    Thread threadMens = new Thread(mens);

    TwitterGravar gravar = new TwitterGravar();

    Thread threadGravar = new Thread(gravar);

    threadGeo.setName("ThreadGeo");
    threadGeo.start();

    threadMens.setName("ThreadMens");
    threadMens.start();

    threadGravar.setName("ThreadGravar");
    threadGravar.start();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(criapi.getCapKey())
            .setOAuthConsumerSecret(criapi.getCapSecret()).setOAuthAccessToken(criapi.getCapToken())
            .setOAuthAccessTokenSecret(criapi.getCapTokenSecret());

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

    String texto = "[\\xF0\\x9F]";

    StatusListener listener;
    listener = new StatusListener() {
        int cont = 0;

        @Override
        public void onStatus(Status status) {
            if (!(status.getText().contains(texto) || status.getText().isEmpty())) {
                Ocorrencia ocor = new Ocorrencia();
                ocor.setApiId(apiid);
                ocor.setCriId(criid);
                ocor.setCapId(capid);

                ocor.setOcrIdApi(status.getId());
                ocor.setOcrCriacao(status.getCreatedAt());
                ocor.setOcrTexto(status.getText());
                //                    System.out.println(ocor.getOcrTexto());
                ocor.setOcrUsuId(status.getUser().getId());
                ocor.setOcrUsuNome(status.getUser().getName());
                ocor.setOcrUsuScreenNome(status.getUser().getScreenName());
                ocor.setOcrFonte(status.getSource());
                ocor.setOcrLingua(status.getLang());
                ocor.setOcrFavorite(status.getFavoriteCount());
                ocor.setOcrRetweet(status.getRetweetCount());
                String coords = "";
                if (status.getPlace() == null) {
                    ocor.setOcrPaisCodigo("");
                    ocor.setOcrPais("");
                    ocor.setOcrLocal("");
                } else {
                    ocor.setOcrPaisCodigo(status.getPlace().getCountryCode());
                    ocor.setOcrPais(status.getPlace().getCountry());
                    ocor.setOcrLocal(status.getPlace().getFullName());
                    GeoLocation locs[][] = status.getPlace().getBoundingBoxCoordinates();
                    for (int x = 0; x < locs.length; x++) {
                        for (int y = 0; y < locs[x].length; y++) {
                            coords += "[" + locs[x][y].getLongitude() + "," + locs[x][y].getLatitude() + "]";
                            if (!(x == locs.length - 1 && y == locs[x].length - 1)) {
                                coords += ",";
                            }
                        }
                    }
                }

                ocor.setOcrCoordenadas(coords);
                ocor.setOcrGeo('0');
                ocor.setOcrIdentificacao('0');
                ocor.setOcrIdenper(0.0f);
                ocor.setOcrGravado('0');
                ocor.setOcrSentimento('0');
                ocor.setOcrTempo('0');

                boolean add = ocors.add(ocor);

                cont++;

                if (ocors.size() > 1000) {
                    Limpar();
                }

                //                    System.out.println(cont+" - "+status.getId() + " - " + status.getCreatedAt() + status.getPlace().getFullName());
            }
        }

        @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();
        }

        private void Limpar() {
            while (!threadGeo.isInterrupted()) {
                threadGeo.interrupt();
            }
            while (!threadMens.isInterrupted()) {
                threadMens.interrupt();
            }
            while (!threadGravar.isInterrupted()) {
                threadGravar.interrupt();
            }
            boolean achou = true;
            int x = 0;
            System.out.println("Removendo: " + ocors.size());
            while (x < ocors.size()) {
                if (ocors.get(x).getOcrGravado() != '0') {
                    ocors.remove(x);
                } else {
                    x++;
                }
            }
            System.out.println("Final: " + ocors.size());
            if (!threadGeo.isAlive()) {
                threadGeo.start();
            }
            if (!threadMens.isAlive()) {
                threadMens.start();
            }
            if (!threadGravar.isAlive()) {
                threadGravar.start();
            }
        }
    };

    FilterQuery filter = new FilterQuery();

    double[][] location = new double[2][2];

    location[0][0] = Double.parseDouble(coords[0]);
    location[0][1] = Double.parseDouble(coords[1]);
    location[1][0] = Double.parseDouble(coords[4]);
    location[1][1] = Double.parseDouble(coords[5]);

    filter.locations(location);

    twitterStream.addListener(listener);

    twitterStream.filter(filter);
}

From source file:be.ugent.tiwi.sleroux.newsrec.newsreclib.newsFetch.storm.spouts.TweetsSpout.java

License:Apache License

private FilterQuery getFilterQuery() throws DaoException {
    FilterQuery f = new FilterQuery();
    List<Long> users = followersDao.getUsersToFollow();
    long[] usersArray = new long[users.size()];
    int i = 0;/*from  w w w.j av a2s.co  m*/
    for (Long l : users) {
        usersArray[i] = l;
        i++;
    }
    f.follow(usersArray);
    return f;
}

From source file:be.ugent.tiwi.sleroux.newsrec.twittertest.StreamReaderService.java

public void readTwitterFeed() {

    TwitterStream stream = TwitterStreamBuilderUtil.getStream();

    StatusListener listener = new StatusListener() {

        @Override/* w w  w. j ava  2s .  com*/
        public void onException(Exception e) {
        }

        @Override
        public void onTrackLimitationNotice(int n) {
        }

        @Override
        public void onStatus(Status status) {
            if (status.getLang().equals("en")) {
                System.out.println(status.getText());
            }
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }

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

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

    stream.addListener(listener);
    FilterQuery f = new FilterQuery();
    f.language(new String[] { "en" });
    f.follow(new long[] { 816653 });
    stream.filter(f);

    try {
        Thread.sleep(60000);
    } catch (InterruptedException ex) {
        Logger.getLogger(StreamReaderService.class.getName()).log(Level.SEVERE, null, ex);
    }
    stream.shutdown();
}

From source file:beproject.myTwitter.java

License:Open Source License

@Override
public void run() {
    f = new ArrayList<>();
    fq = new FilterQuery();
    try {/*w w  w.j a v a 2s  .  c  om*/
        ResultSet rs = stmt.executeQuery("select moviename from movienames");
        while (rs.next()) {
            f.add(rs.getString("movieName"));
        }
        String[] tmp = new String[f.size()];
        f.toArray(tmp);
        fq.track(tmp);
        sl = new myStatusListener();
        ts.addListener(sl);
        ts.filter(fq);
    } catch (SQLException ex) {
        ExceptionManager.handleException(ex, "");
    }
}

From source file:birdseye.Sample.java

License:Apache License

public List<TweetData> execute(String[] args) throws TwitterException {

    final List<TweetData> statuses = new ArrayList();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthAccessToken("14538839-3MX2UoCEUaA6u95iWoYweTKRbhBjqEVuK1SPbCjDV");
    cb.setOAuthAccessTokenSecret("nox7eYyOJpyiDiISHRDou90bGkHKasuw1IMqqJUZMaAbj");

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

    StatusListener listener = new StatusListener() {

        public void onStatus(Status status) {
            String user = status.getUser().getScreenName();
            String content = status.getText();
            TweetData newTweet = new TweetData(user, content);

            statuses.add(newTweet);//from ww  w .  ja  va2s.c o  m
            System.out.println(statuses.size() + ":" + status.getText());
            if (statuses.size() > 15) {
                synchronized (lock) {
                    lock.notify();
                }
                System.out.println("unlocked");
            }
        }

        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) {
            System.out.println(sw.getMessage());

        }
    };

    FilterQuery fq = new FilterQuery();
    String[] keywords = args;

    fq.track(keywords);

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

    try {
        synchronized (lock) {
            lock.wait();
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("returning statuses");
    twitterStream.shutdown();
    return statuses;
}

From source file:br.com.porcelli.hornetq.integration.twitter.stream.impl.StatusStreamHandler.java

License:Apache License

@Override
public void startStream() throws TwitterException {
    if (data.getMentionedUsers() != null || data.getHashTags() != null) {
        final FilterQuery fq = new FilterQuery();
        if (data.getUserIds() != null) {
            fq.follow(data.getUserIds());
        }//from   ww  w . j a v a 2s .  com
        if (data.getHashTags() != null) {
            fq.track(data.getHashTags());
        }
        twitterStream.filter(fq);

    } else {
        twitterStream.firehose(1000);
    }
}

From source file:cats.twitter.collect.Collect.java

public Collect(User user, String name, int duree, Optional<String[]> keywords, Optional<String[]> followsString,
        Optional<Long[]> follows, Optional<double[][]> location, Optional<String> lang,
        CorpusRepository repository, TweetRepository tweetRepository) {
    this.tweetRepository = tweetRepository;
    this.corpusRepository = repository;
    tweets = new ArrayList<>();
    corpus = new Corpus();
    corpus.setName(name);//from  www . ja v  a2  s  . com
    corpus.setDuree(duree);
    corpus.setKeyWords(keywords.isPresent() ? keywords.get() : null);
    corpus.setFollows(followsString.isPresent() ? followsString.get() : null);
    corpus.setLocation(location.isPresent() ? location.get() : null);
    corpus.setUser(user);
    corpus.setLang(lang.isPresent() ? lang.get() : null);
    corpus = corpusRepository.save(corpus);

    this.user = user;
    this.duree = duree;

    filter = new FilterQuery();
    if (keywords.isPresent())
        filter.track(keywords.get());
    if (location.isPresent())
        filter.locations(location.get());
    if (follows.isPresent()) {
        long[] tmp = new long[follows.get().length];
        for (int i = 0; i < follows.get().length; i++)
            tmp[i] = follows.get()[i];
        filter.follow(tmp);
    }
}

From source file:cc.twittertools.stream.GatherStatusStream.java

License:Apache License

public static void main(String[] args) throws TwitterException {
    PatternLayout layoutStandard = new PatternLayout();
    layoutStandard.setConversionPattern("[%p] %d %c %M - %m%n");

    PatternLayout layoutSimple = new PatternLayout();
    layoutSimple.setConversionPattern("%m%n");

    // Filter for the statuses: we only want INFO messages
    LevelRangeFilter filter = new LevelRangeFilter();
    filter.setLevelMax(Level.INFO);
    filter.setLevelMin(Level.INFO);
    filter.setAcceptOnMatch(true);/*from   w ww  .j a  va  2s.  c o m*/
    filter.activateOptions();

    TimeBasedRollingPolicy statusesRollingPolicy = new TimeBasedRollingPolicy();
    statusesRollingPolicy.setFileNamePattern("statuses.log" + HOUR_ROLL);
    statusesRollingPolicy.activateOptions();

    RollingFileAppender statusesAppender = new RollingFileAppender();
    statusesAppender.setRollingPolicy(statusesRollingPolicy);
    statusesAppender.addFilter(filter);
    statusesAppender.setLayout(layoutSimple);
    statusesAppender.activateOptions();

    TimeBasedRollingPolicy warningsRollingPolicy = new TimeBasedRollingPolicy();
    warningsRollingPolicy.setFileNamePattern("warnings.log" + HOUR_ROLL);
    warningsRollingPolicy.activateOptions();

    RollingFileAppender warningsAppender = new RollingFileAppender();
    warningsAppender.setRollingPolicy(statusesRollingPolicy);
    warningsAppender.setThreshold(Level.WARN);
    warningsAppender.setLayout(layoutStandard);
    warningsAppender.activateOptions();

    ConsoleAppender consoleAppender = new ConsoleAppender();
    consoleAppender.setThreshold(Level.WARN);
    consoleAppender.setLayout(layoutStandard);
    consoleAppender.activateOptions();

    // configures the root logger
    Logger rootLogger = Logger.getRootLogger();
    rootLogger.setLevel(Level.INFO);
    rootLogger.removeAllAppenders();
    rootLogger.addAppender(consoleAppender);
    rootLogger.addAppender(statusesAppender);
    rootLogger.addAppender(warningsAppender);

    // creates a custom logger and log messages
    final Logger logger = Logger.getLogger(GatherStatusStream.class);

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    RawStreamListener rawListener = new RawStreamListener() {

        @Override
        public void onMessage(String rawString) {
            cnt++;
            logger.info(rawString);
            if (cnt % 100 == 0) {
                System.out.println(cnt + " messages received.");
            }
        }

        @Override
        public void onException(Exception ex) {
            logger.warn(ex);
        }

    };

    twitterStream.addListener(rawListener);
    FilterQuery fq = new FilterQuery();
    /* double locations[][]={{112.0,-44.0},{155.0,-10.5}}; 
     fq.locations(locations);*/
    long ids[] = { 87818409, 16675569 };
    fq.follow(ids);
    twitterStream.filter(fq);
    //twitterStream.sample();
}