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);
}
}
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();
}
}
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;
}
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");
}
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;
}
Aggregations