Example usage for twitter4j QueryResult nextQuery

List of usage examples for twitter4j QueryResult nextQuery

Introduction

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

Prototype

Query nextQuery();

Source Link

Document

Returns a Query instance to fetch next page or null if there is no next page.

Usage

From source file:TwitterPull.java

public void retrieveTweets() {
    try {/*from   ww w  .  j a v a  2s. c  o  m*/
        Query query = new Query(this.queryString);
        query.setLang("en");
        query.setCount(100);
        QueryResult result;
        int i = 0;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            //                int i = 0;
            for (Status tweet : tweets) {
                System.out.println(tweet.getText().replaceAll("\n", "").replaceAll("\r", ""));
                //                    appendTweetDocument(tweet.getText());
            }
            i++;
        } while ((query = result.nextQuery()) != null && i < 10);
        //            setTwitterFeed(tweets);

        //            System.exit(0);
    } catch (TwitterException te) {
        Logger.getLogger(TwitterPull.class.getName()).log(Level.SEVERE, null, te);
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:SearchTweets.java

License:Apache License

/**
 * Usage: java twitter4j.examples.search.SearchTweets [query]
 *
 * @param args//from  w w  w. j ava2 s  .  c om
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("java twitter4j.examples.search.SearchTweets [query]");
        System.exit(-1);
    }
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        Query query = new Query(args[0]);
        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:au.net.moon.tSearchArchiver.SearchArchiver.java

License:Open Source License

SearchArchiver() {

    Twitter twitter;/* www  .  j  a v  a 2  s. c o  m*/
    int waitBetweenRequests = 2000;
    // 2 sec delay between requests to avoid maxing out the API.
    Status theTweet;
    Query query;
    QueryResult result;

    // String[] searches;
    ArrayList<String> searchQuery = new ArrayList<String>();
    ArrayList<Integer> searchId = new ArrayList<Integer>();

    int searchIndex;
    int totalTweets;
    SimpleDateFormat myFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");

    System.out.println("tSearchArchiver: Loading search queries...");

    // Set timezone to UTC for the Twitter created at dates
    myFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    twitterAuthorise twitterAuth = new twitterAuthorise(false);
    twitter = twitterAuth.getTwitter();

    // Open the old twitter_archive database
    openSQLDataBase();

    if (isDatabaseReady()) {

        // probably should have these in an object not separate arrays?
        try {
            rs = stmt.executeQuery("select * from searches where active = true");
            // perform each search
            while (rs.next()) {
                // if (searchQuery
                searchQuery.add(rs.getString("query"));
                searchId.add(rs.getInt("id"));
            }
            if (rs.wasNull()) {
                System.out.println("tSearchArchiver: No searches in the table \"searches\"");
                System.exit(30);
            } else {
                System.out.println("tSearchArchiver: Found " + searchQuery.size() + " searches.");
            }
        } catch (SQLException e) {
            System.out.println("tSearchArchiver: e:" + e.toString());
        }

        searchIndex = 0;
        totalTweets = 0;

        // set initial value of i to start from middle of search set
        while (searchIndex < searchQuery.size()) {

            query = new Query();
            query.setQuery(searchQuery.get(searchIndex));
            // check to see if their are any tweets already in the database for
            // this search
            //TODO: Change this to look in new raw data files for each search instead
            long max_tw_id = 0;
            try {
                rs = stmt.executeQuery("select max(tweet_id) as max_id from archive where search_id = "
                        + searchId.get(searchIndex));
                if (rs.next()) {
                    max_tw_id = rs.getLong("max_id");
                    // System.out.println("MaxID: " + max_tw_id);
                    query.setSinceId(max_tw_id);
                }
            } catch (SQLException e1) {
                System.err.println("tSearchArchiver: Error looking for maximum tweet_id for " + query.getQuery()
                        + " in archive");
                e1.printStackTrace();
            }
            // System.out.println("Starting searching for tweets for: " +
            // query.getQuery());

            // new style replacement for pagination
            //   Query query = new Query("whatEverYouWantToSearch"); 
            //   do { 
            //       result = twitter.search(query); 
            //       System.out.println(result); 
            // do something 
            //      } while ((query = result.nextQuery()) != null); 
            // TODO: check if twitter4j is doing all the backing off handling already

            int tweetCount = 0;
            Boolean searching = true;
            do {

                // delay waitBetweenRequests milliseconds before making request
                // to make sure not overloading API
                try {
                    Thread.sleep(waitBetweenRequests);
                } catch (InterruptedException e1) {
                    System.err.println("tSearchArchiver: Sleep between requests failed.");
                    e1.printStackTrace();
                }
                try {
                    result = twitter.search(query);
                } catch (TwitterException e) {
                    System.out.println(e.getStatusCode());
                    System.out.println(e.toString());
                    if (e.getStatusCode() == 503) {
                        // TODO use the Retry-After header value to delay & then
                        // retry the request
                        System.out
                                .println("tSearchArchiver: Delaying for 10 minutes before making new request");
                        try {
                            Thread.sleep(600000);
                        } catch (InterruptedException e1) {
                            System.err.println(
                                    "tSearchArchiver: Sleep for 10 minutes because of API load failed.");
                            e1.printStackTrace();
                        }
                    }
                    result = null;
                }

                if (result != null) {
                    List<Status> results = result.getTweets();
                    if (results.size() == 0) {
                        searching = false;
                    } else {
                        tweetCount += results.size();
                        for (int j = 0; j < results.size(); j++) {
                            theTweet = (Status) results.get(j);
                            String cleanText = theTweet.getText();
                            cleanText = cleanText.replaceAll("'", "&#39;");
                            cleanText = cleanText.replaceAll("\"", "&quot;");

                            try {
                                stmt.executeUpdate("insert into archive values (0, " + searchId.get(searchIndex)
                                        + ", '" + theTweet.getId() + "', now())");
                            } catch (SQLException e) {
                                System.err.println("tSearchArchiver: Insert into archive failed.");
                                System.err.println(searchId.get(searchIndex) + ", " + theTweet.getId());

                                e.printStackTrace();
                            }
                            // TODO: change to storing in file instead of database
                            try {
                                rs = stmt.executeQuery("select id from tweets where id = " + theTweet.getId());
                            } catch (SQLException e) {
                                System.err.println(
                                        "tSearchArchiver: checking for tweet in tweets archive failed.");
                                e.printStackTrace();
                            }
                            Boolean tweetNotInArchive = false;
                            try {
                                tweetNotInArchive = !rs.next();
                            } catch (SQLException e) {
                                System.err.println(
                                        "tSearchArchiver: checking for tweet in archive failed at rs.next().");
                                e.printStackTrace();
                            }
                            if (tweetNotInArchive) {
                                String tempLangCode = "";
                                // getIsoLanguageCode() has been removed from twitter4j
                                // looks like it might be added back in in the next version
                                //                           if (tweet.getIsoLanguageCode() != null) {
                                //                              if (tweet.getIsoLanguageCode().length() > 2) {
                                //                                 System.out
                                //                                 .println("tSearchArchiver Error: IsoLanguageCode too long: >"
                                //                                       + tweet.getIsoLanguageCode()
                                //                                       + "<");
                                //                                 tempLangCode = tweet
                                //                                       .getIsoLanguageCode()
                                //                                       .substring(0, 2);
                                //                              } else {
                                //                                 tempLangCode = tweet
                                //                                       .getIsoLanguageCode();
                                //                              }
                                //                           }
                                double myLatitude = 0;
                                double myLongitude = 0;
                                int hasGeoCode = 0;

                                if (theTweet.getGeoLocation() != null) {
                                    System.out.println("GeoLocation: " + theTweet.getGeoLocation().toString());
                                    myLatitude = theTweet.getGeoLocation().getLatitude();
                                    myLongitude = theTweet.getGeoLocation().getLongitude();
                                    hasGeoCode = 1;
                                }
                                Date tempCreatedAt = theTweet.getCreatedAt();
                                String myDate2 = myFormatter
                                        .format(tempCreatedAt, new StringBuffer(), new FieldPosition(0))
                                        .toString();
                                totalTweets++;
                                try {
                                    stmt.executeUpdate("insert into tweets values  (" + theTweet.getId() + ", '"
                                            + tempLangCode + "', '" + theTweet.getSource() + "', '" + cleanText
                                            + "', '" + myDate2 + "', '" + theTweet.getInReplyToUserId() + "', '"
                                            + theTweet.getInReplyToScreenName() + "', '"
                                            + theTweet.getUser().getId() + "', '"
                                            + theTweet.getUser().getScreenName() + "', '" + hasGeoCode + "',"
                                            + myLatitude + ", " + myLongitude + ", now())");
                                } catch (SQLException e) {
                                    System.err.println("tSearchArchiver: Insert into tweets failed.");
                                    System.err.println(theTweet.getId() + ", '" + tempLangCode + "', '"
                                            + theTweet.getSource() + "', '" + cleanText + "', '" + myDate2
                                            + "', '" + theTweet.getInReplyToUserId() + "', '"
                                            + theTweet.getInReplyToScreenName() + "', '"
                                            + theTweet.getUser().getId() + "', '"
                                            + theTweet.getUser().getScreenName());

                                    e.printStackTrace();
                                }
                            }

                        }
                    }
                }
            } while ((query = result.nextQuery()) != null && searching);

            if (tweetCount > 0) {
                System.out.println("tSearchArchiver: New Tweets Found for \"" + searchQuery.get(searchIndex)
                        + "\" = " + tweetCount);
            } else {
                // System.out.println("tSearchArchiver: No Tweets Found for \""
                // + searchQuery.get(searchIndex) + "\" = " + tweetCount);
            }
            try {

                stmt.executeUpdate("update searches SET lastFoundCount=" + tweetCount
                        + ", lastSearchDate=now() where id=" + searchId.get(searchIndex));
            } catch (SQLException e) {
                System.err.println("tSearchArchiver: failed to update searches with lastFoundCount="
                        + tweetCount + " and datetime for search: " + searchId.get(searchIndex));
                e.printStackTrace();
            }
            searchIndex++;
        }
        System.out.println("tSearchArchiver: Completed all " + searchQuery.size() + " searches");
        System.out.println("tSearchArchiver: Archived " + totalTweets + " new tweets");
    }
}

