Example usage for twitter4j Paging sinceId

List of usage examples for twitter4j Paging sinceId

Introduction

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

Prototype

long sinceId

To view the source code for twitter4j Paging sinceId.

Click Source Link

Usage

From source file:com.daiv.android.twitter.services.CatchupPull.java

License:Apache License

@Override
public void onHandleIntent(Intent intent) {
    if (CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning
            || !MainActivity.canSwitch) {
        return;/*  ww  w  .j ava  2  s  .  c o m*/
    }
    CatchupPull.isRunning = true;

    Log.v("Test_pull", "catchup pull started");

    sharedPrefs = getSharedPreferences("com.daiv.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

    final Context context = getApplicationContext();

    int unreadNow = sharedPrefs.getInt("pull_unread", 0);

    // stop it just in case
    context.sendBroadcast(new Intent("com.daiv.android.twitter.STOP_PUSH_SERVICE"));

    AppSettings settings = AppSettings.getInstance(context);

    if (settings.liveStreaming) {
        Log.v("Test_pull", "into the try for catchup service");
        Twitter twitter = Utils.getTwitter(context, settings);

        HomeDataSource dataSource = HomeDataSource.getInstance(context);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        List<Status> statuses = new ArrayList<Status>();

        boolean foundStatus = false;

        Paging paging = new Paging(1, 200);

        long[] lastId;
        long id;

        try {
            lastId = dataSource.getLastIds(currentAccount);
            id = lastId[0];
        } catch (Exception e) {
            context.startService(new Intent(context, TestPullNotificationService.class));
            CatchupPull.isRunning = false;
            return;
        }

        try {
            paging.setSinceId(id);
        } catch (Exception e) {
            paging.setSinceId(1l);
        }

        for (int i = 0; i < settings.maxTweetsRefresh; i++) {
            try {
                if (!foundStatus) {
                    paging.setPage(i + 1);
                    List<Status> list = twitter.getHomeTimeline(paging);

                    statuses.addAll(list);

                    if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
                        Log.v("Test_inserting", "found status");
                        foundStatus = true;
                    } else {
                        Log.v("Test_inserting", "haven't found status");
                        foundStatus = false;
                    }

                }
            } catch (Exception e) {
                // the page doesn't exist
                foundStatus = true;
                e.printStackTrace();
            } catch (OutOfMemoryError o) {
                // don't know why...
                o.printStackTrace();
            }
        }

        Log.v("Test_pull", "got statuses, new = " + statuses.size());

        // hash set to remove duplicates I guess
        HashSet hs = new HashSet();
        hs.addAll(statuses);
        statuses.clear();
        statuses.addAll(hs);

        Log.v("Test_inserting", "tweets after hashset: " + statuses.size());

        lastId = dataSource.getLastIds(currentAccount);

        int inserted = dataSource.insertTweets(statuses, currentAccount, lastId);

        if (inserted > 0 && statuses.size() > 0) {
            sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId())
                    .commit();
            unreadNow += statuses.size();
        }

        if (settings.preCacheImages) {
            // delay it 15 seconds so that we can finish checking mentions first
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startService(new Intent(context, PreCacheService.class));
                }
            }, 15000);
        }

        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
    }

    try {
        Twitter twitter = Utils.getTwitter(context, settings);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        User user = twitter.verifyCredentials();
        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);

        long[] lastId = dataSource.getLastIds(currentAccount);
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId[0] > 0) {
            paging.sinceId(lastId[0]);
        }

        List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);

        int numNew = dataSource.insertTweets(statuses, currentAccount);

        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();

        if (settings.notifications && settings.mentionsNot && numNew > 0) {
            NotificationUtils.refreshNotification(context);
        }

    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }

    sharedPrefs.edit().putInt("pull_unread", unreadNow).commit();
    context.startService(new Intent(context, TestPullNotificationService.class));

    context.sendBroadcast(new Intent("com.daiv.android.Test.UPDATE_WIDGET"));
    getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);

    Log.v("Test_pull", "finished with the catchup service");

    CatchupPull.isRunning = false;
}

From source file:com.daiv.android.twitter.services.MentionsRefreshService.java

License:Apache License

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.daiv.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);

    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;/*from  w ww  . j  a  va  2s.c om*/
    }

    try {
        Twitter twitter = Utils.getTwitter(context, settings);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);

        long[] lastId = dataSource.getLastIds(currentAccount);
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId[0] > 0) {
            paging.sinceId(lastId[0]);
        }

        List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);

        int inserted = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);

        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();

        if (settings.notifications && settings.mentionsNot && inserted > 0) {
            if (intent.getBooleanExtra("from_launcher", false)) {
                NotificationUtils.refreshNotification(context, true);
            } else {
                NotificationUtils.refreshNotification(context);
            }
        }

        if (settings.syncSecondMentions) {
            startService(new Intent(context, SecondMentionsRefreshService.class));
        }

    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}

