Example usage for twitter4j Query setMaxId

List of usage examples for twitter4j Query setMaxId

Introduction

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

Prototype

public void setMaxId(long maxId) 

Source Link

Document

If specified, returns tweets with status ids less than the given id.

Usage

From source file:org.gabrielebaldassarre.twitter.tweet.QueryResultIterator.java

License:Open Source License

/**
 * Check if the collection has a next value
 * /*from   w w w  .  j a  va2s  .  c o m*/
 */
public boolean hasNext() {
    // Check against the limit of tweets
    if (t.getQuery() == null || t.alreadyRetrieved() >= t.getLimit())
        return false;

    // Perform the search and get the last page of results
    t.search();
    current = t.getTargetFlow().getRow(t.getTargetFlow().countRows() - 1);
    List<Status> tweets = (List<Status>) current.getValue(t.getTargetFlow().getColumn("statusSet").getName());

    // No tweets were found
    if (tweets == null || tweets.size() == 0)
        return false;

    // Since the paging query won't work, manually construct the query for the second page
    Query temp = t.getQuery();
    temp.setMaxId((tweets.get(tweets.size() - 1)).getId() - 1l);
    t.query(temp);

    // If here, we have valid results
    return true;

}

From source file:org.getlantern.firetweet.loader.support.TweetSearchLoader.java

License:Open Source License

@NonNull
@Override/*  www. ja v  a 2s.  c  om*/
public List<Status> getStatuses(@NonNull final Twitter twitter, final Paging paging) throws TwitterException {
    final Query query = new Query(processQuery(mQuery));
    query.setRpp(paging.getCount());
    if (paging.getMaxId() > 0) {
        query.setMaxId(paging.getMaxId());
    }
    return twitter.search(query);
}

From source file:org.mule.twitter.TwitterConnector.java

License:Open Source License

/**
 * Returns tweets that match a specified query.
 * <p/>/*from w ww .  jav  a  2s  . c o  m*/
 * This method calls http://search.twitter.com/search.json
 * <p/>
 * {@sample.xml ../../../doc/twitter-connector.xml.sample twitter:search}
 *
 * @param query The search query.
 * @param lang Restricts tweets to the given language, given by an <a href="http://en.wikipedia.org/wiki/ISO_639-1">ISO 639-1 code</a>
 * @param locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases.
 * @param maxId If specified, returns tweets with status ids less than the given id
 * @param since If specified, returns tweets since the given date. Date should be formatted as YYYY-MM-DD
 * @param sinceId Returns tweets with status ids greater than the given id.
 * @param geocode A {@link String} containing the latitude and longitude separated by ','. Used to get the tweets by users located within a given radius of the given latitude/longitude, where the user's location is taken from their Twitter profile
 * @param radius The radius to be used in the geocode -ONLY VALID IF A GEOCODE IS GIVEN-
 * @param unit The unit of measurement of the given radius. Can be 'mi' or 'km'. Miles by default.
 * @param until If specified, returns tweets with generated before the given date. Date should be formatted as YYYY-MM-DD
 * @param resultType If specified, returns tweets included popular or real time or both in the responce. Both by default. Can be 'mixed', 'popular' or 'recent'.
 * @return the {@link QueryResult}
 * @throws TwitterException when Twitter service or network is unavailable
 */
