Search in sources :

Example 46 with Twitter

use of twitter4j.Twitter 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 47 with Twitter

use of twitter4j.Twitter in project aries by apache.

the class TwitterQuery method start.

/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
public void start(BundleContext context) throws Exception {
    Twitter twitter = new Twitter();
    Query query = new Query("from:theasf");
    try {
        QueryResult result = twitter.search(query);
        List<Tweet> tweets = result.getTweets();
        System.out.println("hits:" + tweets.size());
        for (Tweet tweet : tweets) {
            System.out.println(tweet.getFromUser() + ":" + StringEscapeUtils.unescapeXml(tweet.getText()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Tweet(twitter4j.Tweet) Twitter(twitter4j.Twitter)

Example 48 with Twitter

use of twitter4j.Twitter in project asterixdb by apache.

the class TwitterUtil method getTwitterService.

public static Twitter getTwitterService(Map<String, String> configuration) {
    ConfigurationBuilder cb = getAuthConfiguration(configuration);
    TwitterFactory tf = null;
    try {
        tf = new TwitterFactory(cb.build());
    } catch (Exception e) {
        if (LOGGER.isLoggable(Level.WARNING)) {
            StringBuilder builder = new StringBuilder();
            builder.append("Twitter Adapter requires the following config parameters\n");
            builder.append(AuthenticationConstants.OAUTH_CONSUMER_KEY + "\n");
            builder.append(AuthenticationConstants.OAUTH_CONSUMER_SECRET + "\n");
            builder.append(AuthenticationConstants.OAUTH_ACCESS_TOKEN + "\n");
            builder.append(AuthenticationConstants.OAUTH_ACCESS_TOKEN_SECRET + "\n");
            LOGGER.warning(builder.toString());
            LOGGER.warning("Unable to configure Twitter adapter due to incomplete/incorrect authentication credentials");
            LOGGER.warning("For details on how to obtain OAuth authentication token, visit https://dev.twitter.com/oauth" + "/overview/application-owner-access-tokens");
        }
    }
    Twitter twitter = tf.getInstance();
    return twitter;
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) AsterixException(org.apache.asterix.common.exceptions.AsterixException)

Example 49 with Twitter

use of twitter4j.Twitter 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 50 with Twitter

use of twitter4j.Twitter 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)

Aggregations

Twitter (twitter4j.Twitter)127 TwitterException (twitter4j.TwitterException)76 TwitterFactory (twitter4j.TwitterFactory)60 Status (twitter4j.Status)44 Activity (android.app.Activity)35 QueryResult (twitter4j.QueryResult)17 TimelineArrayAdapter (com.klinker.android.twitter.adapters.TimelineArrayAdapter)16 Intent (android.content.Intent)13 ArrayList (java.util.ArrayList)13 User (twitter4j.User)12 Query (twitter4j.Query)11 IDs (twitter4j.IDs)8 LinearLayout (android.widget.LinearLayout)7 IOException (java.io.IOException)7 Context (android.content.Context)6 DrawerActivity (com.klinker.android.twitter.activities.drawer_activities.DrawerActivity)6 LoginActivity (com.klinker.android.twitter.activities.setup.LoginActivity)6 AppSettings (com.klinker.android.twitter.settings.AppSettings)5 GeoLocation (twitter4j.GeoLocation)5 Paging (twitter4j.Paging)5