From source file:com.daiv.android.twitter.services.SecondMentionsRefreshService.java

License:Apache License

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.daiv.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);

    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;//  w  ww .  ja  va2  s.c  o m
    }

    boolean update = false;
    int numberNew = 0;

    try {
        Twitter twitter = Utils.getSecondTwitter(context);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        if (currentAccount == 1) {
            currentAccount = 2;
        } else {
            currentAccount = 1;
        }

        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);

        long lastId = dataSource.getLastIds(currentAccount)[0];
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId > 0) {
            paging.sinceId(lastId);
        }

        List<Status> statuses = twitter.getMentionsTimeline(paging);

        numberNew = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);

        if (numberNew > 0) {
            sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();

            if (settings.notifications && settings.mentionsNot) {
                NotificationUtils.notifySecondMentions(context, currentAccount);
            }

            sendBroadcast(new Intent("com.daiv.android.twitter.REFRESH_SECOND_MENTIONS"));
        }

    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}

From source file:com.joshlong.esb.springintegration.modules.social.twitter.TwitterMessageSource.java

License:Apache License

public Message<Tweet> receive() {
    Assert.state(this.twitterMessageType != null, "the twitterMessageType can't be null!");
    Assert.state(cachedStatuses != null, "the cachedStatuses can't be null!");

    if (cachedStatuses.peek() == null) {
        Paging paging = new Paging();
        paging.setCount(getPagingCount());

        if (-1 != lastStatusIdRetreived) {
            paging.sinceId(lastStatusIdRetreived);
        }//from  w ww  .  j a  va 2s .  co m

        try {
            List<Status> statuses = new ArrayList<Status>();

            switch (getTwitterMessageType()) {
            case DM:
                throw new UnsupportedOperationException("we don't support receiving direct mentions yet!");

            case FRIENDS:
                statuses = twitter.getFriendsTimeline(paging);

                break;

            case MENTIONS:
                statuses = twitter.getMentions(paging);

                break;
            }

            if (cachedStatuses.peek() == null) {
                for (Status status : statuses) {
                    this.cachedStatuses.add(buildTweetFromStatus(status));
                }
            }
        } catch (TwitterException e) {
            logger.info(ExceptionUtils.getFullStackTrace(e));
            throw new RuntimeException(e);
        }
    }

    if (cachedStatuses.peek() != null) {
        // size() == 0 would be more obvious a test, but size() isn't constant time
        Tweet cachedStatus = cachedStatuses.poll();
        lastStatusIdRetreived = cachedStatus.getTweetId();

        return MessageBuilder.withPayload(cachedStatus).build();
    }

    return null;
}

From source file:com.klinker.android.twitter.services.CatchupPull.java

License:Apache License

@Override
public void onHandleIntent(Intent intent) {
    if (CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning
            || !MainActivity.canSwitch) {
        return;/*ww w.  ja  v a2  s. co m*/
    }
    CatchupPull.isRunning = true;

    Log.v("talon_pull", "catchup pull started");

    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

    final Context context = getApplicationContext();

    int unreadNow = sharedPrefs.getInt("pull_unread", 0);

    // stop it just in case
    context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));

    AppSettings settings = AppSettings.getInstance(context);

    if (settings.liveStreaming) {
        Log.v("talon_pull", "into the try for catchup service");
        Twitter twitter = Utils.getTwitter(context, settings);

        HomeDataSource dataSource = HomeDataSource.getInstance(context);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        List<Status> statuses = new ArrayList<Status>();

        boolean foundStatus = false;

        Paging paging = new Paging(1, 200);

        long[] lastId;
        long id;

        try {
            lastId = dataSource.getLastIds(currentAccount);
            id = lastId[0];
        } catch (Exception e) {
            context.startService(new Intent(context, TalonPullNotificationService.class));
            CatchupPull.isRunning = false;
            return;
        }

        try {
            paging.setSinceId(id);
        } catch (Exception e) {
            paging.setSinceId(1l);
        }

        for (int i = 0; i < settings.maxTweetsRefresh; i++) {
            try {
                if (!foundStatus) {
                    paging.setPage(i + 1);
                    List<Status> list = twitter.getHomeTimeline(paging);

                    statuses.addAll(list);

                    if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
                        Log.v("talon_inserting", "found status");
                        foundStatus = true;
                    } else {
                        Log.v("talon_inserting", "haven't found status");
                        foundStatus = false;
                    }

                }
            } catch (Exception e) {
                // the page doesn't exist
                foundStatus = true;
                e.printStackTrace();
            } catch (OutOfMemoryError o) {
                // don't know why...
                o.printStackTrace();
            }
        }

        Log.v("talon_pull", "got statuses, new = " + statuses.size());

        // hash set to remove duplicates I guess
        HashSet hs = new HashSet();
        hs.addAll(statuses);
        statuses.clear();
        statuses.addAll(hs);

        Log.v("talon_inserting", "tweets after hashset: " + statuses.size());

        lastId = dataSource.getLastIds(currentAccount);

        int inserted = dataSource.insertTweets(statuses, currentAccount, lastId);

        if (inserted > 0 && statuses.size() > 0) {
            sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId())
                    .commit();
            unreadNow += statuses.size();
        }

        if (settings.preCacheImages) {
            // delay it 15 seconds so that we can finish checking mentions first
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startService(new Intent(context, PreCacheService.class));
                }
            }, 15000);
        }

        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
    }

    try {
        Twitter twitter = Utils.getTwitter(context, settings);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        User user = twitter.verifyCredentials();
        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);

        long[] lastId = dataSource.getLastIds(currentAccount);
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId[0] > 0) {
            paging.sinceId(lastId[0]);
        }

        List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);

        int numNew = dataSource.insertTweets(statuses, currentAccount);

        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();

        if (settings.notifications && settings.mentionsNot && numNew > 0) {
            NotificationUtils.refreshNotification(context);
        }

    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }

    sharedPrefs.edit().putInt("pull_unread", unreadNow).commit();
    context.startService(new Intent(context, TalonPullNotificationService.class));

    context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
    getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);

    Log.v("talon_pull", "finished with the catchup service");

    CatchupPull.isRunning = false;
}