@Processor
public QueryResult search(String query, @Optional String lang, @Optional String locale, @Optional Long maxId,
        @Optional String since, @Optional Long sinceId, @Optional String geocode, @Optional String radius,
        @Default(value = Query.MILES) @Optional String unit, @Optional String until,
        @Optional String resultType) throws TwitterException {
    final Query q = new Query(query);

    if (lang != null) {
        q.setLang(lang);
    }
    if (locale != null) {
        q.setLocale(locale);
    }
    if (maxId != null && maxId.longValue() != 0) {
        q.setMaxId(maxId.longValue());
    }

    if (since != null) {
        q.setSince(since);
    }
    if (sinceId != null && sinceId.longValue() != 0) {
        q.setSinceId(sinceId.longValue());
    }
    if (geocode != null) {
        final String[] geocodeSplit = StringUtils.split(geocode, ',');
        final double latitude = Double.parseDouble(StringUtils.replace(geocodeSplit[0], " ", ""));
        final double longitude = Double.parseDouble(StringUtils.replace(geocodeSplit[1], " ", ""));
        q.setGeoCode(new GeoLocation(latitude, longitude), Double.parseDouble(radius), unit);
    }
    if (until != null) {
        q.setUntil(until);
    }
    if (resultType != null) {
        q.setResultType(resultType);
    }
    return twitter.search(q);
}

From source file:org.n52.twitter.dao.TwitterAbstractDAO.java

License:Open Source License

/**
 * @throws TwitterException - when Twitter service or network is unavailable
 * @throws DecodingException //from w  ww  .  j  a  va2s .  c  om
 */
protected Collection<TwitterMessage> executeApiRequest(Query query) throws TwitterException, DecodingException {
    LinkedList<TwitterMessage> tweets = new LinkedList<>();
    long lastID = Long.MAX_VALUE;
    int requestCount = 0;

    query.setResultType(ResultType.mixed);

    while (tweets.size() < MAX_TWEETS_TO_HARVEST) {
        if (MAX_TWEETS_TO_HARVEST - tweets.size() > TWEETS_TO_LOAD_PER_API_REQUEST) {
            query.setCount(TWEETS_TO_LOAD_PER_API_REQUEST);
        } else {
            query.setCount(MAX_TWEETS_TO_HARVEST - tweets.size());
        }
        try {
            QueryResult result = twitter.search(query);
            requestCount++;
            if (result.getTweets().isEmpty()) {
                break;
            }
            for (Status tweet : result.getTweets()) {
                TwitterMessage message = TwitterMessage.create(tweet);
                if (message != null) {
                    tweets.add(message);
                }
                if (tweet.getId() < lastID) {
                    lastID = tweet.getId();
                }
            }
            query.setMaxId(lastID - 1);
            LOGGER.debug("Progress: " + tweets.size() + "/" + MAX_TWEETS_TO_HARVEST + "(Requests: "
                    + requestCount + ")");
        } catch (TwitterException e) {
            LOGGER.error(e.getErrorMessage(), e);
            throw e;
        }
    }

    LOGGER.debug("Result count :" + tweets.size());

    return tweets;
}

From source file:org.tweetalib.android.TwitterUtil.java

License:Apache License

public static Query updateQueryWithPaging(Query query, Paging paging) {

    if (paging.getMaxId() > -1) {
        query.setMaxId(paging.getMaxId());
    }//from  w  w w.j  ava 2s .co  m
    if (paging.getSinceId() > -1) {
        query.setSinceId(paging.getSinceId());
    }
    /*
     * if (paging.getPage() != 1 && paging.getPage() != -1) {
     * query.setPage(paging.getPage()); }
     */

    return query;
}

From source file:timeline.CmdSearchTerm.java

License:Apache License

