Example usage for twitter4j Paging setMaxId

List of usage examples for twitter4j Paging setMaxId

Introduction

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

Prototype

public void setMaxId(long maxId) 

Source Link

Usage

From source file:collector.TwitterCollector.java

public LinkedHashSet<Tweet> userSearchData(String userName, int maxResults) {

    LinkedHashSet<Tweet> out = new LinkedHashSet<>();
    Paging paging = new Paging(1, 180);
    int numberOfTweets = maxResults;//512;
    long lastID = Long.MAX_VALUE;
    ArrayList<Status> status = new ArrayList<>();
    while (status.size() < numberOfTweets) {
        if (numberOfTweets - status.size() > 180) {//100) {
            paging.setCount(180);//100);
        } else {/*from ww  w . jav  a  2s.  c  o  m*/
            paging.setCount(numberOfTweets - status.size());
        }
        try {
            List<Status> timeLine = twitter.getUserTimeline(userName, paging);
            status.addAll(timeLine);
            for (Status t : status) {
                if (t.getId() < lastID) {
                    lastID = t.getId();
                }
            }
        } catch (TwitterException ex) {
            System.err.println(ex.getMessage());
        }
        paging.setMaxId(lastID - 1);
    }

    //armazenar os atributos interessantes a analise dos tweets
    int qtdretweet = 0;
    for (Status sta : status) {
        String text = sta.getText();
        if (!sta.isRetweet() && !sta.isRetweeted() && !text.startsWith("RT")) { //&& !TweetMediaDetect.detect(text)) {
            TwitterUser user;
            user = new TwitterUser().addID(sta.getUser().getId()).addName(sta.getUser().getName())
                    .addLocation(sta.getUser().getLocation()).addDateSignin(sta.getUser().getCreatedAt())
                    .addCountTweets(sta.getUser().getStatusesCount())
                    .addCountFavorites(sta.getUser().getFavouritesCount())
                    .addCountFriends(sta.getUser().getFriendsCount())
                    .addCountFollowers(sta.getUser().getFollowersCount());
            Tweet tweet = new Tweet().addUser(user).addText(sta.getText()).addID(sta.getId())
                    .addDate(sta.getCreatedAt())
                    .addLatitude(sta.getGeoLocation() != null ? sta.getGeoLocation().getLatitude()
                            : Double.MAX_VALUE)
                    .addLongitude(sta.getGeoLocation() != null ? sta.getGeoLocation().getLongitude()
                            : Double.MAX_VALUE);
            out.add(tweet);
        } else {
            qtdretweet++;
        }
    }

    return out;
}

From source file:com.dwdesign.tweetings.loader.Twitter4JStatusLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   w  ww .j  ava  2s.  c  o  m
public SynchronizedStateSavedList<ParcelableStatus, Long> loadInBackground() {
    final SynchronizedStateSavedList<ParcelableStatus, Long> data = getData();
    List<Status> statuses = null;

    final Context context = getContext();
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final int load_item_limit = prefs.getInt(PREFERENCE_KEY_LOAD_ITEM_LIMIT,
            PREFERENCE_DEFAULT_LOAD_ITEM_LIMIT);
    try {
        final Paging paging = new Paging();
        paging.setCount(load_item_limit);
        if (mMaxId > 0) {
            paging.setMaxId(mMaxId);
        }
        if (mSinceId > 0) {
            paging.setSinceId(mSinceId);
        }
        statuses = getStatuses(paging);
    } catch (final TwitterException e) {
        e.printStackTrace();
    }
    if (statuses != null) {
        final boolean insert_gap = load_item_limit == statuses.size() && data.size() > 0;
        final Status min_status = statuses.size() > 0 ? Collections.min(statuses) : null;
        final long min_status_id = min_status != null ? min_status.getId() : -1;
        for (final Status status : statuses) {
            final long id = status.getId();
            deleteStatus(id);
            data.add(new ParcelableStatus(status, mAccountId,
                    min_status_id > 0 && min_status_id == id && insert_gap,
                    mInlineImagePreviewDisplayOption == INLINE_IMAGE_PREVIEW_DISPLAY_OPTION_CODE_LARGE_HIGH));
        }
    }
    try {
        final List<ParcelableStatus> statuses_to_remove = new ArrayList<ParcelableStatus>();
        for (final ParcelableStatus status : data) {
            if (isFiltered(context, status.screen_name, status.source, status.text_plain) && !status.is_gap) {
                statuses_to_remove.add(status);
            }
        }
        data.removeAll(statuses_to_remove);
        Collections.sort(data);
    } catch (final ConcurrentModificationException e) {
        Log.w(LOGTAG, e);
    }
    return data;
}

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

