Example usage for twitter4j QueryResult getTweets

List of usage examples for twitter4j QueryResult getTweets

Introduction

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

Prototype

List<Status> getTweets();

Source Link

Usage

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());
    }//from  w ww .j a  v a  2s . c o  m

}

From source file:examencodtwitter.Metodos.java

/**
         /*w  w w . j a  v a 2 s  .com*/
 *
 * @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 w w  .  j  a  v  a2 s  .c  o  m
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();/*  ww w  . j a  va2s  .  com*/
    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);
}

From source file:gui.project2.v1.FXMLDocumentController.java

@FXML
public void searchloaction() throws InterruptedException {
    int maxFollowerCount = 0;
    userslocations = new ArrayList<>();
    nodes = new ArrayList<>();

    GraphicsContext gc = graph.getGraphicsContext2D();
    gc.setFill(Color.GAINSBORO);/*from  w w w.j  a  v  a  2 s .co m*/
    gc.fillRect(0, 0, 308, 308);
    Twitter twitter;
    twitter = tf.getInstance();
    ArrayList<User> users = new ArrayList<>();
    try {
        Query query = new Query("");

        GeoLocation location;
        location = new GeoLocation(parseDouble(latitude.getText()), parseDouble(longitude.getText()));
        Query.Unit unit = Query.KILOMETERS;
        query.setGeoCode(location, parseDouble(radius.getText()), unit);
        QueryResult result;

        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();

            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                boolean q = false;
                if (userslocations != null && !userslocations.isEmpty()) {
                    for (int i = 0; i < userslocations.size(); i++) {
                        if (userslocations.get(i).getName().equals(tweet.getUser().getScreenName())) {
                            q = true;
                            break;
                        }
                    }
                }

                if (!q && tweet.getGeoLocation() != null) {
                    pair n;
                    String latString = "";
                    String lonString = "";
                    int la = 0;
                    int lo = 0;
                    String geoString = tweet.getGeoLocation().toString();
                    for (int i = 0; i < geoString.length(); i++) {
                        if (geoString.charAt(i) == '=') {
                            if (la == 0) {
                                la = 1;
                            } else if (la == -1) {
                                lo = 1;
                            }
                        } else if (geoString.charAt(i) == ',') {
                            la = -1;
                        } else if (geoString.charAt(i) == '}') {
                            lo = -1;
                        } else if (la == 1) {
                            latString = latString + geoString.charAt(i);
                        } else if (lo == 1) {
                            lonString = lonString + geoString.charAt(i);
                        }
                    }
                    User thisUser;
                    thisUser = tweet.getUser();
                    double lat = parseDouble(latString);
                    double lon = parseDouble(lonString);
                    System.out.println(tweet.getGeoLocation().toString());
                    n = new pair(tweet.getUser().getScreenName(), lat, lon);
                    userslocations.add(n);
                    users.add(thisUser);
                    if (thisUser.getFollowersCount() > maxFollowerCount) {
                        maxFollowerCount = thisUser.getFollowersCount();
                    }
                }

            }
        } while ((query = result.nextQuery()) != null);
        for (int i = 0; i < users.size(); i++) {
            if (i % 14 == 0 && i != 0) {
                Thread.sleep(1000 * 60 * 15 + 30);
            }
            IDs friends;
            friends = twitter.getFriendsIDs(users.get(i).getId(), -1);
            for (long j : friends.getIDs()) {
                for (int k = i + 1; k < users.size(); k++) {
                    if (users.get(k).getId() == j) {
                        nodes.add(users.get(i).getScreenName() + ":" + users.get(k).getScreenName());
                    }
                }
            }
        }

    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    xmin = userslocations.get(0).getA();
    xmax = userslocations.get(0).getA();
    ymin = userslocations.get(0).getB();
    ymax = userslocations.get(0).getB();
    for (int i = 1; i < userslocations.size(); i++) {
        if (xmin > userslocations.get(i).getA()) {
            xmin = userslocations.get(i).getA();
        }
        if (xmax < userslocations.get(i).getA()) {
            xmax = userslocations.get(i).getA();
        }
        if (ymin > userslocations.get(i).getB()) {
            ymin = userslocations.get(i).getB();
        }
        if (ymax < userslocations.get(i).getB()) {
            ymax = userslocations.get(i).getB();
        }
    }
    for (int i = 0; i < userslocations.size(); i++) {
        if (userslocations.get(i).getA() - xmin >= 0 && userslocations.get(i).getB() - ymin >= 0) {
            gc.setLineWidth(users.get(i).getFollowersCount() / maxFollowerCount * 3 + 1);
            gc.strokeOval((userslocations.get(i).getA() - xmin) / (xmax - xmin) * 300 + 4,
                    (userslocations.get(i).getB() - ymin) / (ymax - ymin) * 300 + 4, 4, 4);
        }

    }
    ObservableList<String> usersLeftList = FXCollections.observableArrayList();
    for (int i = 0; i < users.size() - 1; i++) {
        User k = null;
        for (int j = i + 1; j < users.size(); j++) {
            if (users.get(j).getFollowersCount() > users.get(i).getFollowersCount()) {
                k = users.get(i);
                users.set(i, users.get(j));
                users.set(j, k);
            }
        }
    }
    for (int i = 0; i < users.size() / 5; i++) {
        usersLeftList.add(users.get(i).getScreenName() + " " + users.get(i).getFollowersCount());
    }
    listView.setItems(usersLeftList);
    gc.setLineWidth(1);
    gc.setFill(Color.BLUE);
    for (int i = 0; i < nodes.size(); i++) {
        String user1 = "";
        String user2 = "";
        int p = 0;
        double x1 = 0;
        double x2 = 0;
        double y1 = 0;
        double y2 = 0;
        for (int j = 0; j < nodes.get(i).length(); j++) {
            if (nodes.get(i).charAt(j) == ':') {
                p = 1;
            } else if (p == 0) {
                user1 = user1 + nodes.get(i).charAt(j);
            } else if (p == 1) {
                user2 = user2 + nodes.get(i).charAt(j);
            }
        }
        for (int j = 0; j < userslocations.size(); j++) {
            if (userslocations.get(j).getName().equals(user1)) {
                x1 = (userslocations.get(j).getA() - xmin) / (xmax - xmin) * 300 + 6;
                y1 = (userslocations.get(j).getB() - ymin) / (ymax - ymin) * 300 + 6;
            } else if (userslocations.get(j).getName().equals(user2)) {
                x2 = (userslocations.get(j).getA() - xmin) / (xmax - xmin) * 300 + 6;
                y2 = (userslocations.get(j).getB() - ymin) / (ymax - ymin) * 300 + 6;
            }
        }
        gc.strokeLine(x1, y1, x2, y2);
        gc.fillOval(x1 - 2, y1 - 2, 4, 4);
        gc.fillOval(x2 - 2, y2 - 2, 4, 4);
    }
}