From source file:Classes.TwitterPull.java

public void retrieveTweets() throws TwitterException {
    Query query = new Query("\"" + this.queryString + "\"");
    query.setLang("en");
    query.setCount(100);//from  www . j  a v a  2 s  . co m
    QueryResult result;
    int i = 0;
    do {
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            String t = tweet.getText().replaceAll("\n", "").replaceAll("\r", "");
            //                appendTweetDocument(t);
            retrievedTweets.add(t);
        }
        i++;
    } while ((query = result.nextQuery()) != null && i < 50);

}

From source file:com.daiv.android.twitter.ui.drawer_activities.discover.NearbyTweets.java

License:Apache License

public void getTweets() {

    canRefresh = false;//from  ww w .ja  va 2 s  .  c  om

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings);

                boolean manualLoc = sharedPrefs.getBoolean("manually_config_location", false);

                int i = 0;
                while (!connected && i < 5 && !manualLoc) {
                    try {
                        Thread.sleep(1500);
                    } catch (Exception e) {

                    }

                    i++;
                }

                double latitude = -1;
                double longitude = -1;

                if (manualLoc) {
                    // need to query yahoos api for the location...
                    double[] loc = getLocationFromYahoo(sharedPrefs.getInt("woeid", 2379574));
                    latitude = loc[0];
                    longitude = loc[1];
                } else {
                    // set it from the location client
                    Location location = mLastLocation;
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                }

                query = new Query();
                query.setGeoCode(new GeoLocation(latitude, longitude), 10, Query.MILES);

                QueryResult result = twitter.search(query);

                if (result.hasNext()) {
                    hasMore = true;
                    query = result.nextQuery();
                } else {
                    hasMore = false;
                }

                for (Status s : result.getTweets()) {
                    statuses.add(s);
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new TimelineArrayAdapter(context, statuses);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);

                        LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (Throwable e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(context, getString(R.string.error), Toast.LENGTH_SHORT).show();
                        } catch (IllegalStateException e) {
                            // not attached to activity
                        }
                    }
                });
            }

            canRefresh = true;
        }
    }).start();
}