License:Apache License

@Override
public BaseResponse loadInBackground() {

    try {/*w w  w .j a  v a 2 s  . c om*/

        LoadMoreTweetDownResponse response = new LoadMoreTweetDownResponse();

        PreferenceManager.setDefaultValues(getContext(), R.xml.preferences, false);
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getContext());
        int maxDownloadTweet = Integer.parseInt(pref.getString("prf_n_max_download", "60"));
        if (maxDownloadTweet <= 0)
            maxDownloadTweet = 60;

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

        Twitter twitter = ConnectionManager.getInstance().getTwitter(request.getUserId());

        Paging p = new Paging();
        p.setCount(maxDownloadTweet);
        p.setSinceId(request.getSinceId());
        p.setMaxId(request.getMaxId());

        ResponseList<Status> statii = null;

        try {
            statii = twitter.getHomeTimeline(p);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }

        boolean breakTimeline = false;

        if (statii != null && statii.size() >= maxDownloadTweet - 10) {
            p = new Paging(1, 10);
            p.setSinceId(request.getSinceId());
            p.setMaxId(statii.get(statii.size() - 1).getId());
            if (twitter.getHomeTimeline().size() > 0) {
                breakTimeline = true;
                response.setHasMoreTweets(true);
            }
        }

        if (statii != null) {

            if (statii.size() > 0) {

                try {
                    DataFramework.getInstance().open(getContext(), Utils.packageName);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                List<InfoTweet> tweets = new ArrayList<InfoTweet>();
                for (Status status : statii) {
                    tweets.add(new InfoTweet(status));
                }
                response.setTweets(tweets);

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

                try {
                    boolean isFirst = true;
                    for (int i = statii.size() - 1; i >= 0; i--) {
                        User u = statii.get(i).getUser();
                        if (u != null) {
                            ContentValues args = new ContentValues();
                            args.put(DataFramework.KEY_ID, "" + nextId);
                            args.put("type_id", TweetTopicsUtils.TWEET_TYPE_TIMELINE);
                            args.put("user_tt_id", "" + request.getUserId());
                            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("" + statii.get(i).getId()));
                            args.put("source", statii.get(i).getSource());
                            args.put("to_username", statii.get(i).getInReplyToScreenName());
                            args.put("to_user_id", "" + statii.get(i).getInReplyToUserId());
                            args.put("date", String.valueOf(statii.get(i).getCreatedAt().getTime()));
                            if (statii.get(i).getRetweetedStatus() != null) {
                                args.put("is_retweet", 1);
                                args.put("retweet_url_avatar", statii.get(i).getRetweetedStatus().getUser()
                                        .getProfileImageURL().toString());
                                args.put("retweet_username",
                                        statii.get(i).getRetweetedStatus().getUser().getScreenName());
                                args.put("retweet_source", statii.get(i).getRetweetedStatus().getSource());
                                String t = Utils.getTwitLoger(statii.get(i).getRetweetedStatus());
                                if (t.equals("")) {
                                    args.put("text", statii.get(i).getRetweetedStatus().getText());
                                    args.put("text_urls",
                                            Utils.getTextURLs(statii.get(i).getRetweetedStatus()));
                                } else {
                                    args.put("text", t);
                                }
                                args.put("is_favorite", 0);
                            } else {
                                String t = Utils.getTwitLoger(statii.get(i));
                                if (t.equals("")) {
                                    args.put("text", statii.get(i).getText());
                                    args.put("text_urls", Utils.getTextURLs(statii.get(i)));
                                } else {
                                    args.put("text", t);
                                }

                                if (statii.get(i).isFavorited()) {
                                    args.put("is_favorite", 1);
                                }
                            }

                            if (statii.get(i).getGeoLocation() != null) {
                                args.put("latitude", statii.get(i).getGeoLocation().getLatitude());
                                args.put("longitude", statii.get(i).getGeoLocation().getLongitude());
                            }
                            args.put("reply_tweet_id", statii.get(i).getInReplyToStatusId());

                            if (breakTimeline && isFirst)
                                args.put("has_more_tweets_down", 1);

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

                            nextId++;

                            if (isFirst)
                                isFirst = false;
                        }

                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                }

                DataFramework.getInstance().close();

            }

        }

        return response;

    } catch (TwitterException twitterException) {
        twitterException.printStackTrace();
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setError(twitterException, twitterException.getMessage());
        return errorResponse;
    } catch (Exception exception) {
        exception.printStackTrace();
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setError(exception, exception.getMessage());
        return errorResponse;
    }

}

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

