Example usage for twitter4j Paging Paging

List of usage examples for twitter4j Paging Paging

Introduction

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

Prototype

public Paging() 

Source Link

Usage

From source file:com.vodafone.twitter.service.TwitterService.java

License:Apache License

private int fetchHomeTimeline() {
    // retrieve the latest up to maxQueueMessages twt-msgs
    int count = 0, countProcessed = 0;
    if (twitter != null) {
        List<Status> statuses = null;
        try {//from   ww  w  . j a  v a 2s.  c o m
            int messageListSize = 0;

            Paging paging = new Paging();
            paging.setCount(maxQueueMessages);
            if (Config.LOGD)
                Log.i(LOGTAG, String.format("fetchHomeTimeline() twitter.getHomeTimeline() maxQueueMessages=%d",
                        maxQueueMessages));
            statuses = twitter.getHomeTimeline(paging);

            if (statuses != null) {
                count = statuses.size();

                for (Status status : statuses) {
                    if (processStatus(status))
                        countProcessed++;
                }

                synchronized (messageList) {
                    messageListSize = messageList.size();
                }
                if (Config.LOGD)
                    Log.i(LOGTAG,
                            String.format("fetchHomeTimeline() countProcessed=%d, messageList.size()=%d done",
                                    countProcessed, messageListSize));

                if (countProcessed > 0)
                    notifyClient();
            } else {
                if (Config.LOGD)
                    Log.i(LOGTAG, "fetchHomeTimeline() no statuses");
            }
        } catch (TwitterException twex) {
            Log.e(LOGTAG, "fetchHomeTimeline() failed to get twitter timeline: " + twex);
            errMsg = twex.getMessage();
        } catch (java.lang.IllegalStateException illstaex) {
            Log.e(LOGTAG, "fetchHomeTimeline() IllegalStateException illstaex=" + illstaex);
            errMsg = "IllegalStateException on .getHomeTimeline()";
        }
    }
    return count;
}

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 ww  w. j  ava  2 s. 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.vanita5.twittnuker.loader.support.Twitter4JActivitiesLoader.java

License:Open Source License

@Override
public final List<ParcelableActivity> loadInBackground() {
    if (mAccountIds == null)
        return Collections.emptyList();
    final File serializationFile = getSerializationFile();
    if (mIsFirstLoad && mUseCache && serializationFile != null) {
        final List<ParcelableActivity> cached = getCachedData(serializationFile);
        if (cached != null) {
            Collections.sort(cached);
            return new CopyOnWriteArrayList<ParcelableActivity>(cached);
        }//from w  ww .  j  av  a 2s.c om
    }
    final SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
            Context.MODE_PRIVATE);
    final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
    final List<ParcelableActivity> result = new ArrayList<ParcelableActivity>();
    for (final long accountId : mAccountIds) {
        final List<Activity> activities;
        try {
            final Paging paging = new Paging();
            paging.setCount(Math.min(100, loadItemLimit));
            activities = getActivities(getTwitter(accountId), paging);
        } catch (final TwitterException e) {
            e.printStackTrace();
            final List<ParcelableActivity> cached = getCachedData(serializationFile);
            if (cached == null)
                return Collections.emptyList();
            return new CopyOnWriteArrayList<ParcelableActivity>(cached);
        }
        if (activities == null)
            return new CopyOnWriteArrayList<ParcelableActivity>(mData);
        for (final Activity activity : activities) {
            result.add(new ParcelableActivity(activity, accountId));
        }
    }
    Collections.sort(result);
    saveCachedData(serializationFile, result);
    return new CopyOnWriteArrayList<ParcelableActivity>(result);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w  w  w. j  a v  a 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);
}

From source file:it.greenvulcano.gvesb.social.twitter.directcall.TwitterOperationGetUserTimeline.java

License:Open Source License

