Example usage for twitter4j Query Query

List of usage examples for twitter4j Query Query

Introduction

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

Prototype

public Query(String query) 

Source Link

Usage

From source file:es.portizsan.twitrector.tasks.TweetSearchTask.java

License:Open Source License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    long before = System.currentTimeMillis() - (1000 * 60 * 15);
    try {/*  w  ww  .j a  v  a  2  s.  co  m*/
        List<Twitrector> trl = new TwitrectorService().getTwitrectors();
        if (trl == null || trl.isEmpty()) {
            logger.log(Level.WARNING, "No Twitrectors found!!!!!");
            return;
        }
        for (Twitrector tr : trl) {
            logger.info("Searching for :" + tr.getQuery());
            String search = tr.getQuery();
            Twitter twitter = new TwitterService().getTwitterInstance();
            Query query = new Query(search);
            query.setLocale("es");
            query.setCount(100);
            if (tr.getLocation() != null) {
                GeoLocation location = new GeoLocation(tr.getLocation().getLatitude(),
                        tr.getLocation().getLongitude());
                Unit unit = Unit.valueOf(tr.getLocation().getUnit().name());
                query.setGeoCode(location, tr.getLocation().getRadius(), unit);
            }
            QueryResult result;
            do {
                result = twitter.search(query);
                List<Status> tweets = result.getTweets();
                for (Status tweet : tweets) {
                    if (tweet.getCreatedAt().getTime() < before)
                        continue;
                    Queue queue = QueueFactory.getQueue("default");
                    queue.add(TaskOptions.Builder.withUrl("/tasks/tweetReply")
                            .param("statusId", String.valueOf(tweet.getId()))
                            .param("message", "@" + tweet.getUser().getScreenName() + " "
                                    + String.valueOf(tr.getResponse())));

                    logger.info("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                }
            } while ((query = result.nextQuery()) != null);
        }
    } catch (TwitterException te) {
        logger.log(Level.WARNING, "Failed to search tweets: ", te);
    }
}

From source file:es.upm.oeg.entity.extractor.extractor.gate.TwitterCorpus.java

public void createCorpus() {

    repository = new FarolasRepo();

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();/* ww  w  .  jav a2s . c  o m*/
    try {
        corpus = Factory.newCorpus("tweetcorpus");
        Query query = new Query(queryString); //"oddfarolas"
        QueryResult result;
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            Document doc = Factory.newDocument(tweet.getText());
            doc.setName(String.valueOf(tweet.getId()));
            corpus.add(doc);

            logger.info(tweet.getId() + "  @" + tweet.getUser().getScreenName() + " - " + tweet.getText() + " -"
                    + tweet.getGeoLocation());
            repository.instanciateNew(String.valueOf(tweet.getId()), tweet.getUser().getScreenName(),
                    tweet.getText(), tweet.getGeoLocation());

        }

    } catch (TwitterException te) {
        logger.error(te);
        logger.error("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    } catch (ResourceInstantiationException ex) {
        logger.error(ex);
    }
    logger.info("corpus size" + corpus.size());

}

From source file:eu.smartfp7.SocialNetworkDriver.TwitterDriver.java

License:Mozilla Public License

@Override
public void SearchForTermUsingGeolocation(String queryPar, int PageSize, double latitude, double longitude,
        double radius) {
    try {/*from w ww.  j  av a  2 s  . c om*/
        results = new ArrayList<TwitterPostData>();

        if (queryPar != null) {
            this.pageSize = PageSize;
            this.queryPar = queryPar;
            Query query = new Query(queryPar);
            query.setPage(pageIndex);
            query.setRpp(PageSize);
            query.setResultType(Query.RECENT);
            query.setGeoCode(new GeoLocation(latitude, longitude), radius, "km");
            QueryResult result = twitter.search(query);
            ArrayList tweets = (ArrayList) result.getTweets();
            for (int i = 0; i < tweets.size(); i++) {
                results.add(new TwitterPostData((Tweet) tweets.get(i)));
            }

        }
    } catch (TwitterException ex) {
        System.err.println("Twitter Error");
        // Logger.getLogger(TwitterSearch.class.getName()).log(Level.SEVERE,
        // null, ex);
    }

}

From source file:eu.smartfp7.SocialNetworkDriver.TwitterDriver.java

License:Mozilla Public License

@Override
public void setNextPage() {
    results = new ArrayList<TwitterPostData>();
    pageIndex++;/*from   w  ww.jav a 2  s. c  o m*/

    Query query = new Query(queryPar);
    query.setRpp(pageSize);
    query.setResultType(Query.RECENT);
    query.setPage(pageIndex);

    QueryResult result;
    try {
        result = twitter.search(query);
        ArrayList tweets = (ArrayList) result.getTweets();

        for (int i = 0; i < tweets.size(); i++) {
            results.add(new TwitterPostData((Tweet) tweets.get(i)));
        }
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:eu.smartfp7.SocialNetworkDriver.TwitterDriver.java

License:Mozilla Public License

public void setPreviousPage() {
    results = new ArrayList<TwitterPostData>();
    pageIndex--;/*from  w w  w .j av a 2s.  c  o m*/

    Query query = new Query(queryPar);
    query.setRpp(pageSize);
    query.setResultType(Query.RECENT);
    query.setPage(pageIndex);

    QueryResult result;
    try {
        result = twitter.search(query);
        ArrayList tweets = (ArrayList) result.getTweets();

        for (int i = 0; i < tweets.size(); i++) {
            results.add(new TwitterPostData((Tweet) tweets.get(i)));
        }
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:eu.smartfp7.SocialNetworkDriver.TwitterDriver.java

License:Mozilla Public License

@Override
public void SearchForTerm(String queryPar, Integer PageSize) {
    Query query = null;//ww  w  .j a v  a 2 s .  c o  m
    try {
        results = new ArrayList<TwitterPostData>();
        this.pageSize = PageSize;
        if (queryPar != null) {

            this.queryPar = queryPar.replace("%24", "#");
            System.out.println(this.queryPar);
            query = new Query(this.queryPar);
            query.setPage(pageIndex);
            query.setRpp(PageSize);
            query.setResultType(Query.RECENT);
            QueryResult result = twitter.search(query);
            ArrayList tweets = (ArrayList) result.getTweets();
            for (int i = 0; i < tweets.size(); i++) {
                results.add(new TwitterPostData((Tweet) tweets.get(i)));
            }

        }
    } catch (TwitterException ex) {
        System.err.println(ex);
        System.err.println(query);
        // Logger.getLogger(TwitterSearch.class.getName()).log(Level.SEVERE,
        // null, ex);
    }
}

From source file:examencodrecu.MetodosTwitter.java

public void buscarTuit(String busqueda) throws TwitterException {

    Query query = new Query(busqueda);
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }//w w w  .  j  a v a 2s.c  o  m

}

From source file:examencodtwitter.Metodos.java

/**
         //from w  ww.j  a v a  2s .  co  m
 *
 * @param busqueda
 * @throws TwitterException
 */
public void buscar(String busqueda) throws TwitterException {

    Query query = new Query(busqueda);
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }
}

From source file:examendetwitter.metodostwitter.java

/**
 * Busca tweets con el texto introducido
 *///from   w  ww  .ja v  a2 s . com
public void buscarTrending(String busqueda) throws TwitterException {

    Query query = new Query(busqueda);
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }

}

From source file:gui.project.v3.FXMLDocumentController.java

@FXML
public void btn(ActionEvent event) {
    twitter = tf.getInstance();/*from  w  w  w .  j a  va  2s. c o  m*/
    String[] fields = { ch1.getText(), ch2.getText(), ch3.getText(), ch4.getText(), ch5.getText(),
            ch6.getText(), ch7.getText(), ch8.getText(), ch9.getText(), ch10.getText() };
    int[] amount = new int[10];
    for (int i = 0; i < 10; i++) {
        amount[i] = 0;
    }
    Query query = new Query(parent.getText());
    query.setLang("en");
    QueryResult result;
    int number = 0;
    try {
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            number += tweets.size();
            for (Status tweet : tweets) {
                String tweetText = tweet.getText();
                System.out.println(tweetText);
                for (int i = 0; i < 10; i++) {
                    if ((tweetText.startsWith(fields[i] + " ") || (tweetText.endsWith(" " + fields[i])
                            || tweetText.contains(" " + fields[i] + " "))) && fields[i].length() > 0) {
                        amount[i]++;
                    }
                }
            }
        } while ((query = result.nextQuery()) != null);
    } catch (TwitterException ex) {
    }

    ObservableList<PieChart.Data> list = FXCollections.observableArrayList();

    for (int i = 0; i < 10; i++) {
        if (fields[i].length() > 0) {
            list.add(new PieChart.Data(fields[i], amount[i]));

        }
        switch (i) {
        case 0:
            lab1.setText("" + amount[i]);
            break;
        case 1:
            lab2.setText("" + amount[i]);
            break;
        case 2:
            lab3.setText("" + amount[i]);
            break;
        case 3:
            lab4.setText("" + amount[i]);
            break;
        case 4:
            lab5.setText("" + amount[i]);
            break;
        case 5:
            lab6.setText("" + amount[i]);
            break;
        case 6:
            lab7.setText("" + amount[i]);
            break;
        case 7:
            lab8.setText("" + amount[i]);
            break;
        case 8:
            lab9.setText("" + amount[i]);
            break;
        case 9:
            lab10.setText("" + amount[i]);
            break;
        default:
            System.out.print(" ");
        }
    }
    chart.setData(list);
}