Example usage for twitter4j TwitterException getStatusCode

List of usage examples for twitter4j TwitterException getStatusCode

Introduction

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

Prototype

public int getStatusCode() 

Source Link

Usage

From source file:twitterAPI.Twitter_testing.java

public void sendTweet(String t) {
    try {//from w w  w  .j  ava  2 s.  c o m
        Status status = twitter.updateStatus(t);
        System.out.println("Successfully updated the status to [" + status.getText() + "].");
    } catch (TwitterException e) {
        System.out.println("Send tweet: " + e + " Status code: " + e.getStatusCode());
    }
}

From source file:twitterAPI.Twitter_testing.java

public void getTimeline() {
    try {//  ww  w  .  ja  v a  2  s  .co m
        statuses = twitter.getUserTimeline();
    } catch (TwitterException e) {
        System.out.println("Get timeline: " + e + " Status code: " + e.getStatusCode());
    }

    for (int i = 0; i < statuses.size(); i++) {
        Status status = (Status) statuses.get(i);
        System.out.println(status.getUser().getName() + ": " + status.getText());
    }
}

From source file:twitterplugin.TwitterLoginDialog.java

License:Open Source License

/**
 * Create Gui//from ww w  .  j av  a 2s  .co m
 */
private void createGui() {
    setTitle(mLocalizer.msg("login", "Login"));

    UiUtilities.registerForClosing(this);

    PanelBuilder content = new PanelBuilder(
            new FormLayout("5dlu, pref:grow(0.5), 3dlu, 100dlu, fill:pref:grow(0.5), 5dlu"));

    CellConstraints cc = new CellConstraints();

    final Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(mSettings.getConsumerKey(), mSettings.getConsumerSecret());
    try {
        mRequestToken = twitter.getOAuthRequestToken();
        mAuthorizationUrl = mRequestToken.getAuthorizationURL();
    } catch (TwitterException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    content.appendRow("3dlu");
    content.appendRow("pref");
    mLabelBrowser = new JLabel("1. " + mLocalizer.msg("step1", "Open authentication page on Twitter"));
    content.add(mLabelBrowser, cc.xy(2, content.getRowCount()));
    mUrlButton = new JButton(mLocalizer.msg("openBrowser", "Open browser"));
    mUrlButton.setToolTipText(mAuthorizationUrl);
    content.add(mUrlButton, cc.xy(4, content.getRowCount()));
    mUrlButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Launch.openURL(mAuthorizationUrl);
        }
    });

    content.appendRow("3dlu");
    content.appendRow("pref");
    mLabelPin = new JLabel("2. " + mLocalizer.msg("step2", "Enter PIN from web page"));
    content.add(mLabelPin, cc.xy(2, content.getRowCount()));
    mPIN = new JTextField();
    content.add(mPIN, cc.xy(4, content.getRowCount()));

    ButtonBarBuilder builder = new ButtonBarBuilder();
    builder.addGlue();

    JButton ok = new JButton(Localizer.getLocalization(Localizer.I18N_OK));

    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            mReturnValue = JOptionPane.OK_OPTION;
            AccessToken accessToken = null;
            try {
                String pin = mPIN.getText().trim();
                if (pin.length() > 0) {
                    accessToken = twitter.getOAuthAccessToken(mRequestToken, pin);
                } else {
                    accessToken = twitter.getOAuthAccessToken();
                }
            } catch (TwitterException te) {
                if (401 == te.getStatusCode()) {
                    System.out.println("Unable to get the access token.");
                } else {
                    te.printStackTrace();
                }
            }
            try {
                twitter.verifyCredentials().getId();
                mSettings.setAccessToken(accessToken);
            } catch (TwitterException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            setVisible(false);
        }
    });

    getRootPane().setDefaultButton(ok);

    JButton cancel = new JButton(Localizer.getLocalization(Localizer.I18N_CANCEL));

    cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            close();
        }
    });

    builder.addGriddedButtons(new JButton[] { ok, cancel });

    content.appendRow("fill:pref:grow");
    content.appendRow("pref");
    content.add(builder.getPanel(), cc.xyw(1, content.getRowCount(), 4));

    content.appendRow("5dlu");
    content.setBorder(Borders.DIALOG_BORDER);
    getContentPane().add(content.getPanel());

    UiUtilities.setSize(this, Sizes.dialogUnitXAsPixel(200, this), Sizes.dialogUnitYAsPixel(140, this));
}

From source file:uk.ac.susx.tag.method51.twitter.pipeline.TweetSearch.java

License:Apache License

@Override
protected void doWork() throws Exception {
    try {//  w  w  w .  j  a v  a2 s  . c o m

        final QueryResult result = twitter.search(query);

        final List<Status> statuses = result.getTweets();
        final List<Tweet> tweets = new ArrayList<>();

        for (final Status status : statuses) {

            getStats().update(status);

            final Tweet tweet = new Tweet(status);

            //strip out descs and urls
            if (!incProfileDescriptionUrl) {
                tweet.setDescription(null);
                tweet.setUrl(null);
            }

            tweets.add(tweet);

        }

        LOG.info("got {} tweets", tweets.size());
        long minId = getStats().getMinId();
        LOG.info("min id {}", new DateTime(Util.getInfoFromId(minId)[0]).toString("yyyy-MM-dd HH:mm:ss"));

        getStats().incrementTotal(tweets.size());

        LOG.info("{} / {} = {} tps", getStats().getTotal(), getStats().getDuration(TimeUnit.SECONDS),
                getStats().getTweetsPer(TimeUnit.SECONDS));

        produce(tweets);

        if (false && !result.hasNext()) {
            throw new ProducerUnderflowException("No more search result available.");
        } else if (tweets.size() == 0) {
            throw new ProducerUnderflowException("No more search result available.");
        } else if (searchRequested >= 0 && getStats().getTotal() >= searchRequested) {
            throw new ProducerUnderflowException("Reached requested size.");
        } else {
            query.setMaxId(getStats().getMinId() - 1);
            //query = result.nextQuery();
        }

    } catch (final TwitterException e) {

        switch (e.getStatusCode()) {

        case 130: {
            //over-capacity
            LOG.warn(e.getMessage());
            query.setMaxId(getStats().getMinId());

            int retryAfter = 60;

            LOG.info("rate limited - sleeping for {} seconds", retryAfter);
            Thread.sleep(1000 * (retryAfter + 1));
            break;
        }
        case 403:
            query.setMaxId(getStats().getMinId());
            LOG.info("ID reached: {}", getStats().getMinId());
            LOG.warn(e.getMessage());
            throw e;
            //break;

        case 420:
            LOG.info("rate limited - sleeping for 60 seconds", e);
            Thread.sleep(60000);
            break;

        case 429:
            LOG.error("", e);

            Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus();
            int retryAfter = rateLimitStatus.get("/search/tweets").getSecondsUntilReset();

            LOG.info("rate limited - sleeping for {} seconds", retryAfter);
            Thread.sleep(1000 * (retryAfter + 1));

            break;
        default:
            throw e;
        }
    } catch (Method51Exception e) {
        setStateFailure(e);
    }
}