@Override
public void execute(SocialAdapterAccount account) throws SocialAdapterException {
    try {//  w  w w. j a  va2 s.com
        Twitter twitter = (Twitter) account.getProxyObject();
        Paging paging = new Paging();
        if ((sinceId != null) && !"".equals(sinceId)) {
            paging.setSinceId(Long.parseLong(sinceId));
        }
        if ((maxId != null) && !"".equals(maxId)) {
            paging.setMaxId(Long.parseLong(maxId));
        }
        if ((count != null) && !"".equals(count)) {
            paging.setCount(Integer.parseInt(count));
        }
        if ((userId == null) || "".equals(userId)) {
            dataUser = twitter.getScreenName();
            dataUserId = String.valueOf(twitter.getId());
            statusList = twitter.getUserTimeline(paging);
        } else {
            User user = null;
            try {
                long id = Long.parseLong(userId);
                user = twitter.showUser(id);
                statusList = twitter.getUserTimeline(id, paging);
            } catch (NumberFormatException exc) {
                user = twitter.showUser(userId);
                statusList = twitter.getUserTimeline(userId, paging);
            }
            dataUser = user.getScreenName();
            dataUserId = String.valueOf(user.getId());
        }
    } catch (NumberFormatException exc) {
        logger.error("Call to TwitterOperationGetUserTimeline failed. Check userId[" + userId + "], sinceId["
                + sinceId + "], maxId[" + maxId + "] and count[" + count + "] format.", exc);
        throw new SocialAdapterException("Call to TwitterOperationGetUserTimeline failed. Check followingId["
                + userId + "], sinceId[" + sinceId + "], maxId[" + maxId + "] and count[" + count + "] format.",
                exc);
    } catch (TwitterException exc) {
        logger.error("Call to TwitterOperationGetUserTimeline followingId[" + userId + "], sinceId[" + sinceId
                + "], maxId[" + maxId + "] and count[" + count + "] failed.", exc);
        throw new SocialAdapterException("Call to TwitterOperationGetUserTimeline followingId[" + userId
                + "], sinceId[" + sinceId + "], maxId[" + maxId + "] and count[" + count + "] failed.", exc);
    }
}

From source file:org.apache.nutch.protocol.http.api.HttpBase.java

License:Apache License

private HttpResponse getTwitterResponse(String url)
        throws MalformedURLException, TwitterException, UnsupportedEncodingException, CharacterCodingException {
    url = URLDecoder.decode(url, "UTF-8");
    Map<String, String> map = getQueryParams(url);
    String consumerKey = map.get("consumer_key");
    String consumerSecret = map.get("consumer_secret");
    String accessToken = map.get("oauth_key");
    String accessTokenSecret = map.get("oauth_secret");

    Twitter twitter = getTwitterInstance(consumerKey, consumerSecret, accessToken, accessTokenSecret);
    Paging paging = new Paging();
    paging.setCount(10);// ww  w .j av  a  2 s  .c  o m
    twitter4j.internal.http.HttpResponse twitterResponse;
    String result = null;
    if (!(url.indexOf("statuses/show.json") >= 0)) {
        String max_id = map.get("max_id");
        if (null != max_id)
            paging.setMaxId(Long.parseLong(max_id));
        ResponseList<Status> homeTimeline = twitter.getUserTimeline(paging);
        twitterResponse = homeTimeline.getResponse();
        result = twitterResponse.asJSONArray().toString();
    } else {
        String id = map.get("id");
        Long statusId = Long.parseLong(id);
        Status status = twitter.showStatus(statusId);
        twitterResponse = status.getResponse();
        result = twitterResponse.asJSONObject().toString();
    }

    u = new URL(twitterResponse.getRequestURL());
    code = twitterResponse.getStatusCode();
    ByteBuffer buffer = toByteBUffer(result);
    contents = buffer.array();
    contentType = "text/javascript";

    headers = new SpellCheckedMetadata();
    Map<String, List<String>> responseHeaders = twitterResponse.getResponseHeaderFields();
    for (String key : responseHeaders.keySet()) {
        List<String> values = responseHeaders.get(key);
        for (String value : values) {
            if (null == key)
                continue;
            if (null == value)
                continue;
            headers.add(key, value);
        }
    }
    return null;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   w w  w .j  a v  a2s. c o  m*/
public final List<ParcelableActivity> loadInBackground() {
    final File serializationFile = getSerializationFile();
    final List<ParcelableActivity> data = getData();
    if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) {
        final List<ParcelableActivity> cached = getCachedData(serializationFile);
        if (cached != null) {
            data.addAll(cached);
            if (mComparator != null) {
                Collections.sort(data, mComparator);
            } else {
                Collections.sort(data);
            }
            return new CopyOnWriteArrayList<>(data);
        }
    }
    final List<Activity> activities;
    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);
        }
        activities = new ArrayList<>();
        truncated = truncateActivities(getActivities(getTwitter(), paging), activities, mSinceId);
    } catch (final TwitterException e) {
        // mHandler.post(new ShowErrorRunnable(e));
        e.printStackTrace();
        return new CopyOnWriteArrayList<>(data);
    }
    final Pair<Long, Long> minId;
    if (activities.isEmpty()) {
        minId = new Pair<>(-1L, -1L);
    } else {
        final Activity minActivity = Collections.min(activities);
        minId = new Pair<>(minActivity.getMinPosition(), minActivity.getMaxPosition());
    }
    final boolean insertGap = minId.first > 0 && minId.second > 0 && activities.size() > 1 && !data.isEmpty()
            && !truncated;
    //        mHandler.post(CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountIds, activities)));
    for (final Activity activity : activities) {
        final long min = activity.getMinPosition(), max = activity.getMaxPosition();
        final boolean deleted = deleteStatus(data, max);
        final boolean isGap = minId.first == min && minId.second == max && insertGap && !deleted;
        data.add(new ParcelableActivity(activity, mAccountIds, isGap));
    }
    //        final ParcelableActivity[] array = data.toArray(new ParcelableActivity[data.size()]);
    //        for (int i = 0, size = array.length; i < size; i++) {
    //            final ParcelableActivity status = array[i];
    //            if (shouldFilterActivity(mDatabase, status) && !status.is_gap && i != size - 1) {
    //                deleteStatus(data, status.max_position);
    //            }
    //        }
    if (mComparator != null) {
        Collections.sort(data, mComparator);
    } else {
        Collections.sort(data);
    }
    saveCachedData(serializationFile, data);
    return new CopyOnWriteArrayList<>(data);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w w  w .  ja  va2  s . c  om
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);
            if (mComparator != null) {
                Collections.sort(data, mComparator);
            } else {
                Collections.sort(data);
            }
            return new CopyOnWriteArrayList<>(data);
        }
    }
    if (!isFromUser())
        return 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<>();
        final Twitter twitter = getTwitter();
        if (twitter == null) {
            throw new TwitterException("Account is null");
        }
        truncated = truncateStatuses(getStatuses(twitter, paging), statuses, mSinceId);
    } catch (final TwitterException e) {
        // mHandler.post(new ShowErrorRunnable(e));
        Log.w(LOGTAG, e);
        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;
    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));
    }
    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);
        }
    }
    if (mComparator != null) {
        Collections.sort(data, mComparator);
    } else {
        Collections.sort(data);
    }
    saveCachedData(serializationFile, data);
    return new CopyOnWriteArrayList<>(data);
}

