Search in sources :

Example 31 with TwitterException

use of twitter4j.TwitterException in project twitter4j by yusuke.

the class ShowFriendship method main.

/**
     * Usage: java twitter4j.examples.friendship.ShowFriendship  [source screen name] [target screen name]
     *
     * @param args message
     */
public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage: java twitter4j.examples.friendship.ShowFriendship [source screen name] [target screen name]");
        System.exit(-1);
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        Relationship relationship = twitter.showFriendship(args[0], args[1]);
        System.out.println("isSourceBlockingTarget: " + relationship.isSourceBlockingTarget());
        System.out.println("isSourceFollowedByTarget: " + relationship.isSourceFollowedByTarget());
        System.out.println("isSourceFollowingByTarget: " + relationship.isSourceFollowingTarget());
        System.out.println("isSourceNotificationsEnabled: " + relationship.isSourceNotificationsEnabled());
        System.out.println("canSourceDm: " + relationship.canSourceDm());
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to show friendship: " + te.getMessage());
        System.exit(-1);
    }
}
Also used : Relationship(twitter4j.Relationship) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException)

Example 32 with TwitterException

use of twitter4j.TwitterException in project quorrabot by GloriousEggroll.

the class TwitterAPI method getlast.

public static String getlast() {
    try {
        List<Status> statuses = twitter.getUserTimeline();
        Status status = statuses.get(0);
        return status.getText();
    } catch (TwitterException te) {
        te.printStackTrace();
        return null;
    }
}
Also used : Status(twitter4j.Status) TwitterException(twitter4j.TwitterException)

Example 33 with TwitterException

use of twitter4j.TwitterException in project asterixdb by apache.

the class TwitterPullRecordReader method next.

@Override
public IRawRecord<String> next() throws IOException, InterruptedException {
    if (result == null || nextTweetIndex >= result.getTweets().size()) {
        Thread.sleep(1000 * requestInterval);
        query.setSinceId(lastTweetIdReceived);
        try {
            result = twitter.search(query);
        } catch (TwitterException e) {
            throw new HyracksDataException(e);
        }
        nextTweetIndex = 0;
    }
    if (result != null && !result.getTweets().isEmpty()) {
        List<Status> tw = result.getTweets();
        Status tweet = tw.get(nextTweetIndex++);
        if (lastTweetIdReceived < tweet.getId()) {
            lastTweetIdReceived = tweet.getId();
        }
        // transform tweet obj to json
        String jsonTweet = TwitterObjectFactory.getRawJSON(tweet);
        record.set(jsonTweet);
        return record;
    } else {
        return null;
    }
}
Also used : Status(twitter4j.Status) TwitterException(twitter4j.TwitterException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 34 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 35 with TwitterException

use of twitter4j.TwitterException in project android-priority-jobqueue by path.

the class PostTweetJob method onAdded.

@Override
public void onAdded() {
    // job has been secured to disk, add item to database
    try {
        Tweet tweet = new Tweet(localId, null, text, TwitterController.getInstance().getUserId(), null, new Date(System.currentTimeMillis()));
        TweetModel.getInstance().insertOrReplace(tweet);
        EventBus.getDefault().post(new PostingTweetEvent(tweet));
    } catch (TwitterException exception) {
    // if we cannot get user id, we won't add it locally for now.
    }
}
Also used : Tweet(com.path.android.jobqueue.examples.twitter.entities.Tweet) TwitterException(twitter4j.TwitterException) Date(java.util.Date) PostingTweetEvent(com.path.android.jobqueue.examples.twitter.events.PostingTweetEvent)

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