From source file:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationSearch.java

License:Open Source License

@Override
public void execute(SocialAdapterAccount account) throws SocialAdapterException {
    try {/*w  ww.  j a  va 2  s.c  o  m*/
        Twitter twitter = (Twitter) account.getProxyObject();
        Query q = new Query();
        if ((query != null) && !"".equals(query)) {
            q.setQuery(query);
        }
        if ((sinceId != null) && !"".equals(sinceId)) {
            q.setSinceId(Long.parseLong(sinceId));
        }
        if ((maxId != null) && !"".equals(maxId)) {
            q.setMaxId(Long.parseLong(maxId));
        }
        if ((since != null) && !"".equals(since)) {
            q.setSince(since);
        }
        if ((until != null) && !"".equals(until)) {
            q.setUntil(until);
        }
        if ((count != null) && !"".equals(count)) {
            q.setCount(Integer.parseInt(count));
        }

        XMLUtils parser = null;
        try {
            parser = XMLUtils.getParserInstance();
            doc = parser.newDocument("TwitterQuery");
            Element root = doc.getDocumentElement();
            parser.setAttribute(root, "user", twitter.getScreenName());
            parser.setAttribute(root, "userId", String.valueOf(twitter.getId()));
            parser.setAttribute(root, "createdAt", DateUtils.nowToString(DateUtils.FORMAT_ISO_DATETIME_UTC));

            QueryResult result;
            do {
                result = twitter.search(q);
                List<Status> tweets = result.getTweets();
                for (Status tweet : tweets) {
                    dumpTweet(parser, root, tweet);
                }
            } while ((q = result.nextQuery()) != null);
        } catch (Exception exc) {
            logger.error("Error formatting TwitterOperationSearch query[" + query + "], sinceId[" + sinceId
                    + "], maxId[" + maxId + "], since[" + since + "], until[" + until + "] and count[" + count
                    + "] response.", exc);
            throw new SocialAdapterException("Error formatting TwitterOperationSearch query[" + query
                    + "], sinceId[" + sinceId + "], maxId[" + maxId + "], since[" + since + "], until[" + until
                    + "] and count[" + count + "] response.", exc);
        } finally {
            XMLUtils.releaseParserInstance(parser);
        }
    } catch (NumberFormatException exc) {
        logger.error("Call to TwitterOperationSearch failed. Check query[" + query + "], sinceId[" + sinceId
                + "], maxId[" + maxId + "], since[" + since + "], until[" + until + "] and count[" + count
                + "] format.", exc);
        throw new SocialAdapterException("Call to TwitterOperationSearch failed. Check query[" + query
                + "], sinceId[" + sinceId + "], maxId[" + maxId + "], since[" + since + "], until[" + until
                + "] and count[" + count + "] format.", exc);
    }
}

