Example usage for twitter4j Query setSinceId

List of usage examples for twitter4j Query setSinceId

Introduction

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

Prototype

public void setSinceId(long sinceId) 

Source Link

Document

returns tweets with status ids greater than the given id.

Usage

From source file:au.net.moon.tSearchArchiver.SearchArchiver.java

License:Open Source License

SearchArchiver() {

    Twitter twitter;//from  w w  w .  ja  v a2s.  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:com.aremaitch.codestock2010.library.TwitterLib.java

License:Apache License

public QueryResult search(long sinceId, String[] hashTags) throws TwitterException {
    Query q = new Query();
    if (sinceId >= 0) {
        q.setSinceId(sinceId);
    }// www.j a v a 2 s.  c  o m

    q.setQuery(TextUtils.join(" OR ", hashTags));
    try {
        return t.search(q);
    } catch (NumberFormatException nfe) {
        ACLogger.error(CSConstants.LOG_TAG, "number format exception was thrown by twitter4j");
    }
    return null;
}

From source file:com.concursive.connect.web.modules.profile.jobs.TwitterQueryJob.java

License:Open Source License

public void execute(JobExecutionContext context) throws JobExecutionException {
    long startTime = System.currentTimeMillis();
    LOG.debug("Starting job...");
    SchedulerContext schedulerContext = null;
    Connection db = null;/*from  ww w. ja va  2 s . co m*/

    try {
        schedulerContext = context.getScheduler().getContext();

        // Determine if the twitter hash is enabled
        ApplicationPrefs prefs = (ApplicationPrefs) schedulerContext.get("ApplicationPrefs");
        String twitterHash = prefs.get(ApplicationPrefs.TWITTER_HASH);
        if (!StringUtils.hasText(twitterHash)) {
            LOG.debug("Hash is not defined exiting from Twitter query job...");
            return;
        }

        db = SchedulerUtils.getConnection(schedulerContext);

        // Determine the previous retrieved twitter id to use for query
        Process process = new Process(db, "TwitterQueryJob");
        long sinceId = process.getLongValue();
        LOG.debug("Last saved twitter id is : " + sinceId);

        // Create Query Object for searching twitter
        Query query = new Query("#" + twitterHash);
        query.setRpp(99);
        if (sinceId > 0) {
            // Set since_id in the query
            query.setSinceId(sinceId);
        }

        // Get the Twitter search results
        Twitter twitter = new Twitter(TWITTER_BASE_URL);
        QueryResult result = twitter.search(query);
        LOG.debug("Found and retrieved " + result.getTweets().size() + " tweet(s).");

        // Iterate through the tweets and store in project history
        int count = 0;
        for (Tweet tweet : result.getTweets()) {
            count++;
            LOG.debug("Got tweet from " + tweet.getFromUser() + " as " + tweet.getText());

            // See if this matches any profiles in the system
            // @note it's possible that more than one project can have the same twitter id
            ProjectList projectList = new ProjectList();
            projectList.setTwitterId(tweet.getFromUser());
            projectList.setApprovedOnly(true);
            projectList.buildList(db);

            // Clean up the tweet output
            String message = tweet.getText();

            // Turn links into wiki links
            message = WikiUtils.addWikiLinks(message);

            // Remove the hash tag - beginning or middle
            message = StringUtils.replace(message, "#" + twitterHash + " ", "");
            // Remove the hash tag - middle or end
            message = StringUtils.replace(message, " #" + twitterHash, "");
            // Remove the hash tag - untokenized
            message = StringUtils.replace(message, "#" + twitterHash, "");

            // Update the activity stream for the matching profiles
            for (Project project : projectList) {
                ProjectHistory projectHistory = new ProjectHistory();
                projectHistory.setProjectId(project.getId());
                projectHistory.setEnabled(true);
                // If there is a user profile, use the user's id, else use the businesses id? or use a different event
                if (project.getProfile()) {
                    projectHistory.setEnteredBy(project.getOwner());
                } else {
                    projectHistory.setEnteredBy(project.getOwner());
                }
                projectHistory.setLinkStartDate(new Timestamp(System.currentTimeMillis()));
                String desc = WikiLink.generateLink(project) + " [[http://twitter.com/" + tweet.getFromUser()
                        + "/statuses/" + tweet.getId() + " tweeted]] " + message;
                projectHistory.setDescription(desc);
                projectHistory.setLinkItemId(project.getId());
                projectHistory.setLinkObject(ProjectHistoryList.TWITTER_OBJECT);
                projectHistory.setEventType(ProjectHistoryList.TWITTER_EVENT);
                // Store the tweets in project history
                projectHistory.insert(db);
            }
            // Set the tweet id as since_Id
            if (sinceId < tweet.getId()) {
                sinceId = tweet.getId();
            }
        }
        //update the recent sinceId and process timestamp
        process.setLongValue(sinceId);
        process.setProcessed(new Timestamp(new java.util.Date().getTime()));
        process.update(db);

        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        LOG.debug("Finished: " + count + " took " + totalTime + " ms");
    } catch (Exception e) {
        LOG.error("TwitterQueryJob Exception", e);
        throw new JobExecutionException(e.getMessage());
    } finally {
        SchedulerUtils.freeConnection(schedulerContext, db);
    }
}

From source file:com.infine.android.devoxx.service.TwitterService.java

License:Apache License

public void triggerRefresh() {
    try {//from  w ww.  j av a  2  s .  c  o m
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        long lastTweetId = prefs.getLong(LAST_TWEET_ID, 0);
        Query query = new Query(TWEETS_QUERY);
        if (lastTweetId > 0) {
            query.setSinceId(lastTweetId);
        }
        QueryResult result = mTwitter.search(query);
        Log.i("Twitter", "nombre de tweets recu:" + result.getTweets().size());
        ArrayList<ContentProviderOperation> databaseOps = new ArrayList<ContentProviderOperation>();
        long maxId = 0;
        for (Status status : result.getTweets()) {
            maxId = Math.max(maxId, status.getId());
            databaseOps.add(buildCPOperation(status));
        }
        // batch update
        if (!databaseOps.isEmpty()) {
            mResolver.applyBatch(ScheduleContract.CONTENT_AUTHORITY, databaseOps);
        }
        if (maxId > 0) {
            prefs.edit().putLong(LAST_TWEET_ID, maxId);
        }

    } catch (TwitterException e) {
        // pas de message c'est qui doit pas avoir de reseau
        // Toast.makeText(this, "Erreur durant la rcupration des Tweets",
        // Toast.LENGTH_SHORT).show();
    } catch (RemoteException e) {
        // pas de message
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        // pas de message
        e.printStackTrace();
    }
}

From source file:com.javielinux.api.loaders.SearchLoader.java

License:Apache License

@Override
public BaseResponse loadInBackground() {

    try {/*ww w.j av  a2 s . c o  m*/
        SearchResponse response = new SearchResponse();

        ConnectionManager.getInstance().open(getContext());

        if (entitySearch.getInt("notifications") == 1) {
            response.setInfoSaveTweets(entitySearch.saveTweets(getContext(), false, since_id));
        } else {
            response.setInfoSaveTweets(null);

            ArrayList<InfoTweet> infoTweets = new ArrayList<InfoTweet>();

            if (entitySearch.isUser()) {
                // La bsqueda es de un usuario, as que buscamos en twitter directamente
                ResponseList<Status> statuses = ConnectionManager.getInstance().getUserForSearchesTwitter()
                        .getUserTimeline(entitySearch.getString("from_user"));
                for (twitter4j.Status status : statuses) {
                    infoTweets.add(new InfoTweet(status));
                }
            } else {
                Query query = entitySearch.getQuery(getContext());
                if (since_id != -1)
                    query.setSinceId(since_id);
                QueryResult result = ConnectionManager.getInstance().getUserForSearchesTwitter().search(query);
                ArrayList<Status> tweets = (ArrayList<Status>) result.getTweets();
                for (Status tweet : tweets) {
                    infoTweets.add(new InfoTweet(tweet));
                }
            }

            response.setInfoTweets(infoTweets);
        }

        return response;
    } catch (Exception e) {
        e.printStackTrace();
        ErrorResponse response = new ErrorResponse();
        response.setError(e, e.getMessage());
        return response;
    }
}

From source file:com.javielinux.database.EntitySearch.java

License:Apache License

public InfoSaveTweets saveTweets(Context cnt, boolean saveNotifications, long since_id) {
    ConnectionManager.getInstance().open(cnt);
    Twitter twitter = ConnectionManager.getInstance().getUserForSearchesTwitter();
    InfoSaveTweets out = new InfoSaveTweets();
    try {/*from   w ww.ja va  2 s.  com*/
        int nResult = DataFramework.getInstance().getEntityListCount("tweets",
                "favorite=0 and search_id=" + getId());

        Query query = getQuery(cnt);
        if (since_id != -1)
            query.setSinceId(since_id);

        QueryResult result = twitter.search(query);
        ArrayList<Status> tweets = (ArrayList<Status>) result.getTweets();

        if (tweets.size() > 0) {

            out.setNewMessages(tweets.size());
            out.setNewerId(tweets.get(0).getId());
            out.setOlderId(tweets.get(tweets.size() - 1).getId());
            if (saveNotifications) {
                setValue("new_tweets_count", getInt("new_tweets_count") + tweets.size());
                save();
            }

            Log.d(Utils.TAG, tweets.size() + " mensajes nuevos en " + getString("name"));

            long fisrtId = 1;
            Cursor c = DataFramework.getInstance().getCursor("tweets", new String[] { DataFramework.KEY_ID },
                    null, null, null, null, DataFramework.KEY_ID + " desc", "1");
            if (!c.moveToFirst()) {
                c.close();
                fisrtId = 1;
            } else {
                long Id = c.getInt(0) + 1;
                c.close();
                fisrtId = Id;
            }

            for (int i = tweets.size() - 1; i >= 0; i--) {

                /*String sql = "INSERT INTO 'tweets' ("  + DataFramework.KEY_ID + ", search_id, url_avatar, username, user_id, tweet_id,"
                      + "text, source, to_username, to_user_id, date, favorite) VALUES (" + fisrtId + "," + getId()
                      + ",'" +tweets.get(i).getProfileImageUrl() + "','"+tweets.get(i).getFromUser()+"','"
                      + tweets.get(i).getFromUserId() + "','" + tweets.get(i).getId() + "','" + tweets.get(i).getText() 
                      + "','" + tweets.get(i).getSource()   + "','"+tweets.get(i).getToUser()
                      +"','"+tweets.get(i).getToUserId()+"','"+String.valueOf(tweets.get(i).getCreatedAt().getTime())
                      + "',0);\n";*/
                User u = tweets.get(i).getUser();
                ContentValues args = new ContentValues();
                args.put(DataFramework.KEY_ID, "" + fisrtId);
                args.put("search_id", "" + getId());
                if (u.getProfileImageURL() != null) {
                    args.put("url_avatar", u.getProfileImageURL().toString());
                } else {
                    args.put("url_avatar", "");
                }
                args.put("username", u.getScreenName());
                args.put("fullname", u.getName());
                args.put("user_id", "" + u.getId());
                args.put("tweet_id", Utils.fillZeros("" + tweets.get(i).getId()));
                args.put("text", tweets.get(i).getText());
                args.put("source", tweets.get(i).getSource());
                args.put("to_username", tweets.get(i).getInReplyToScreenName());
                args.put("to_user_id", "" + tweets.get(i).getInReplyToUserId());
                args.put("date", String.valueOf(tweets.get(i).getCreatedAt().getTime()));
                if (tweets.get(i).getGeoLocation() != null) {
                    args.put("latitude", tweets.get(i).getGeoLocation().getLatitude());
                    args.put("longitude", tweets.get(i).getGeoLocation().getLongitude());
                }
                args.put("favorite", "0");

                DataFramework.getInstance().getDB().insert("tweets", null, args);

                fisrtId++;

            }

            if (saveNotifications) {
                setValue("last_tweet_id_notifications", tweets.get(0).getId() + "");
                save();
            }

            int total = nResult + tweets.size();
            if (total > Utils.MAX_ROW_BYSEARCH) {
                Log.d(Utils.TAG, "Limpiando base de datos");
                String date = DataFramework.getInstance()
                        .getEntityList("tweets", "favorite=0 and search_id=" + getId(), "date desc")
                        .get(Utils.MAX_ROW_BYSEARCH).getString("date");
                String sqldelete = "DELETE FROM tweets WHERE favorite=0 AND search_id=" + getId()
                        + " AND date  < '" + date + "'";
                DataFramework.getInstance().getDB().execSQL(sqldelete);
            }

        }

    } catch (TwitterException e) {
        e.printStackTrace();
        RateLimitStatus rate = e.getRateLimitStatus();
        if (rate != null) {
            out.setError(Utils.LIMIT_ERROR);
            out.setRate(rate);
        } else {
            out.setError(Utils.UNKNOWN_ERROR);
        }
    } catch (Exception e) {
        e.printStackTrace();
        out.setError(Utils.UNKNOWN_ERROR);
    }
    return out;
}

From source file:com.javielinux.database.EntitySearch.java

License:Apache License

public Query getQuery(Context cnt) {
    String q = this.getString("words_and");

    if (!this.getString("words_or").equals("")) {
        q += Utils.getQuotedText(this.getString("words_or"), "OR ", false);
    }//ww w .  j a va2 s.co  m

    if (!this.getString("words_not").equals("")) {
        q += Utils.getQuotedText(this.getString("words_not"), "-", true);
    }

    if (!this.getString("from_user").equals("")) {
        q += " from:" + this.getString("from_user");
    }

    if (!this.getString("to_user").equals("")) {
        q += " to:" + this.getString("to_user");
    }

    if (!this.getString("source").equals("")) {
        q += " source:" + this.getString("source");
    }

    if (this.getInt("attitude") == 1)
        q += " :)";
    if (this.getInt("attitude") == 2)
        q += " :(";

    String modLinks = "filter:links";
    String websVideos = "twitvid OR youtube OR vimeo OR youtu.be";
    String webPhotos = "lightbox.com OR mytubo.net OR imgur.com OR instagr.am OR twitpic OR yfrog OR plixi OR twitgoo OR img.ly OR picplz OR lockerz";

    if (this.getInt("filter") == 1)
        q += " " + modLinks;
    if (this.getInt("filter") == 2)
        q += " " + webPhotos + " " + modLinks;
    if (this.getInt("filter") == 3)
        q += " " + websVideos + " " + modLinks;
    if (this.getInt("filter") == 4)
        q += " " + websVideos + " OR " + webPhotos + " " + modLinks;
    if (this.getInt("filter") == 5)
        q += " source:twitterfeed " + modLinks;
    if (this.getInt("filter") == 6)
        q += " ?";
    if (this.getInt("filter") == 7)
        q += " market.android.com OR androidzoom.com OR androlib.com OR appbrain.com OR bubiloop.com OR yaam.mobi OR slideme.org "
                + modLinks;

    Log.d(Utils.TAG, "Buscando: " + q);

    Query query = new Query(q);

    if (this.getInt("use_geo") == 1) {
        if (this.getInt("type_geo") == 0) { // coordenadas del mapa
            GeoLocation gl = new GeoLocation(this.getDouble("latitude"), this.getDouble("longitude"));
            String unit = Query.KILOMETERS;
            if (this.getInt("type_distance") == 0)
                unit = Query.MILES;
            query.setGeoCode(gl, this.getDouble("distance"), unit);
        }

        if (this.getInt("type_geo") == 1) { // coordenadas del gps
            Location loc = LocationUtils.getLastLocation(cnt);
            if (loc != null) {
                GeoLocation gl = new GeoLocation(loc.getLatitude(), loc.getLongitude());
                String unit = Query.KILOMETERS;
                if (this.getInt("type_distance") == 0)
                    unit = Query.MILES;
                query.setGeoCode(gl, this.getDouble("distance"), unit);
            } else {
                mErrorLastQuery = cnt.getString(R.string.no_location);
            }
        }
    }

    PreferenceManager.setDefaultValues(cnt, R.xml.preferences, false);
    SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(cnt);

    int count = Integer.parseInt(preference.getString("prf_n_max_download", "60"));
    if (count <= 0)
        count = 60;

    query.setCount(count);

    String lang = "";
    if (!this.getString("lang").equals(""))
        lang = this.getString("lang");
    if (!lang.equals("all"))
        query.setLang(lang);

    // obtener desde donde quiero hacer la consulta

    if (getInt("notifications") == 1) {
        String where = "search_id = " + this.getId() + " AND favorite = 0";
        int nResult = DataFramework.getInstance().getEntityListCount("tweets", where);
        if (nResult > 0) {
            long mLastIdNotification = DataFramework.getInstance().getTopEntity("tweets", where, "date desc")
                    .getLong("tweet_id");
            query.setSinceId(mLastIdNotification);
        }
    }

    //query.setResultType(Query.POPULAR);

    return query;
}

From source file:com.twitter.TwitterCrawler.java

/**
 *
 * @return @throws TwitterException//from   w  ww  . ja  v  a2 s.  c o m
 * @throws InterruptedException returns the number of tweets
 */
public TwitterApiResponse stream() throws TwitterException, InterruptedException {
    //************************ Variables *************************
    TwitterApiResponse response = new TwitterApiResponse();
    long timeNow = System.currentTimeMillis();
    long afterInterval = timeNow + (interval * 1000);
    int countTweets = 0;
    //define the tweet id that the application should read relative to the IDs of Tweets it has already processed
    long maxID = 0;
    ArrayList<Status> tweets = new ArrayList<Status>();

    Query query = new Query(keywords);

    //************************ Action *************************
    //max 450 calls in 15 mins -->30/min, 5 calls/10 secs
    while (timeNow < afterInterval) {
        try {
            if (sinceID != 0) {
                query.setSinceId(sinceID);
            }

            if (maxID != 0) {
                query.setMaxId(maxID);
            }

            query.setResultType(Query.ResultType.recent);
            query.setCount(100);
            QueryResult result = twitter.search(query);
            tweets.addAll(result.getTweets());

            if (tweets.size() == 100) {
                countTweets += tweets.size();
                maxID = tweets.get(tweets.size() - 1).getId() - 1;
            } else {
                countTweets += tweets.size();
                break;
            }
            timeNow = System.currentTimeMillis();
        } catch (TwitterException te) {
            System.out.println("Couldn't connect: " + te);
        }
    }

    sinceID = tweets.get(0).getId();
    response.setCount(countTweets);
    response.setSinceID(sinceID);

    return response;
}

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  2s . 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:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationSearch.java

License:Open Source License

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