From source file:org.getlantern.firetweet.util.TwitterWrapper.java

License:Open Source License

@NonNull
public static User showUserAlternative(final Twitter twitter, final long id, final String screenName)
        throws TwitterException {
    final String searchScreenName;
    if (screenName != null) {
        searchScreenName = screenName;//from w  w  w . j ava2s.  c  o  m
    } else if (id != -1) {
        searchScreenName = twitter.showFriendship(twitter.getId(), id).getTargetUserScreenName();
    } else
        throw new IllegalArgumentException();
    final Paging paging = new Paging();
    paging.count(1);
    if (id != -1) {
        final ResponseList<Status> timeline = twitter.getUserTimeline(id, paging);
        for (final Status status : timeline) {
            final User user = status.getUser();
            if (user.getId() == id)
                return user;
        }
    } else {
        final ResponseList<Status> timeline = twitter.getUserTimeline(screenName, paging);
        for (final Status status : timeline) {
            final User user = status.getUser();
            if (searchScreenName.equalsIgnoreCase(user.getScreenName()))
                return user;
        }
    }
    for (final User user : twitter.searchUsers(searchScreenName, 1)) {
        if (user.getId() == id || searchScreenName.equalsIgnoreCase(user.getScreenName()))
            return user;
    }
    throw new TwitterException("can't find user");
}

From source file:org.hornetq.integration.twitter.impl.IncomingTweetsHandler.java

License:Apache License

public void start() throws Exception {
    Binding b = postOffice.getBinding(new SimpleString(queueName));
    if (b == null) {
        throw new Exception(connectorName + ": queue " + queueName + " not found");
    }/*  w w  w  .ja va2 s  .c  o m*/

    paging = new Paging();
    TwitterFactory tf = new TwitterFactory();
    this.twitter = tf.getOAuthAuthorizedInstance(this.consumerKey, this.consumerSecret,
            new AccessToken(this.accessToken, this.accessTokenSecret));
    this.twitter.verifyCredentials();

    // getting latest ID
    this.paging.setCount(TwitterConstants.FIRST_ATTEMPT_PAGE_SIZE);

    // If I used annotations here, it won't compile under JDK 1.7
    ResponseList res = this.twitter.getHomeTimeline(paging);
    this.paging.setSinceId(((Status) res.get(0)).getId());
    HornetQTwitterLogger.LOGGER
            .debug(connectorName + " initialise(): got latest ID: " + this.paging.getSinceId());

    // TODO make page size configurable
    this.paging.setCount(TwitterConstants.DEFAULT_PAGE_SIZE);

    scheduledFuture = this.scheduledPool.scheduleWithFixedDelay(new TweetsRunnable(), intervalSeconds,
            intervalSeconds, TimeUnit.SECONDS);
    isStarted = true;
}