From source file:JavaFiles.TwitterAPI.java

public Report getTweet(Report report, String keyword) {

    try {/*from www  . j av a 2s. c  o  m*/
        Query query = new Query(keyword);
        QueryResult result = twitter.search(query);

        for (Status status : result.getTweets()) {
            report.setTweet(status.getText());
            report.setTweetID(status.getId());
            report.setUserID(status.getUser().getName());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
    }
    return report;
}

From source file:Jums.AllAPI.java

public int TweetCount(String word) {
    int i = 0;/*from  w  ww.j a v a 2 s.  c  o m*/
    try {
        Query query = new Query(word);
        query.setCount(100);
        Date date = this.SetDate();
        query.setSince(this.SetSDF());
        QueryResult result = this.tw.search(query);
        for (Status status : result.getTweets()) {
            if (date.compareTo(status.getCreatedAt()) < 0) {
                i++;
            }
        }
    } catch (TwitterException te) {
        System.out.println(te.getMessage());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return i;
}

From source file:Jums.SearchTweets.java

License:Apache License

/**
 * Usage: java twitter4j.examples.search.SearchTweets [query]
 *
 * @param args search query//from w ww. j  a  va2  s .com
 */
public void main(String args) {
    System.out.println("java twitter4j.examples.search.SearchTweets [query]");
    System.exit(-1);

    Twitter twitter = new TwitterFactory().getInstance();
    try {
        Query query = new Query(args);
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }
        } while ((query = result.nextQuery()) != null);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:keypoh.TwitterCrawler.java

/**
 * Function getTimeline// ww w . j a v a 2s  .co  m
 * @param key
 * @return List
 * @throws Exception 
 */
private List getTimeline(String key) throws Exception {
    List<String> ans = new LinkedList();
    Query query = new Query(key);
    query.setCount(limit);
    QueryResult res = tw.search(query);

    List<Status> tweets = res.getTweets();
    for (Status tweet : tweets) {
        if (tweet.getText() != null) {
            ans.add(tweet.getText());
        }
    }

    return ans;
}