From source file:com.daiv.android.twitter.ui.drawer_activities.discover.NearbyTweets.java

License:Apache License

public void getMore() {
    if (hasMore) {
        canRefresh = false;//  ww  w.j a v a 2 s.c o m
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Twitter twitter = Utils.getTwitter(context, settings);
                    QueryResult result = twitter.search(query);

                    for (twitter4j.Status status : result.getTweets()) {
                        statuses.add(status);
                    }

                    if (result.hasNext()) {
                        query = result.nextQuery();
                        hasMore = true;
                    } else {
                        hasMore = false;
                    }

                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            canRefresh = true;
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            canRefresh = false;
                        }
                    });
                }
            }
        }).start();
    }
}

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.sub_fragments.ProfileMentionsFragment.java

License:Apache License

public void doSearch() {
    spinner.setVisibility(View.VISIBLE);

    new Thread(new Runnable() {
        @Override/*  w w w  .j  ava2s.com*/
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                query = new Query("@" + screenName + " -RT");
                query.sinceId(1);
                QueryResult result = twitter.search(query);

                tweets.clear();

                for (twitter4j.Status status : result.getTweets()) {
                    tweets.add(status);
                }

                if (result.hasNext()) {
                    query = result.nextQuery();
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new TimelineArrayAdapter(context, tweets);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);

                        spinner.setVisibility(View.GONE);
                        canRefresh = true;

                        if (!hasMore) {
                            View footer = inflater.inflate(R.layout.mentions_footer, null);
                            listView.addFooterView(footer);
                            listView.setFooterDividersEnabled(false);
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;

                        if (!hasMore) {
                            View footer = inflater.inflate(R.layout.mentions_footer, null);
                            listView.addFooterView(footer);
                            listView.setFooterDividersEnabled(false);
                        }
                    }
                });

            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        canRefresh = false;

                        if (!hasMore) {
                            View footer = inflater.inflate(R.layout.mentions_footer, null);
                            listView.addFooterView(footer);
                            listView.setFooterDividersEnabled(false);
                        }
                    }
                });
            }

        }
    }).start();
}

