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: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   ww  w.  j  ava 2  s .  com*/
    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:humbala.Bonbon.java

public QueryResult search(String keyword) throws TwitterException {
    Query query = new Query(keyword);
    return twitter.search(query);
}

From source file:JavaFiles.TwitterAPI.java

public Report getTweet(Report report, String keyword) {

    try {//from w w  w  .  j a va 2s.  co 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;/* ww  w .  j av a 2s  .  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 w  w. ja v a 2s  . c  o m*/
 */
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/*from   w w  w.  ja va 2 s.c  om*/
 * @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;
}

From source file:libreriatwitter.Metodos.java

/**
 * Buscador de Tweets//ww  w  .j a  va  2 s.  c om
 * @param buscar
 * @throws twitter4j.TwitterException
 */
public void buscarTweet(String buscar) throws TwitterException {

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

}

From source file:mashup.MashTweetManager.java

public static ArrayList<String> getTweets(String topic) throws IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthAccessToken(Common.AccessToken)
            .setOAuthAccessTokenSecret(Common.AccessTokenSecret).setOAuthConsumerKey(Common.ConsumerKey)
            .setOAuthConsumerSecret(Common.ConsumerSecret);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();/*from  w  w w . j av  a 2s.co m*/

    //Twitter twitter = new TwitterFactory().getInstance();

    //  twitter.setOAuthAccessToken(null);

    ArrayList<String> tweetList = new ArrayList<>();

    try {
        Query query = new Query(topic.toLowerCase().trim());

        query.setCount(100);
        query.setLocale("en");
        query.setLang("en");

        QueryResult result = null;

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

            for (Status tweet : tweets) {
                String data = tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", "");

                HinghlishStopWords.removeStopWords(data.trim());
                // Remove Special... Characters 
                // Remove stop words 
                tweetList.add(tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", ""));

            }

        } while ((query = result.nextQuery()) != null);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return tweetList;
}

From source file:mitwitter.MiTwitter.java

/**
 * @param args the command line arguments
 *///from  ww  w .  ja v  a 2  s  .co m
public static void main(String[] args) throws TwitterException {
    // TODO code application logic here

    //camobios

    Twitter twitter = new TwitterFactory().getInstance();

    //CODIGO CAMBIAR ESTADO
    Status miStatus = twitter.updateStatus("Tercer ejemplo");//cambia el estado de twitter
    System.out.println(miStatus.getText());

    //CODIGO TIMELINE
    List<Status> statuses = twitter.getHomeTimeline();
    System.out.println("Showing home timeline.");//aparecen los ultimos twitts
    for (Status status : statuses) {
        System.out.println(status.getUser().getName() + ":" + status.getText());
    }

    //CODIGO BUSCAR TAGS
    Query query = new Query("Chelsea"); //Dentro del String va el tag que quieres buscar
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }

}

From source file:moderation.Moderate.java

public List getTwitterpost() {

    List posts = new ArrayList();
    try {/*from w  w w . ja  v  a2 s  .  com*/
        List savedpost = getSavedList(album_id);
        String tagname = this.hash;
        Twitter twitter = setting.TwitterToken.twitterObject();
        Query query = new Query(tagname);
        query.setCount(50);
        QueryResult result;
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            System.out.println("\n\n\n" + tweet);
            PostModel post = new PostModel();
            post.setAlbum_id(this.album_id);
            if (savedpost.contains(tweet.getId()))
                post.setStatus("old");
            else
                post.setStatus("new");
            post.setPost_id("" + tweet.getId());
            post.setSender_name(URLEncoder.encode(tweet.getUser().getScreenName(), "UTF-8"));
            post.setCaption_text(URLEncoder.encode(tweet.getText(), "UTF-8"));
            post.setSender_pic(tweet.getUser().getProfileImageURL());
            post.setSender_id("" + tweet.getUser().getId());

            for (MediaEntity mediaEntity : tweet.getMediaEntities()) {
                post.setImage_standard(mediaEntity.getMediaURL());
                post.setImage_low(mediaEntity.getMediaURL());
            }
            post.setPost_time(tweet.getCreatedAt().toString());
            post.setType("twitter_post");
            post.setLink(null);
            post.setParam("post_id=" + post.getPost_id() + "&album_id=" + post.getAlbum_id() + "&type="
                    + post.getType() + "&post_time=" + post.getPost_time() + "&link=" + post.getLink()
                    + "&pic_low=" + post.getImage_low() + "&pic_standard=" + post.getImage_standard()
                    + "&post_message=" + post.getCaption_text() + "&sender_name=" + post.getSender_name()
                    + "&sender_id=" + post.getSender_id() + "&sender_pic=" + post.getSender_pic());

            posts.add(post);

        }
        this.twitternext = result.nextQuery();

    } catch (Exception e) {
        System.err.println("Exception occure in getTwitter " + e);
    }

    return posts;
}