public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, IOException {

    // Check how many arguments were passed in
    if ((args == null) || (args.length != 6)) {

        System.err.println("Please provide command as following.");
        System.err.println("java -cp twitter4j-multi-oauth-0.5.jar "
                + "timeline.CmdSearchTerm consumer_key consumer_secret" + " user_token user_secret output_path "
                + "term ");
        System.exit(-1);/*  w ww.j  a  v a2  s.  c o m*/
    }

    AppOAuth AppOAuths = new AppOAuth();
    String endpoint = "/search/tweets";

    String consumer_key = null;
    try {
        consumer_key = StringEscapeUtils.escapeJava(args[0]);
    } catch (Exception e) {
        System.err.println("Argument" + args[0] + " must be an String.");
        System.exit(-1);
    }

    String consumer_secret = null;
    try {
        consumer_secret = StringEscapeUtils.escapeJava(args[1]);
    } catch (Exception e) {
        System.err.println("Argument" + args[1] + " must be an String.");
        System.exit(-1);
    }
    String user_token = null;
    try {
        user_token = StringEscapeUtils.escapeJava(args[2]);
    } catch (Exception e) {
        System.err.println("Argument" + args[2] + " must be an String.");
        System.exit(-1);
    }
    String user_secret = null;
    try {
        user_secret = StringEscapeUtils.escapeJava(args[3]);
    } catch (Exception e) {
        System.err.println("Argument" + args[3] + " must be an String.");
        System.exit(-1);
    }

    String OutputDirPath = null;
    try {
        OutputDirPath = StringEscapeUtils.escapeJava(args[4]);
    } catch (Exception e) {
        System.err.println("Argument" + args[4] + " must be an String.");
        System.exit(-1);
    }

    String term = "";
    try {
        term = StringEscapeUtils.escapeJava(args[5]);
    } catch (Exception e) {
        System.err.println("Argument" + args[5] + " must be an String.");
        System.exit(-1);
    }

    try {

        TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint, consumer_key, consumer_secret, user_token,
                user_secret);
        Twitter twitter = tf.getInstance();

        int RemainingCalls = AppOAuths.RemainingCalls - 2;
        int RemainingCallsCounter = 0;
        System.out.println("Remianing Calls: " + RemainingCalls);

        // screen_name / user_id provided by arguments
        System.out.println("Trying to create output directory");
        String filesPath = OutputDirPath + "/";
        File theDir = new File(filesPath);

        // If the directory does not exist, create it
        if (!theDir.exists()) {

            try {
                theDir.mkdirs();

            } catch (SecurityException se) {

                System.err.println("Could not create output " + "directory: " + OutputDirPath);
                System.err.println(se.getMessage());
                System.exit(-1);
            }
        }

        String fileName = filesPath + term.replace(" ", "");
        PrintWriter writer = new PrintWriter(fileName, "UTF-8");

        Query query = new Query(term);
        QueryResult result;

        List<Status> statuses = new ArrayList<>();
        int totalTweets = 0;
        int numberOfTweetsToGet = 5000;
        long lastID = Long.MAX_VALUE;

        while (totalTweets < numberOfTweetsToGet) {
            if (numberOfTweetsToGet - totalTweets > 100) {
                query.setCount(100);
            } else {
                query.setCount(numberOfTweetsToGet - totalTweets);
            }
            try {
                result = twitter.search(query);
                statuses.addAll(result.getTweets());

                if (statuses.size() > 0) {
                    for (Status status : statuses) {
                        String rawJSON = TwitterObjectFactory.getRawJSON(status);
                        writer.println(rawJSON);

                        totalTweets += 1;

                        if (status.getId() < lastID) {
                            lastID = status.getId();
                        }
                    }
                } else {
                    break;
                }
                System.out.println("totalTweets: " + totalTweets);
                statuses.clear();

            } catch (TwitterException e) {
                // e.printStackTrace();

                System.out.println("Tweets Get Exception: " + e.getMessage());

                // If rate limit reached then switch Auth user
                RemainingCallsCounter++;
                if (RemainingCallsCounter >= RemainingCalls) {

                    System.out.println("No more remianing calls");
                }

                if (totalTweets < 1) {
                    writer.close();
                    // Remove file if tweets not found
                    File fileToDelete = new File(fileName);
                    fileToDelete.delete();
                    break;
                }
            }
            query.setMaxId(lastID - 1);

            // If rate limit reached then switch Auth user
            RemainingCallsCounter++;
            if (RemainingCallsCounter >= RemainingCalls) {

                System.out.println("No more remianing calls");
                break;
            }
        }

        if (totalTweets > 0) {
            System.out.println("Total dumped tweets of " + term + " are: " + totalTweets);
        } else {

            // Remove file if tweets not found
            File fileToDelete = new File(fileName);
            fileToDelete.delete();
        }
        writer.close();
    } catch (TwitterException te) {
        // te.printStackTrace();
        System.out.println("Failed to get term results because: " + te.getMessage());
        System.exit(-1);
    }
    System.out.println("!!!! DONE !!!!");
}

