Search in sources :

Example 36 with TwitterException

use of twitter4j.TwitterException in project opennms by OpenNMS.

the class MicroblogDMNotificationStrategy method send.

/**
 * {@inheritDoc}
 */
@Override
public int send(List<Argument> arguments) {
    Twitter svc = buildUblogService(arguments);
    String destUser = findDestName(arguments);
    DirectMessage response;
    if (destUser == null || "".equals(destUser)) {
        LOG.error("Cannot send a microblog DM notice to a user with no microblog username set. Either set a microblog username for this OpenNMS user or use the MicroblogUpdateNotificationStrategy instead.");
        return 1;
    }
    // In case the user tried to be helpful, avoid a double @@
    if (destUser.startsWith("@"))
        destUser = destUser.substring(1);
    String fullMessage = buildMessageBody(arguments);
    LOG.debug("Dispatching microblog DM notification at base URL '{}' with destination user '{}' and message '{}'", svc.getConfiguration().getClientURL(), destUser, fullMessage);
    try {
        response = svc.sendDirectMessage(destUser, fullMessage);
    } catch (TwitterException e) {
        LOG.error("Microblog notification failed at service URL '{}' to destination user '{}'", svc.getConfiguration().getClientURL(), destUser, e);
        return 1;
    }
    LOG.info("Microblog DM notification succeeded: DM sent with ID {}", response.getId());
    return 0;
}
Also used : DirectMessage(twitter4j.DirectMessage) Twitter(twitter4j.Twitter) TwitterException(twitter4j.TwitterException)

Example 37 with TwitterException

use of twitter4j.TwitterException in project Talon-for-Twitter by klinker24.

the class HomeFragment method doRefresh.

public int doRefresh() {
    int numberNew = 0;
    if (TimelineRefreshService.isRunning || WidgetRefreshService.isRunning || CatchupPull.isRunning) {
        // quit if it is running in the background
        return 0;
    }
    try {
        Cursor cursor = cursorAdapter.getCursor();
        if (cursor.moveToLast()) {
            long id = cursor.getLong(cursor.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID));
            sharedPrefs.edit().putLong("current_position_" + currentAccount, id).commit();
            HomeDataSource.getInstance(context).markPosition(currentAccount, id);
        // HomeContentProvider.updateCurrent(currentAccount, context, id);
        }
    } catch (Exception e) {
    }
    boolean needClose = false;
    context.sendBroadcast(new Intent("com.klinker.android.twitter.CLEAR_PULL_UNREAD"));
    twitter = Utils.getTwitter(context, settings);
    final List<twitter4j.Status> statuses = new ArrayList<twitter4j.Status>();
    boolean foundStatus = false;
    Paging paging = new Paging(1, 200);
    long[] lastId = null;
    long id;
    try {
        lastId = HomeDataSource.getInstance(context).getLastIds(currentAccount);
        id = lastId[1];
    } catch (Exception e) {
        id = sharedPrefs.getLong("account_" + currentAccount + "_lastid", 1l);
    }
    Log.v("talon_inserting", "since_id=" + id);
    try {
        paging.setSinceId(id);
    } catch (Exception e) {
    // 0 for some reason, so dont set one and let the database sort which should show and which shouldn't
    }
    long beforeDownload = Calendar.getInstance().getTimeInMillis();
    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 (TwitterException e) {
            Log.v("talon_error", "code: " + e.getErrorCode());
            if (e.getErrorCode() == 88) {
                // rate limit reached
                rateLimited = true;
                foundStatus = true;
                return 0;
            }
        } catch (Exception e) {
            // the page doesn't exist
            e.printStackTrace();
            Log.v("talon_error", "error with refresh");
            foundStatus = true;
        } catch (OutOfMemoryError o) {
        // don't know why...
        }
    }
    long afterDownload = Calendar.getInstance().getTimeInMillis();
    Log.v("talon_inserting", "downloaded " + statuses.size() + " tweets in " + (afterDownload - beforeDownload));
    if (statuses.size() > 0) {
        statuses.remove(statuses.size() - 1);
    }
    HashSet hs = new HashSet();
    hs.addAll(statuses);
    statuses.clear();
    statuses.addAll(hs);
    Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
    manualRefresh = false;
    if (needClose) {
        HomeDataSource.dataSource = null;
        Log.v("talon_home_frag", "sending the reset home broadcase in needclose section");
        dontGetCursor = true;
        context.sendBroadcast(new Intent("com.klinker.android.twitter.RESET_HOME"));
    }
    if (lastId == null) {
        try {
            lastId = HomeDataSource.getInstance(context).getLastIds(currentAccount);
        } catch (Exception e) {
            // let the
            lastId = new long[] { 0, 0, 0, 0, 0 };
        }
    }
    try {
        numberNew = insertTweets(statuses, lastId);
    } catch (NullPointerException e) {
        return 0;
    }
    if (numberNew > statuses.size()) {
        numberNew = statuses.size();
    }
    if (numberNew > 0 && statuses.size() > 0) {
        sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
    }
    Log.v("talon_inserting", "inserted " + numberNew + " tweets in " + (Calendar.getInstance().getTimeInMillis() - afterDownload));
    // numberNew = statuses.size();
    unread = numberNew;
    statuses.clear();
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long now = new Date().getTime();
    long alarm = now + settings.timelineRefresh;
    PendingIntent pendingIntent = PendingIntent.getService(context, HOME_REFRESH_ID, new Intent(context, TimelineRefreshService.class), 0);
    if (settings.timelineRefresh != 0)
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, settings.timelineRefresh, pendingIntent);
    else
        am.cancel(pendingIntent);
    int unreadCount;
    try {
        unreadCount = HomeDataSource.getInstance(context).getUnreadCount(currentAccount);
    } catch (Exception e) {
        unreadCount = numberNew;
    }
    return unreadCount;
}
Also used : Status(twitter4j.Status) TimelineRefreshService(com.klinker.android.twitter.services.TimelineRefreshService) Paging(twitter4j.Paging) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor) TwitterException(twitter4j.TwitterException) StaleDataException(android.database.StaleDataException) Date(java.util.Date) AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent) TwitterException(twitter4j.TwitterException) HashSet(java.util.HashSet)