From source file:com.klinker.android.twitter.services.MentionsRefreshService.java

License:Apache License

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);

    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;/*from  ww w. j a v a2  s. c om*/
    }

    try {
        Twitter twitter = Utils.getTwitter(context, settings);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);

        long[] lastId = dataSource.getLastIds(currentAccount);
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId[0] > 0) {
            paging.sinceId(lastId[0]);
        }

        List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);

        int inserted = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);

        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();

        if (settings.notifications && settings.mentionsNot && inserted > 0) {
            if (intent.getBooleanExtra("from_launcher", false)) {
                NotificationUtils.refreshNotification(context, true);
            } else {
                NotificationUtils.refreshNotification(context);
            }
        }

        if (settings.syncSecondMentions) {
            startService(new Intent(context, SecondMentionsRefreshService.class));
        }

    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}

From source file:com.klinker.android.twitter.services.SecondMentionsRefreshService.java

License:Apache License

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences",
            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);

    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;//from   w w  w .j a va2  s  .c  o  m
    }

    boolean update = false;
    int numberNew = 0;

    try {
        Twitter twitter = Utils.getSecondTwitter(context);

        int currentAccount = sharedPrefs.getInt("current_account", 1);

        if (currentAccount == 1) {
            currentAccount = 2;
        } else {
            currentAccount = 1;
        }

        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);

        long lastId = dataSource.getLastIds(currentAccount)[0];
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId > 0) {
            paging.sinceId(lastId);
        }

        List<Status> statuses = twitter.getMentionsTimeline(paging);

        numberNew = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);

        if (numberNew > 0 && settings.notifications && settings.mentionsNot) {
            NotificationUtils.notifySecondMentions(context, currentAccount);
        }

    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}

From source file:com.mothsoft.integration.twitter.TwitterServiceImpl.java

License:Apache License

@SuppressWarnings("deprecation")
public List<Status> getHomeTimeline(AccessToken accessToken, Long sinceId, Short maximumNumber) {
    final Twitter twitter = this.factory.getInstance(accessToken);
    final List<Status> statuses = new ArrayList<Status>(maximumNumber);

    // default maximum number to 200 if null
    maximumNumber = maximumNumber == null ? 200 : maximumNumber;

    // default page size to lesser of maximumNumber, 200
    final int pageSize = maximumNumber > 200 ? 200 : maximumNumber;
    int page = 0;

    while (statuses.size() < maximumNumber) {
        Paging paging = new Paging(++page, pageSize);
        final ResponseList temp;

        if (sinceId != null) {
            paging = paging.sinceId(sinceId);
        }//from   w w  w .j  a va  2  s .c  om

        try {
            temp = twitter.getHomeTimeline(paging);
        } catch (TwitterException e) {
            throw this.wrapException(e);
        }

        // break out as soon as we get a page smaller than the designated
        // page size
        if (temp.size() == 0) {
            break;
        } else {
            statuses.addAll(temp);
        }

        // check rate limit status and warn or skip remaining fetches as
        // appropriate
        final RateLimitStatus rateLimitStatus = temp.getRateLimitStatus();
        if (rateLimitStatus.getRemaining() < (.1 * rateLimitStatus.getLimit())) {
            logger.warn("Twitter rate limit approaching. Calls remaining: " + rateLimitStatus.getRemaining());
        }

        if (rateLimitStatus.getRemainingHits() == 0) {
            final Date resetTime = new Date(
                    System.currentTimeMillis() + (rateLimitStatus.getSecondsUntilReset() * 1000));

            logger.error("Twitter rate limit hit.  Will reset at: " + resetTime.toLocaleString());
            break;
        }
    }

    return statuses;
}