Example usage for twitter4j Query setCount

List of usage examples for twitter4j Query setCount

Introduction

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

Prototype

public void setCount(int count) 

Source Link

Document

sets the number of tweets to return per page, up to a max of 100

Usage

From source file:crawling.FoundUsersBySearchGeo.java

License:Apache License

public static void main(String[] args) {

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            /* my shutdown code here */
            // Record the user status
            out.println("Final status -------------------:");
            for (Long id : discoveredUsers.keySet()) {
                out.println(id + "," + discoveredUsers.get(id));
            }/*from  w  w  w.j  a  v a 2 s  .  co m*/
            out.close();

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            date = new Date();
            System.out.println("End at: " + dateFormat.format(date));
        }
    });

    if (args.length < 5) {
        System.out.println("Usage: java twitter4j.examples.PrintFilterStreamGeo lati long radius unit CITY");
        System.exit(-1);
    }
    if (args.length == 6) {
        // Preload the collected user IDs
        preloadID(args[5]);
    }

    try {
        FileWriter outFile = new FileWriter("discoveredUser" + args[4] + ".txt", true);
        out = new PrintWriter(outFile);
        //out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    avgUsers = new int[histCount];

    Query query = new Query();
    query.setGeoCode(new GeoLocation(Double.parseDouble(args[0]), Double.parseDouble(args[1])),
            Double.parseDouble(args[2]), args[3]);
    // New York Metropolitan
    //query.setGeoCode(new GeoLocation(40.7, -74), 100, "km");
    // Bay Area MetroPolitan
    //query.setGeoCode(new GeoLocation(37.78, -122.4), 100, "km");
    // LA MetroPolitan
    //query.setGeoCode(new GeoLocation(34.05, -118.24), 200, "km");

    query.setCount(100);
    //query.setPage(20);

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

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    //System.out.println();
    System.out.println("----------------------------------------------");
    System.out.println("Start at: " + dateFormat.format(date));

    while (true) {
        //for (int i = 1; i <= 15; i ++){
        //   query.setPage(i);
        try {
            doASearch(twitter, query);
        } catch (TwitterException te) {
            // TODO Auto-generated catch block
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());

            // back-off
            Thread.currentThread();
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Thread.currentThread();
        try {
            Thread.sleep(5 * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:crawling.FoundUsersBySearchHashtag.java

License:Apache License

public static void main(String[] args) {

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            /* my shutdown code here */
            // Record the user status
            out.println("Final status -------------------:");
            for (Long id : discoveredUsers.keySet()) {
                out.println(id + "," + discoveredUsers.get(id));
            }/*ww  w.j  a  v  a  2s . co m*/
            out.close();

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            date = new Date();
            System.out.println("End at: " + dateFormat.format(date));
        }
    });

    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.PrintFilterStreamHashtag hashtag");
        System.exit(-1);
    }
    if (args.length == 2) {
        // Preload the collected user IDs
        preloadID(args[1]);
    }

    try {
        FileWriter outFile = new FileWriter("discoveredUser" + args[0] + ".txt", true);
        out = new PrintWriter(outFile);
        hashtag = args[0];
        //out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    avgUsers = new int[histCount];

    Query query = new Query();
    //query.setGeoCode(new GeoLocation(Double.parseDouble(args[0])
    //      , Double.parseDouble(args[1])), Double.parseDouble(args[2]), args[3]);
    query.setQuery(hashtag);
    // New York Metropolitan
    //query.setGeoCode(new GeoLocation(40.7, -74), 100, "km");
    // Bay Area MetroPolitan
    //query.setGeoCode(new GeoLocation(37.78, -122.4), 100, "km");
    // LA MetroPolitan
    //query.setGeoCode(new GeoLocation(34.05, -118.24), 200, "km");

    query.setCount(100);
    //query.setPage(20);

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

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    //System.out.println();
    System.out.println("----------------------------------------------");
    System.out.println("Start at: " + dateFormat.format(date));

    while (true) {
        //for (int i = 1; i <= 15; i ++){
        //   query.setPage(i);
        try {
            doASearch(twitter, query);
        } catch (TwitterException te) {
            // TODO Auto-generated catch block
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());

            // back-off
            Thread.currentThread();
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Thread.currentThread();
        try {
            Thread.sleep(5 * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(String keyword) {
    try {//from www  .  ja  va2 s  .c o m
        Query query = new Query(keyword);
        query.setCount(20000);
        QueryResult result;
        result = twitter.search(query);

        //System.out.println("Getting Tweets..by Key.");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {

                collection.insert(basicObj);
            } catch (Exception e) {
                //System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:crawltweets2mongo.MonoThread.java

void getNewTweets(GeoLocation myLoc, double radius) {
    try {// w w  w .  ja va2  s .  com
        Query query = new Query();
        Query.Unit unit = Query.KILOMETERS; // or Query.MILES;
        query.setGeoCode(myLoc, radius, unit);
        if (radius > 200)
            query.setCount(20000);
        else
            query.setCount(20000);

        QueryResult result;
        result = this.twitter.search(query);

        //System.out.println("Getting Tweets. by Geo..");
        List<Status> tweets = result.getTweets();

        for (Status tweet : tweets) {

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_Rname", tweet.getUser().getName());
            basicObj.put("user_name", tweet.getUser().getScreenName());
            basicObj.put("retweet_count", tweet.getRetweetCount());
            basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount());

            UserMentionEntity[] mentioned = tweet.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", tweet.getId());
            basicObj.put("tweet_text", tweet.getText());
            Status temp1 = tweet.getRetweetedStatus();
            if (temp1 != null)
                basicObj.put("Re_tweet_ID", temp1.getUser().getId());
            GeoLocation loc = tweet.getGeoLocation();
            if (loc != null) {
                basicObj.put("Latitude", loc.getLatitude());
                basicObj.put("Longitude", loc.getLongitude());
            }
            basicObj.put("CreateTime", tweet.getCreatedAt());
            basicObj.put("FavoriteCount", tweet.getFavoriteCount());
            basicObj.put("user_Id", tweet.getUser().getId());

            if (tweet.getUser().getTimeZone() != null)
                basicObj.put("UsertimeZone", tweet.getUser().getTimeZone());
            if (tweet.getUser().getStatus() != null)
                basicObj.put("UserStatus", tweet.getUser().getStatus());
            //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates());
            String U_Loc = tweet.getUser().getLocation();
            if (U_Loc != null)
                basicObj.put("userLocation", U_Loc);
            basicObj.put("number_of_rt", tweet.getRetweetCount());
            //basicObj.put("isRetweet", tweet.getPlace().getGeometryCoordinates());                
            //basicObj.put("POS", tweet.getWithheldInCountries());

            if (mentioned.length > 0) {
                basicObj.append("mentions", pickMentions(mentioned));
            }
            try {
                //items.insert(basicObj);
                collection.insert(basicObj);
            } catch (Exception e) {
                //     System.out.println("MongoDB Connection Error : " + e.getMessage());
                //                            loadMenu();
            }
        }
        collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true));
    } catch (TwitterException ex) {
        java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:DataCollections.TweetCollections_HashTags.java

public void collectTweetsFromHashtag_popterm(Hashtag_dbo hashtag) throws InterruptedException {

    long max_id = 0;
    boolean midused = false, sidused = false;
    if (hashtag.values[Hashtag_dbo.map.get("max_id")].used) {
        max_id = hashtag.values[Hashtag_dbo.map.get("")].lnumber;
        midused = true;/*from  w ww .  j a  v  a 2 s.  c  o m*/
    }
    long since_id = 0;
    if (hashtag.values[Hashtag_dbo.map.get("since_id")].used) {
        since_id = hashtag.values[Hashtag_dbo.map.get("since_id")].lnumber;
        sidused = true;
    }
    Query q = new Query(hashtag.values[Hashtag_dbo.map.get("hashtag_popterm")].string);
    //LogPrinter.printLog("Collection Tweets for hashtag_searchterm"+hashtag.values[Hashtag_dbo.map.get("hashtag_popterm")].string);
    q.setCount(100);
    if (midused) {
        q.setMaxId(max_id);
    }
    if (sidused) {
        q.setSinceId(since_id);
    }

    QueryResult result = null;
    try {
        result = searchres.search(q);
    } catch (Exception e) {
        LogPrinter.printLog("Tweet Search Resources Rate Limit reached ");
        if (e instanceof TwitterException) {

        }
        if (e instanceof InterruptedException) {
            //Thread.sleep(((TwitterException)e).getRetryAfter()*1000+5000);
        }
    }
    int count = 0;
    for (Status s : result.getTweets()) {
        Tweet_dbo tweet = thelper.convertStatusToTweet_dbo(s);
        String whereclause = "tweet_id = " + Long.toString(tweet.values[Tweet_dbo.map.get("tweet_id")].lnumber);
        tweet.values[Tweet_dbo.map.get("processed")].setValue("true");
        tweet.values[Tweet_dbo.map.get("f_search")].setValue("true");
        tweet.values[Tweet_dbo.map.get("searchterm")]
                .setValue(hashtag.values[Hashtag_dbo.map.get("hashtag_popterm")].string);
        if (TweetsTable.select(whereclause, 0, 2).length == 0) {
            //LogPrinter.printLog(" Inserting tweet "+count+tweet.values[Tweet_dbo.map.get("tweet_id")].lnumber);
            TweetsTable.insert(tweet);
            users_edgescollections.extract_InsertUsers_EdgesFromTweet(s);
            count++;
        }
    }
}

From source file:de.jetsli.twitter.TwitterSearch.java

License:Apache License

long search(String term, Collection<Status> result, Map<String, User> userMap, int tweets,
        long lastMaxCreateTime) throws TwitterException {
    long maxId = 0L;
    long maxMillis = 0L;
    // TODO it looks like only one page is possible with 4.0.0
    int maxPages = 1;
    int hitsPerPage = tweets;

    boolean breakPaging = false;
    for (int page = 0; page < maxPages; page++) {
        Query query = new Query(term);
        // RECENT or POPULAR
        query.setResultType(Query.MIXED);

        // avoid that more recent results disturb our paging!
        if (page > 0)
            query.setMaxId(maxId);//from   w  w  w. j a v  a  2 s .c  om

        query.setCount(hitsPerPage);
        QueryResult res = twitter.search(query);

        // is res.getTweets() sorted?
        for (Status twe : res.getTweets()) {
            // determine maxId in the first page
            if (page == 0 && maxId < twe.getId())
                maxId = twe.getId();

            if (maxMillis < twe.getCreatedAt().getTime())
                maxMillis = twe.getCreatedAt().getTime();

            if (twe.getCreatedAt().getTime() + 1000 < lastMaxCreateTime)
                breakPaging = true;
            else {
                String userName = twe.getUser().getScreenName().toLowerCase();
                User user = userMap.get(userName);
                if (user == null)
                    userMap.put(userName, twe.getUser());

                result.add(twe);
            }
        }

        // minMillis could force us to leave earlier than defined by maxPages
        // or if resulting tweets are less then request (but -10 because of twitter strangeness)
        if (breakPaging || res.getTweets().size() < hitsPerPage - 10)
            break;
    }

    return maxMillis;
}

From source file:edu.american.student.util.TwitterSpider.java

License:Open Source License

/**
 * Grabs the last 100 tweet objects (Status) from a handle
 * @param handle/* w ww  .  j  a  v a 2  s. c o  m*/
 * @return
 * @throws SpiderException
 */
public static List<Status> spider(String handle) throws SpiderException {
    //connect to Twitter
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.getTwitterOAuthConsumerKey())
            .setOAuthConsumerSecret(Constants.getTwitterOAuthConsumerSecret())
            .setOAuthAccessToken(Constants.getTwitterOAuthAccessToken())
            .setOAuthAccessTokenSecret(Constants.getTwitterOAuthAccessTokenSecret());
    TwitterFactory tf = new TwitterFactory(cb.build());

    Twitter twitter = tf.getInstance();
    //get  tweets
    Query query = new Query("from:" + handle.replace("@", ""));
    query.setCount(100);
    QueryResult result;
    try {
        result = twitter.search(query);
        List<Status> toReturn = result.getTweets();
        return toReturn;
    } catch (TwitterException e) {
        String gripe = "Twitter spider failed to...err spider!";
        throw new SpiderException(gripe, e);
    }
}

From source file:edu.mum.cs.wap.TwitterUtil.java

private static List<Status> getStatusesByKeyword(String keyword) {
    try {//from   w ww  .ja v  a  2 s  .com
        Query query = new Query("movie " + keyword + "-filter:retweets");
        query.setCount(8);
        QueryResult result;
        do {
            result = twitter.search(query);
            return result.getTweets();
        } while ((query = result.nextQuery()) != null);
    } catch (TwitterException te) {
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
    return null;
}

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 w w.jav  a 2  s  .  c om*/
        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:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationSearch.java

License:Open Source License

@Override
public void execute(SocialAdapterAccount account) throws SocialAdapterException {
    try {/*from ww w. j ava2  s  .co  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);
    }
}