License:Apache License

public InfoSaveTweets saveTweets(Context context, Twitter twitter) {

    InfoSaveTweets out = new InfoSaveTweets();

    try {/*from   w  w  w.ja v  a 2 s  .  c  om*/
        String where = "type_id = " + tweet_type + " AND user_tt_id=" + getId();

        int nResult = DataFramework.getInstance().getEntityListCount("tweets_user", where);
        if (nResult > 0)
            mLastIdNotification = DataFramework.getInstance().getTopEntity("tweets_user", where, "date desc")
                    .getLong("tweet_id");

        boolean breakTimeline = false;

        PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

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

        ResponseList<twitter4j.Status> statii = null;
        ResponseList<twitter4j.DirectMessage> directs = null;

        if (mLastIdNotification > 0) {
            if (tweet_type == TweetTopicsUtils.TWEET_TYPE_TIMELINE) {

                Paging p = new Paging(1, maxDownloadTweet);
                p.setSinceId(mLastIdNotification);

                try {
                    statii = twitter.getHomeTimeline(p);
                } catch (OutOfMemoryError e) {
                    e.printStackTrace();
                }

                if (statii != null && statii.size() >= maxDownloadTweet - 10) {
                    p = new Paging(1, 10);
                    p.setSinceId(mLastIdNotification);
                    p.setMaxId(statii.get(statii.size() - 1).getId());
                    if (twitter.getHomeTimeline().size() > 0) {
                        breakTimeline = true;
                    }
                }

            } else if (tweet_type == TweetTopicsUtils.TWEET_TYPE_MENTIONS) {
                Paging p = new Paging();
                p.setCount(100);
                p.setSinceId(mLastIdNotification);
                statii = twitter.getMentionsTimeline(p);
            } else if (tweet_type == TweetTopicsUtils.TWEET_TYPE_DIRECTMESSAGES) {
                Paging p = new Paging();
                p.setCount(100);
                p.setSinceId(mLastIdNotification);
                directs = twitter.getDirectMessages(p);
            } else if (tweet_type == TweetTopicsUtils.TWEET_TYPE_SENT_DIRECTMESSAGES) {
                Paging p = new Paging();
                p.setCount(100);
                p.setSinceId(mLastIdNotification);
                directs = twitter.getSentDirectMessages(p);
            }
        } else {
            try {
                Log.d(Utils.TAG, "Primera carga de " + getTypeText());
                if (tweet_type == TweetTopicsUtils.TWEET_TYPE_TIMELINE) {
                    statii = twitter.getHomeTimeline(new Paging(1, 40));
                } else if (tweet_type == TweetTopicsUtils.TWEET_TYPE_MENTIONS) {
                    statii = twitter.getMentionsTimeline(new Paging(1, 40));
                } else if (tweet_type == TweetTopicsUtils.TWEET_TYPE_DIRECTMESSAGES) {
                    directs = twitter.getDirectMessages();
                } else if (tweet_type == TweetTopicsUtils.TWEET_TYPE_SENT_DIRECTMESSAGES) {
                    directs = twitter.getSentDirectMessages();
                }
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
        }

        // guardar statii

        if (statii != null) {

            if (statii.size() > 0) {
                out.setNewMessages(statii.size());
                out.setNewerId(statii.get(0).getId());
                out.setOlderId(statii.get(statii.size() - 1).getId());

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

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

                DataFramework.getInstance().getDB().beginTransaction();

                try {
                    boolean isFirst = true;
                    for (int i = statii.size() - 1; i >= 0; i--) {
                        User u = statii.get(i).getUser();
                        if (u != null) {
                            ContentValues args = new ContentValues();
                            args.put(DataFramework.KEY_ID, "" + nextId);
                            args.put("type_id", tweet_type);
                            args.put("user_tt_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("" + statii.get(i).getId()));
                            args.put("source", statii.get(i).getSource());
                            args.put("to_username", statii.get(i).getInReplyToScreenName());
                            args.put("to_user_id", "" + statii.get(i).getInReplyToUserId());
                            args.put("date", String.valueOf(statii.get(i).getCreatedAt().getTime()));
                            if (statii.get(i).getRetweetedStatus() != null) {
                                args.put("is_retweet", 1);
                                args.put("retweet_url_avatar", statii.get(i).getRetweetedStatus().getUser()
                                        .getProfileImageURL().toString());
                                args.put("retweet_username",
                                        statii.get(i).getRetweetedStatus().getUser().getScreenName());
                                args.put("retweet_source", statii.get(i).getRetweetedStatus().getSource());
                                String t = Utils.getTwitLoger(statii.get(i).getRetweetedStatus());
                                if (t.equals("")) {
                                    args.put("text", statii.get(i).getRetweetedStatus().getText());
                                    args.put("text_urls",
                                            Utils.getTextURLs(statii.get(i).getRetweetedStatus()));
                                } else {
                                    args.put("text", t);
                                }
                                args.put("is_favorite", 0);
                            } else {
                                String t = Utils.getTwitLoger(statii.get(i));
                                if (t.equals("")) {
                                    args.put("text", statii.get(i).getText());
                                    args.put("text_urls", Utils.getTextURLs(statii.get(i)));
                                } else {
                                    args.put("text", t);
                                }

                                if (statii.get(i).isFavorited()) {
                                    args.put("is_favorite", 1);
                                }
                            }

                            if (statii.get(i).getGeoLocation() != null) {
                                args.put("latitude", statii.get(i).getGeoLocation().getLatitude());
                                args.put("longitude", statii.get(i).getGeoLocation().getLongitude());
                            }
                            args.put("reply_tweet_id", statii.get(i).getInReplyToStatusId());

                            if (breakTimeline && isFirst)
                                args.put("has_more_tweets_down", 1);

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

                            out.addId(nextId);

                            nextId++;

                            if (isFirst)
                                isFirst = false;
                        }

                    }

                    // finalizar

                    int total = nResult + statii.size();

                    if (total > Utils.MAX_ROW_BYSEARCH && getValueNewCount() < Utils.MAX_ROW_BYSEARCH
                            || total > Utils.MAX_ROW_BYSEARCH_FORCE) {
                        try {
                            Log.d(Utils.TAG, "Limpiando base de datos de " + getTypeText() + " actualmente "
                                    + total + " registros");
                            String date = DataFramework.getInstance()
                                    .getEntityList("tweets_user",
                                            "type_id=" + tweet_type + " and user_tt_id=" + getId(), "date desc")
                                    .get(Utils.MAX_ROW_BYSEARCH).getString("date");
                            String sqldelete = "DELETE FROM tweets_user WHERE type_id=" + tweet_type
                                    + " and user_tt_id=" + getId() + " AND date  < '" + date + "'";
                            DataFramework.getInstance().getDB().execSQL(sqldelete);
                        } catch (OutOfMemoryError e) {
                        }
                    }

                    DataFramework.getInstance().getDB().setTransactionSuccessful();

                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    DataFramework.getInstance().getDB().endTransaction();
                }

            }

        }

        // guardar directs

        if (directs != null) {
            if (directs.size() > 0) {
                out.setNewMessages(directs.size());
                out.setNewerId(directs.get(0).getId());
                out.setOlderId(directs.get(directs.size() - 1).getId());

                Log.d(Utils.TAG, directs.size() + " mensajes directos a " + getString("name"));

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

                DataFramework.getInstance().getDB().beginTransaction();

                try {
                    for (int i = directs.size() - 1; i >= 0; i--) {
                        User u = directs.get(i).getSender();
                        if (u != null) {
                            ContentValues args = new ContentValues();
                            args.put(DataFramework.KEY_ID, "" + nextId);
                            args.put("type_id", tweet_type);
                            args.put("user_tt_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("" + directs.get(i).getId()));
                            args.put("source", "");
                            args.put("to_username", directs.get(i).getRecipientScreenName());
                            args.put("to_user_id", "" + directs.get(i).getRecipientId());
                            args.put("date", String.valueOf(directs.get(i).getCreatedAt().getTime()));
                            args.put("text", directs.get(i).getText());

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

                            out.addId(nextId);

                            Log.d(Utils.TAG,
                                    "getRecipientScreenName: " + directs.get(i).getRecipientScreenName());

                            nextId++;
                        }

                    }

                    // finalizar

                    int total = nResult + directs.size();

                    if (total > Utils.MAX_ROW_BYSEARCH && getValueNewCount() < Utils.MAX_ROW_BYSEARCH) {
                        Log.d(Utils.TAG, "Limpiando base de datos de " + getTypeText() + " actualmente " + total
                                + " registros");
                        String date = DataFramework.getInstance()
                                .getEntityList("tweets_user",
                                        "type_id=" + tweet_type + " and user_tt_id=" + getId(), "date desc")
                                .get(Utils.MAX_ROW_BYSEARCH).getString("date");
                        String sqldelete = "DELETE FROM tweets_user WHERE type_id=" + tweet_type
                                + " and user_tt_id=" + getId() + " AND date  < '" + date + "'";
                        DataFramework.getInstance().getDB().execSQL(sqldelete);
                    }

                    DataFramework.getInstance().getDB().setTransactionSuccessful();

                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    DataFramework.getInstance().getDB().endTransaction();
                }

            }

        }
    } 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.marpies.ane.twitter.functions.GetDirectMessagesFunction.java

License:Apache License

private Paging getPaging(int count, long sinceID, long maxID) {
    Paging paging = null;
    if (count != 20) {
        paging = new Paging();
        paging.setCount(count);/* w ww .j  a  v a2  s. c  om*/
    }
    if (sinceID >= 0) {
        paging = (paging == null) ? new Paging() : paging;
        paging.setSinceId(sinceID);
    }
    if (maxID >= 0) {
        paging = (paging == null) ? new Paging() : paging;
        paging.setMaxId(maxID);
    }
    return paging;
}

From source file:com.marpies.ane.twitter.functions.GetSentDirectMessagesFunction.java

License:Apache License

private Paging getPaging(int count, long sinceID, long maxID, int page) {
    Paging paging = null;
    if (count != 20) {
        paging = new Paging();
        paging.setCount(count);/* www.  jav  a  2s . com*/
    }
    if (sinceID >= 0) {
        paging = (paging == null) ? new Paging() : paging;
        paging.setSinceId(sinceID);
    }
    if (maxID >= 0) {
        paging = (paging == null) ? new Paging() : paging;
        paging.setMaxId(maxID);
    }
    if (page > 0) {
        paging = (paging == null) ? new Paging() : paging;
        paging.setPage(page);
    }
    return paging;
}

From source file:com.TweetExtractor.java

/**
 * */*  ww  w  .j a  v  a  2  s  . c om*/
 *
 */
public ArrayList<Tweet> retrieveTweets(String searchWord) {

    Paging paging;

    // set the lowest value of the tweet ID initially to one less than Long.MAX_VALUE
    long min_id = Long.MAX_VALUE - 1;
    int count = 0;
    int index = 0;
    boolean maxValueReached = false;
    userToSearch = searchWord;

    while (true) {
        try {

            //count = tweetList.size();
            // paging tweets at a rate of 100 per page
            paging = new Paging(1, 100);

            // if this is not the first iteration set the new min_id value for the page
            if (count != 0) {

                paging.setMaxId(min_id - 1);
            }

            // get a page of the tweet timeline with tweets with ids less than the min_id value
            List<Status> tweetTempList = twitterApp.getUserTimeline(userToSearch, paging);

            // iterate the results and add to tweetList
            for (Status s : tweetTempList) {
                if (count == maxTweets) {
                    maxValueReached = true;
                    break;
                }
                count++;
                Tweet tweet = new Tweet(s.getId(), s.getCreatedAt(), s.getText());
                tweetList.add(tweet);

                // set the value for the min value for the next iteration
                if (s.getId() < min_id) {
                    min_id = s.getId();
                }
            }

            // if the results for this iteration is zero, means we have reached the API limit or we have extracted the maximum
            // possible, so break
            if (tweetTempList.size() == 0 || maxValueReached) {
                return tweetList;
                // break;
            }

        } catch (TwitterException e) {
            e.printStackTrace();
            break;
        } catch (Exception e) {
            e.printStackTrace();
            break;
        }
    }
    return tweetList;

}

From source file:DataCollections.UserTimeLineCollection.java

public void collect_InsertTimeLineOfUser(User_dbo user) throws InterruptedException {

    Paging p = new Paging();
    int count = 20;
    p.setCount(count);//from   w w  w  .ja v  a  2s  . c o  m
    long max_id, since_id;
    int totaltweets = 0;
    long timestamp = -1;
    int nooftweets = 0;
    if (user.values[User_dbo.map.get("max_id")].used) {
        max_id = user.values[User_dbo.map.get("max_id")].lnumber;
        p.setMaxId(max_id);
    } else {
        max_id = -1;
    }
    if (user.values[User_dbo.map.get("since_id")].used) {
        since_id = user.values[User_dbo.map.get("since_id")].lnumber;
    } else {
        since_id = -1;
    }

    ResponseList<Status> statuses = null;
    boolean available = true;
    while (available) {

        try {
            LogPrinter.printLog("Retrieving some more tweets....");
            statuses = timelinesres.getUserTimeline(user.values[User_dbo.map.get("user_id")].lnumber, p);
        } catch (Exception e) {
            e.printStackTrace();
            TwitterException te = (TwitterException) e;
            if (te.exceededRateLimitation()) {
                LogPrinter.printLog("Rate Limited Reached.. Sleeping.. for ms " + 900 * 1000);
                Thread.sleep(900 * 1000 + 500);
            } else {

                return;
            }

        }
        totaltweets += statuses.size();
        if (statuses.isEmpty()) {
            LogPrinter.printLog("All tweets are retrieved....");
            available = false;
            continue;

        }
        ListIterator li = statuses.listIterator();
        while (li.hasNext()) {
            Status s = (Status) li.next();
            if (since_id < 0 && nooftweets == 0) {
                since_id = s.getId();
            }
            Tweet_dbo tweet = tweethelper.convertStatusToTweet_dbo(s);
            Tweet_dbo[] currentweets = TweetsTable
                    .select(" tweet_id = " + tweet.values[Tweet_dbo.map.get("tweet_id")].lnumber, 0, 2);
            if (currentweets.length == 0) {
                tweet.values[Tweet_dbo.map.get("processed")].setValue("true");
                tweet.values[Tweet_dbo.map.get("f_usertimeline")].setValue("true");
                TweetsTable.insert(tweet);
                useredgecollections.extract_InsertUsers_EdgesFromTweet(s);
                nooftweets++;
                LogPrinter.printLog("Inserting a new tweet.." + nooftweets);
            } else {
                LogPrinter.printLog("This tweet already exists in the database...");
            }
        }
        if (!statuses.isEmpty()) {
            max_id = ((Status) statuses.get(statuses.size() - 1)).getId();
            p.setMaxId(max_id);
        }
        if (totaltweets > 250) {
            LogPrinter.printLog("No of tweets collected reached goal....");
            available = false;
            continue;

        }

    }
    LogPrinter.printLog(String.valueOf(max_id));
    LogPrinter.printLog(String.valueOf(since_id));
    if (since_id > 0 && max_id > 0) {
        boolean selected[] = new boolean[User_dbo.nooffields];
        selected[User_dbo.map.get("max_id")] = true;
        selected[User_dbo.map.get("since_id")] = true;
        selected[User_dbo.map.get("utimeline_processed")] = true;
        user.values[User_dbo.map.get("max_id")].setValue(String.valueOf(max_id));
        user.values[User_dbo.map.get("since_id")].setValue(String.valueOf(since_id));
        user.values[User_dbo.map.get("utimeline_processed")].setValue("true");
        UsersTable.update(user, selected, " user_id =  " + user.values[User_dbo.map.get("user_id")].lnumber);
    }

}

From source file:de.jetwick.tw.TwitterSearch.java

License:Apache License

/**
 * This method only returns up to 800 statuses, including retweets.
 *//*from w  w  w  . j  a va 2  s  . co  m*/
public long getHomeTimeline(Collection<JTweet> result, int tweets, long lastId) throws TwitterException {
    if (lastId <= 0)
        lastId = 1;

    Map<String, JUser> userMap = new LinkedHashMap<String, JUser>();
    int hitsPerPage = 100;
    long maxId = lastId;
    long sinceId = lastId;
    int maxPages = tweets / hitsPerPage + 1;

    END_PAGINATION: for (int page = 0; page < maxPages; page++) {
        Paging paging = new Paging(page + 1, tweets, sinceId);
        // avoid that more recent results disturb our paging!
        if (page > 0)
            paging.setMaxId(maxId);

        Collection<Status> tmp = twitter.getHomeTimeline(paging);
        rateLimit--;
        for (Status st : tmp) {
            // determine maxId in the first page
            if (page == 0 && maxId < st.getId())
                maxId = st.getId();

            if (st.getId() < sinceId)
                break END_PAGINATION;

            Tweet tw = toTweet(st);
            String userName = tw.getFromUser().toLowerCase();
            JUser user = userMap.get(userName);
            if (user == null) {
                user = new JUser(st.getUser()).init(tw);
                userMap.put(userName, user);
            }

            result.add(new JTweet(tw, user));
        }

        // sinceId could force us to leave earlier than defined by maxPages
        if (tmp.size() < hitsPerPage)
            break;
    }

    return maxId;
}

From source file:de.vanita5.twittnuker.loader.support.Twitter4JStatusesLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   w w w .ja va  2 s  .co  m
public final List<ParcelableStatus> loadInBackground() {
    final File serializationFile = getSerializationFile();
    final List<ParcelableStatus> data = getData();
    if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) {
        final List<ParcelableStatus> cached = getCachedData(serializationFile);
        if (cached != null) {
            data.addAll(cached);
            Collections.sort(data);
            return new CopyOnWriteArrayList<>(data);
        }
    }
    final List<Status> statuses;
    final boolean truncated;
    final Context context = getContext();
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
    try {
        final Paging paging = new Paging();
        paging.setCount(loadItemLimit);
        if (mMaxId > 0) {
            paging.setMaxId(mMaxId);
        }
        if (mSinceId > 0) {
            paging.setSinceId(mSinceId - 1);
        }
        statuses = new ArrayList<>();
        truncated = truncateStatuses(getStatuses(getTwitter(), paging), statuses, mSinceId);
    } catch (final TwitterException e) {
        // mHandler.post(new ShowErrorRunnable(e));
        e.printStackTrace();
        return new CopyOnWriteArrayList<>(data);
    }
    final long minStatusId = statuses.isEmpty() ? -1 : Collections.min(statuses).getId();
    final boolean insertGap = minStatusId > 0 && statuses.size() > 1 && !data.isEmpty() && !truncated;
    mHandler.post(CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountId, statuses)));
    for (final Status status : statuses) {
        final long id = status.getId();
        final boolean deleted = deleteStatus(data, id);
        data.add(new ParcelableStatus(status, mAccountId, minStatusId == id && insertGap && !deleted));
    }
    Collections.sort(data);
    final ParcelableStatus[] array = data.toArray(new ParcelableStatus[data.size()]);
    for (int i = 0, size = array.length; i < size; i++) {
        final ParcelableStatus status = array[i];
        if (shouldFilterStatus(mDatabase, status) && !status.is_gap && i != size - 1) {
            deleteStatus(data, status.id);
        }
    }
    saveCachedData(serializationFile, data);
    return new CopyOnWriteArrayList<>(data);
}