Search in sources :

Example 6 with TwitterException

use of twitter4j.TwitterException in project twitter-2-weibo by rjyo.

the class UserServlet method handleRequest.

@Override
protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) {
    HttpServletRouter r = new HttpServletRouter(request);
    r.setPattern("/:id");
    HttpSession session = request.getSession(false);
    DBHelper helper = (DBHelper) request.getAttribute(Keys.REQUEST_DB_HELPER);
    // Service limit
    String uId = r.get(":id");
    if (!helper.isUser(uId) && helper.getUserCount() > 50) {
        return getTemplate("full.vm");
    }
    T2WUser user = helper.findOneByUser(uId);
    if (r.has(":id")) {
        log.info("Displaying user info for @" + uId);
        ctx.put("user_id", uId);
        ctx.put("user", helper.findOneByUser(uId));
        try {
            User weiboUser = (User) session.getAttribute(Keys.SESSION_WEIBO_USER);
            if (weiboUser == null) {
                Users um = new Users();
                weiboUser = um.showUserById(user.getWeiboUserId());
                session.setAttribute(Keys.SESSION_WEIBO_USER, weiboUser);
            }
            ctx.put("weibo_user", weiboUser.getScreenName());
            ctx.put("weibo_user_image", weiboUser.getProfileImageURL().toString());
            ctx.put("weibo_login", 1);
            // save weiboUser ID mapping
            helper.setWeiboId(user.getUserId(), weiboUser.getScreenName());
        } catch (Exception e) {
            // 401 = not logged in
            if (e instanceof WeiboException && ((WeiboException) e).getStatusCode() != 401) {
                e.printStackTrace();
            }
        }
        try {
            twitter4j.User twitterUser = (twitter4j.User) session.getAttribute(Keys.SESSION_TWITTER_USER);
            if (twitterUser == null) {
                TwitterFactory factory = new TwitterFactory();
                Twitter t = factory.getInstance();
                t.setOAuthAccessToken(new AccessToken(user.getTwitterToken(), user.getTwitterTokenSecret()));
                twitterUser = t.verifyCredentials();
                session.setAttribute(Keys.SESSION_TWITTER_USER, twitterUser);
            }
            ctx.put("twitter_user", twitterUser.getScreenName());
            ctx.put("twitter_user_image", twitterUser.getProfileImageURL().toString());
            ctx.put("twitter_login", 1);
        } catch (Exception e) {
            // 401 = not logged in
            if (e instanceof TwitterException && ((TwitterException) e).getStatusCode() != 401) {
                e.printStackTrace();
            }
        }
    }
    Object message = session.getAttribute(Keys.SESSION_MESSAGE);
    if (message != null) {
        ctx.put("message", message);
        session.removeAttribute(Keys.SESSION_MESSAGE);
    }
    Object prompt = session.getAttribute(Keys.SESSION_PROMPT_TWEET);
    if (prompt != null) {
        ctx.put("prompt", prompt);
        session.removeAttribute(Keys.SESSION_PROMPT_TWEET);
    }
    return getTemplate("user.vm");
}
Also used : T2WUser(h2weibo.model.T2WUser) User(weibo4j.model.User) HttpSession(javax.servlet.http.HttpSession) DBHelper(h2weibo.model.DBHelper) Twitter(twitter4j.Twitter) Users(weibo4j.Users) TwitterFactory(twitter4j.TwitterFactory) WeiboException(weibo4j.model.WeiboException) TwitterException(twitter4j.TwitterException) WeiboException(weibo4j.model.WeiboException) T2WUser(h2weibo.model.T2WUser) AccessToken(twitter4j.auth.AccessToken) HttpServletRouter(h2weibo.HttpServletRouter) TwitterException(twitter4j.TwitterException)

Example 7 with TwitterException

use of twitter4j.TwitterException in project camel by apache.

the class TwitterComponentVerifier method verifyCredentials.

private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) throws Exception {
    try {
        TwitterConfiguration configuration = setProperties(new TwitterConfiguration(), parameters);
        Twitter twitter = configuration.getTwitter();
        twitter.verifyCredentials();
    } catch (TwitterException e) {
        // verifyCredentials throws TwitterException when Twitter service or
        // network is unavailable or if supplied credential is wrong
        ResultErrorBuilder errorBuilder = ResultErrorBuilder.withHttpCodeAndText(e.getStatusCode(), e.getErrorMessage()).attribute("twitter.error.code", e.getErrorCode()).attribute("twitter.status.code", e.getStatusCode()).attribute("twitter.exception.code", e.getExceptionCode()).attribute("twitter.exception.message", e.getMessage()).attribute("twitter.exception.instance", e);
        //   https://dev.twitter.com/overview/api/response-codes
        if (e.getErrorCode() == 89) {
            errorBuilder.parameter("accessToken");
        }
        builder.error(errorBuilder.build());
    }
}
Also used : Twitter(twitter4j.Twitter) ResultErrorBuilder(org.apache.camel.impl.verifier.ResultErrorBuilder) TwitterException(twitter4j.TwitterException)

Example 8 with TwitterException

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

the class ActivityDataSource method getRetweeterContentValues.