Example 38 with TwitterException

use of twitter4j.TwitterException in project Talon-for-Twitter by klinker24.

the class ConversationFragment method getReplies.

public void getReplies(final ListView listView, final long tweetId, final LinearLayout progressSpinner, final HoloTextView none) {
    Thread getReplies = new Thread(new Runnable() {

        @Override
        public void run() {
            if (!isRunning) {
                return;
            }
            Twitter twitter = Utils.getTwitter(context, settings);
            replies = new ArrayList<twitter4j.Status>();
            try {
                status = twitter.showStatus(tweetId);
                if (status.isRetweet()) {
                    status = status.getRetweetedStatus();
                }
                twitter4j.Status replyStatus = twitter.showStatus(status.getInReplyToStatusId());
                try {
                    while (!replyStatus.getText().equals("")) {
                        if (!isRunning) {
                            return;
                        }
                        replies.add(replyStatus);
                        Log.v("reply_status", replyStatus.getText());
                        replyStatus = twitter.showStatus(replyStatus.getInReplyToStatusId());
                    }
                } catch (Exception e) {
                // the list of replies has ended, but we dont want to go to null
                }
            } catch (TwitterException e) {
                e.printStackTrace();
            }
            if (status != null && replies.size() > 0) {
                replies.add(0, status);
            }
            ((Activity) context).runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (replies.size() > 0) {
                            ArrayList<twitter4j.Status> reversed = new ArrayList<twitter4j.Status>();
                            for (int i = replies.size() - 1; i >= 0; i--) {
                                reversed.add(replies.get(i));
                            }
                            replies = reversed;
                            adapter = new TimelineArrayAdapter(context, replies);
                            listView.setAdapter(adapter);
                            listView.setVisibility(View.VISIBLE);
                            progressSpinner.setVisibility(View.GONE);
                        } else {
                        }
                    } catch (Exception e) {
                    // none and it got the null object
                    }
                    if (status != null) {
                        // everything here worked, so get the discussion on the tweet
                        getDiscussion(listView, tweetId, progressSpinner, none, status);
                    }
                }
            });
        }
    });
    getReplies.setPriority(7);
    getReplies.start();
}
Also used : Status(twitter4j.Status) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter) TwitterException(twitter4j.TwitterException) TwitterException(twitter4j.TwitterException)

Example 39 with TwitterException

use of twitter4j.TwitterException in project twicalico by moko256.

the class ShowTweetActivity method updateStatus.

private Single<Status> updateStatus() {
    return Single.create(subscriber -> {
        try {
            Status status = GlobalApplication.twitter.showStatus(statusId);
            GlobalApplication.statusCache.add(status);
            subscriber.onSuccess(GlobalApplication.statusCache.get(statusId));
        } catch (TwitterException e) {
            subscriber.onError(e);
        }
    });
}
Also used : Status(twitter4j.Status) TwitterException(twitter4j.TwitterException)

Example 40 with TwitterException

use of twitter4j.TwitterException in project twicalico by moko256.

the class ShowUserActivity method getUserSingle.

public Single<User> getUserSingle() {
    return Single.create(subscriber -> {
        try {
            User user = null;
            if (userId != -1) {
                user = GlobalApplication.twitter.showUser(userId);
            } else if (userScreenName != null) {
                user = GlobalApplication.twitter.showUser(userScreenName);
                GlobalApplication.userCache.add(user);
            }
            subscriber.onSuccess(user);
        } catch (TwitterException e) {
            subscriber.onError(e);
        }
    });
}
Also used : User(twitter4j.User) TwitterException(twitter4j.TwitterException)

Aggregations

TwitterException (twitter4j.TwitterException)96 Twitter (twitter4j.Twitter)69 TwitterFactory (twitter4j.TwitterFactory)54 Status (twitter4j.Status)21 User (twitter4j.User)12 Intent (android.content.Intent)11 ArrayList (java.util.ArrayList)9 File (java.io.File)6 IDs (twitter4j.IDs)6 Context (android.content.Context)5 Date (java.util.Date)4 DirectMessage (twitter4j.DirectMessage)4 Paging (twitter4j.Paging)4 AccessToken (twitter4j.auth.AccessToken)4 Activity (android.app.Activity)3 SharedPreferences (android.content.SharedPreferences)3 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)3 View (android.view.View)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3