From source file:tweetdownloader.cnr_stable.version.TweetDownload.java

/**
 * this method return an ArrayList of dataOfTweets
 * @param allMyTweets/*from   ww w  . ja  v  a 2 s  . co m*/
 * @return allMyTweet
 */
public ArrayList<dataOfTweet> downloadMyTweet(ArrayList<dataOfTweet> allMyTweets) {
    {
        try {
            Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search");
            RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets");
            for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) {
                dataOfTweet tweet = new dataOfTweet();

                //System.out.printf("\n\n!!! Starting loop %d\n\n", queryNumber);
                if (searchTweetsRateLimit.getRemaining() == 0) {
                    try {
                        //System.out.printf("!!! Sleeping for %d seconds due to rate limits\n", searchTweetsRateLimit.getSecondsUntilReset());
                        Thread.sleep((searchTweetsRateLimit.getSecondsUntilReset() + 2) * 1000l);
                    } catch (InterruptedException ex) {
                        java.util.logging.Logger.getLogger(TweetDownload.class.getName()).log(Level.SEVERE,
                                null, ex);
                    }
                }
                Query q = new Query(SEARCH_TERM);
                q.setCount(TWEETS_PER_QUERY);
                q.setLang("it");

                if (maxID != -1) {
                    q.setMaxId(maxID - 1);
                }

                QueryResult r = twitter.search(q);

                if (r.getTweets().isEmpty()) {
                    break;
                }
                for (Status s : r.getTweets()) {

                    totalTweets++;
                    if (maxID == -1 || s.getId() < maxID) {
                        maxID = s.getId();
                    }
                    if (s.isRetweeted() == false) {

                        tweet.setUsername(s.getUser().getScreenName());
                        tweet.setCreatedAt(s.getCreatedAt().toString());
                        tweet.setTweetText(s.getText());

                        if (s.getGeoLocation() != null) {
                            tweet.setLat(s.getGeoLocation().getLatitude());
                            tweet.setLongi(s.getGeoLocation().getLongitude());
                        }
                    }
                }
                allMyTweets.add(tweet);
            }
        } catch (TwitterException ex) {
            java.util.logging.Logger.getLogger(TweetDownload.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return allMyTweets;
}

From source file:tweets.Tweets.java

/**
 * @param args the command line arguments
 *//* w  ww  . j a  va2  s . co  m*/
public static void main(String[] args) throws Exception {

    ConfigurationBuilder configurtacion = new ConfigurationBuilder();
    configurtacion.setDebugEnabled(true).setOAuthConsumerKey("KdVOb7h8mqcjWIfkXXED2G6sJ")
            .setOAuthConsumerSecret("EXImfgfGCYIbWZIOSEeYVvaDE5oxwJZY7UIuwUusbyRWf2ds7l")
            .setOAuthAccessToken("785481406654722049-aRARaHgZujPZIdpYla4mVZIMMlvzJRb")
            .setOAuthAccessTokenSecret("YWWQwbFw8K7rxsnivwpBRZVoQlUhMYy7gOs7KUWOb8Rvy");
    Twitter twitter = new TwitterFactory(configurtacion.build()).getInstance();
    twitter.getUserTimeline();

    String hashtag = "#ElectionNight";
    Query busqueda = new Query(hashtag);
    int numeroTweets = 1500;
    long ultimo = Long.MAX_VALUE;
    ArrayList<Status> tweets = new ArrayList<Status>();

    while (tweets.size() < numeroTweets) {
        if (numeroTweets - tweets.size() > 255) {
            busqueda.setCount(255);
        } else {
            busqueda.setCount(numeroTweets - tweets.size());
        }
        try {
            QueryResult result = twitter.search(busqueda);
            tweets.addAll(result.getTweets());
            System.out.println("Generados " + tweets.size() + " tweets" + "\n");
            for (Status t : tweets) {
                if (t.getId() < ultimo) {
                    ultimo = t.getId();
                }
                System.out.println("Generados " + tweets.size() + " tweets" + "\n");

            }

        } catch (TwitterException excepcion) {
            System.out.println("Sin conexin " + excepcion);
        }
        ;
        busqueda.setMaxId(ultimo - 1);
    }

    // guardamos los datos
    java.util.Date fecha = new Date();
    int hora = fecha.getHours();
    int minuto = fecha.getMinutes();
    System.out.println();
    String nombre = "Datos_" + hashtag + " " + hora + " " + minuto + ".txt";
    File f = new File(nombre);
    FileWriter fw = new FileWriter(f);
    BufferedWriter bw = new BufferedWriter(fw);

    System.out.println("identificador\tUsuario\ttweet\n");
    for (int i = 0; i < tweets.size(); i++) {
        Status estadoTweet = (Status) tweets.get(i);

        GeoLocation loc = estadoTweet.getGeoLocation();
        String user = estadoTweet.getUser().getScreenName();
        String msg = estadoTweet.getText();
        Boolean sensitive = estadoTweet.isPossiblySensitive();
        int fav = estadoTweet.getFavoriteCount();

        System.out.println("Id: " + i + "| User: " + user + "| Texto: " + msg + "| SentimientoPositivo: "
                + sensitive + "$\n");
        int id = i + 1;
        bw.append(msg + " | " + sensitive + ";");
        bw.newLine();

    }
    bw.close();
}

From source file:TwitterStats.Facade.Twitter.java

public List<Status> getTuitsCuenta(String user, Date desde, Date hasta, int cantidad) throws TwitterException {
    SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
    Query q = new Query("from:" + user + " since:" + dt.format(desde) + " until:" + dt.format(hasta));
    q.setCount(100);//from ww w. j  a  v a2  s.  c o  m
    List<Status> lista = new ArrayList<>();
    List<Status> res = twitter.search(q).getTweets();

    while (res.size() > 1 && lista.size() < 3000) {
        lista.addAll(res);

        q.setMaxId(res.get(res.size() - 1).getId());
        res = twitter.search(q).getTweets();
    }

    Collections.sort(lista, new Comparator<Status>() {

        public int compare(Status s1, Status s2) {
            return s1.getFavoriteCount() > s2.getFavoriteCount() ? -1
                    : (s1.getFavoriteCount() < s2.getFavoriteCount()) ? 1 : 0;
        }
    });

    if (lista.size() > cantidad) {
        return lista.subList(0, cantidad);
    } else {
        return lista;
    }
}

From source file:TwitterStats.Facade.Twitter.java

public Map<String, Integer> getTendencias(String user, Date desde, Date hasta) throws TwitterException {
    Map<String, Integer> tendencias = new HashMap<>();

    SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
    Query q = new Query("from:" + user + " since:" + dt.format(desde) + " until:" + dt.format(hasta));

    q.setCount(100);/*from  w ww.j a  v  a 2  s .  co  m*/
    List<Status> lista = new ArrayList<>();
    List<Status> res = twitter.search(q).getTweets();

    while (res.size() > 1 && lista.size() < 3000) {
        lista.addAll(res);

        q.setMaxId(res.get(res.size() - 1).getId());
        res = twitter.search(q).getTweets();
    }

    for (Status status : lista) {
        HashtagEntity[] ht = status.getHashtagEntities();
        for (int i = 0; i < ht.length; i++) {
            String hash = ht[i].getText();
            if (tendencias.containsKey(hash)) {
                tendencias.put(hash, tendencias.get(hash) + 1);
            } else {
                tendencias.put(hash, 1);
            }
        }
    }

    return sortByValue(tendencias);
}