public ContentValues getRetweeterContentValues(Status status, int account, Twitter twitter) {
    try {
        List<Status> retweets = twitter.getRetweets(status.getId());
        List<User> users = new ArrayList<User>();
        for (Status s : retweets) {
            users.add(s.getUser());
        }
        ContentValues values = new ContentValues();
        if (retweets.size() > 0) {
            long id = status.getId();
            String[] html = TweetLinkUtils.getLinksInStatus(status);
            String text = html[0];
            String media = html[1];
            String otherUrl = html[2];
            String hashtags = html[3];
            String userString = html[4];
            if (media.contains("/tweet_video/")) {
                media = media.replace("tweet_video", "tweet_video_thumb").replace(".mp4", ".png");
            }
            values.put(ActivitySQLiteHelper.COLUMN_TITLE, buildUsersTitle(users));
            values.put(ActivitySQLiteHelper.COLUMN_ACCOUNT, account);
            values.put(ActivitySQLiteHelper.COLUMN_TEXT, status.getRetweetCount() + " " + (status.getRetweetCount() == 1 ? context.getString(R.string.retweet) : context.getString(R.string.retweets)) + ": " + text);
            values.put(ActivitySQLiteHelper.COLUMN_TWEET_ID, status.getId());
            values.put(ActivitySQLiteHelper.COLUMN_NAME, status.getUser().getName());
            values.put(ActivitySQLiteHelper.COLUMN_SCREEN_NAME, status.getUser().getScreenName());
            values.put(ActivitySQLiteHelper.COLUMN_PRO_PIC, buildProPicUrl(users));
            values.put(ActivitySQLiteHelper.COLUMN_TIME, retweets.get(0).getCreatedAt().getTime());
            values.put(ActivitySQLiteHelper.COLUMN_PIC_URL, media);
            values.put(ActivitySQLiteHelper.COLUMN_TYPE, TYPE_RETWEETS);
            values.put(ActivitySQLiteHelper.COLUMN_FAV_COUNT, status.getFavoriteCount());
            values.put(ActivitySQLiteHelper.COLUMN_RETWEET_COUNT, status.getRetweetCount());
            values.put(ActivitySQLiteHelper.COLUMN_URL, otherUrl);
            values.put(ActivitySQLiteHelper.COLUMN_PIC_URL, media);
            values.put(ActivitySQLiteHelper.COLUMN_USERS, userString);
            values.put(ActivitySQLiteHelper.COLUMN_HASHTAGS, hashtags);
            values.put(ActivitySQLiteHelper.COLUMN_ANIMATED_GIF, TweetLinkUtils.getGIFUrl(status, otherUrl));
            values.put(HomeSQLiteHelper.COLUMN_CONVERSATION, status.getInReplyToStatusId() == -1 ? 0 : 1);
        } else {
            return null;
        }
        return values;
    } catch (TwitterException e) {
        return null;
    }
}
Also used : Status(twitter4j.Status) ContentValues(android.content.ContentValues) User(twitter4j.User) ArrayList(java.util.ArrayList) TwitterException(twitter4j.TwitterException)

Example 9 with TwitterException

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

the class MentionsRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    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;
    }
    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());
    }
}
Also used : Context(android.content.Context) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) Twitter(twitter4j.Twitter) Intent(android.content.Intent) MentionsDataSource(com.klinker.android.twitter.data.sq_lite.MentionsDataSource) TwitterException(twitter4j.TwitterException)

Example 10 with TwitterException

use of twitter4j.TwitterException in project twitter4j by yusuke.

the class GetRateLimitStatus method main.

/**
     * Usage: java twitter4j.examples.account.GetRateLimitStatus
     *
     * @param args message
     */
public static void main(String[] args) {
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus();
        for (String endpoint : rateLimitStatus.keySet()) {
            RateLimitStatus status = rateLimitStatus.get(endpoint);
            System.out.println("Endpoint: " + endpoint);
            System.out.println(" Limit: " + status.getLimit());
            System.out.println(" Remaining: " + status.getRemaining());
            System.out.println(" ResetTimeInSeconds: " + status.getResetTimeInSeconds());
            System.out.println(" SecondsUntilReset: " + status.getSecondsUntilReset());
        }
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get rate limit status: " + te.getMessage());
        System.exit(-1);
    }
}
Also used : RateLimitStatus(twitter4j.RateLimitStatus) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException)

Aggregations

TwitterException (twitter4j.TwitterException)88 Twitter (twitter4j.Twitter)69 TwitterFactory (twitter4j.TwitterFactory)54 Status (twitter4j.Status)19 Intent (android.content.Intent)9 User (twitter4j.User)8 ArrayList (java.util.ArrayList)7 File (java.io.File)6 DirectMessage (twitter4j.DirectMessage)6 IDs (twitter4j.IDs)6 Paging (twitter4j.Paging)6 Context (android.content.Context)4 AppSettings (com.klinker.android.twitter.settings.AppSettings)4 Date (java.util.Date)4 SavedSearch (twitter4j.SavedSearch)4 AccessToken (twitter4j.auth.AccessToken)4 MentionsDataSource (com.klinker.android.twitter.data.sq_lite.MentionsDataSource)3 IOException (java.io.IOException)3 UserList (twitter4j.UserList)3 RequestToken (twitter4j.auth.RequestToken)3