From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.sub_fragments.ProfileMentionsFragment.java

License:Apache License

public void getMore() {
    canRefresh = false;//ww  w. java 2s.  c om

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                QueryResult result = twitter.search(query);

                for (twitter4j.Status status : result.getTweets()) {
                    tweets.add(status);
                }

                if (result.hasNext()) {
                    query = result.nextQuery();
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        canRefresh = true;

                        if (!hasMore) {
                            View footer = inflater.inflate(R.layout.mentions_footer, null);
                            listView.addFooterView(footer);
                            listView.setFooterDividersEnabled(false);
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        canRefresh = false;
                        hasMore = false;
                    }
                });

            }

        }
    }).start();
}

From source file:com.daiv.android.twitter.ui.search.TwitterSearchFragment.java

License:Apache License

public void onRefreshStarted() {
    new Thread(new Runnable() {
        @Override// w w w . jav  a 2s . c o m
        public void run() {
            final long topId;
            if (tweets.size() > 0) {
                topId = tweets.get(0).getId();
            } else {
                topId = 0;
            }

            try {
                Twitter twitter = Utils.getTwitter(context, settings);
                query = new Query(searchQuery);
                if (topTweets) {
                    query.setResultType(Query.POPULAR);
                } else {
                    query.setResultType(null);
                }
                QueryResult result = twitter.search(query);

                tweets.clear();

                for (twitter4j.Status status : result.getTweets()) {
                    tweets.add(status);
                }

                if (result.hasNext()) {
                    query = result.nextQuery();
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int top = 0;
                        for (int i = 0; i < tweets.size(); i++) {
                            if (tweets.get(i).getId() == topId) {
                                top = i;
                                break;
                            }
                        }

                        adapter = new TimelineArrayAdapter(context, tweets, onlyStatus);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);
                        listView.setSelection(top);

                        spinner.setVisibility(View.GONE);

                        mPullToRefreshLayout.setRefreshing(false);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        mPullToRefreshLayout.setRefreshing(false);
                    }
                });
            }
        }
    }).start();
}

From source file:com.daiv.android.twitter.ui.search.TwitterSearchFragment.java

License:Apache License

public void doSearch(final String mQuery) {
    spinner.setVisibility(View.VISIBLE);

    if (listView.getVisibility() != View.GONE) {
        listView.setVisibility(View.GONE);
    }//from w  ww.ja  va  2  s .  c  om

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);
                Log.v("Test_searching", "query in frag: " + mQuery);
                query = new Query(mQuery);
                if (topTweets) {
                    query.setResultType(Query.ResultType.popular);
                } else {
                    query.setResultType(null);
                }
                QueryResult result = twitter.search(query);

                tweets.clear();

                for (twitter4j.Status status : result.getTweets()) {
                    tweets.add(status);
                }

                if (result.hasNext()) {
                    query = result.nextQuery();
                    hasMore = true;
                } else {
                    hasMore = false;
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new TimelineArrayAdapter(context, tweets, onlyStatus);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);

                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                    }
                });
            }
        }
    }).start();
}