List of usage examples for twitter4j StatusUpdate StatusUpdate
public StatusUpdate(String status)
From source file:jp.maju.wifiserver.client.ClientActivity.java
License:Apache License
@Override public void onClickLink(String url) { if (url.startsWith(CustomWebView.URL_RESULT_REGISTER) || url.startsWith(CustomWebView.URL_RESULT_LOGIN)) { url = url + "&uuid=" + PreferenceUtil.getUUID(getApplication()); }//from w w w .j av a 2 s .c om if (url.startsWith(CustomWebView.URL_TWEET)) { String query = url.substring(CustomWebView.URL_TWEET.length()); String[] params = query.split("&"); String kind = params[0].substring("name=".length()); String msg = params[1].substring("href=".length()); String login = params[2].substring("login=".length()); boolean isLogin = login.equals("true"); Twitter twitter = TwitterUtils.getRegisteredTwitterInstance(getApplication(), false); try { URLCodec codec = new URLCodec(); kind = codec.decode(kind, "UTF-8"); kind = kind.substring(0, kind.length() - 2).toString(); String storedKind = PreferenceUtil.getAnchor(getApplication()); if (storedKind == null) { storedKind = kind; } Logger.d(TAG, kind + "/" + storedKind); if (!kind.equals(storedKind)) { finish(); return; } PreferenceUtil.setAnchor(getApplication(), kind); } catch (UnsupportedEncodingException e) { Logger.e(TAG, e); } catch (DecoderException e) { Logger.e(TAG, e); } if (twitter != null) { Logger.d(TAG, "Twitter is not null"); Logger.d(TAG, "isLogin from server is " + isLogin); Logger.d(TAG, "isLogin from prefs is " + PreferenceUtil.getPreference(getApplication()).getBoolean("isLogin", false)); if (isLogin != PreferenceUtil.getPreference(getApplication()).getBoolean("isLogin", false)) { PreferenceUtil.getPreference(getApplication()).edit().putLong(kind + isLogin, -1L).commit(); } if (PreferenceUtil.isDifferentDate(getApplication(), kind + isLogin)) { PreferenceUtil.getPreference(getApplication()).edit().putBoolean("isLogin", isLogin).commit(); TweetTask tTask = new TweetTask(twitter, isLogin); tTask.setOnTweetResultListener(this); long time = System.currentTimeMillis(); URLCodec codec = new URLCodec(); try { msg = codec.decode(msg, "UTF-8"); PreferenceUtil.setClientMessage(getApplication(), msg, kind + isLogin); msg = msg.replace("${client}", "").replace("${date}", CommonUtil.formatDate(time)) .toString(); Logger.d(TAG, msg); StatusUpdate su = new StatusUpdate(msg); tTask.exec(su); } catch (UnsupportedEncodingException e) { Logger.e(TAG, e); } catch (DecoderException e) { Logger.e(TAG, e); } } } } else { Logger.d(TAG, "Twitter is null"); } sendMessage(url); }
From source file:kerguelenpetrel.BotherSomeoneServlet.java
License:Apache License
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { StringBuilder builder = new StringBuilder(); long[] friendIDs, victimIDs; resp.setContentType("text/plain; charset=UTF-8"); try {//w ww .j a v a 2 s . c o m //Get the Twitter object Twitter twit = TwitterFactory.getSingleton(); //Find a friend of a follower to bother friendIDs = twit.getFollowersIDs(twit.getId(), cursor).getIDs(); if (friendIDs.length == 0) { resp.getWriter().println("Cannot find any followers to bother \n"); return; } //Load the potential victim IDs victimIDs = twit.getFollowersIDs(friendIDs[r.nextInt(friendIDs.length)], cursor).getIDs(); if (victimIDs.length == 0) { resp.getWriter().println("Cannot find any followers to bother \n"); return; } //Write to our victim String victim = twit.showUser(victimIDs[r.nextInt(victimIDs.length)]).getScreenName(); //Get a global trend Trends t = twit.getPlaceTrends(1); String trend = t.getTrends()[r.nextInt(t.getTrends().length)].getName(); builder.append(getWordnikContent(victim, trend, resp)); if (builder.length() > 280) builder.setLength(280); //Tweets are limited to 280 characters //Set the status StatusUpdate status = new StatusUpdate(builder.toString()); //Post the status twit.updateStatus(status); resp.getWriter().println("Tweet posted: " + status.getStatus()); } catch (TwitterException e) { resp.getWriter().println("Problem with Twitter \n"); e.printStackTrace(resp.getWriter()); } }
From source file:kerguelenpetrel.FriendSomeoneServlet.java
License:Apache License
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { User friend = null;/*from ww w. j av a2 s.c o m*/ resp.setContentType("text/plain; charset=UTF-8"); try { //Get the Twitter object Twitter twit = TwitterFactory.getSingleton(); //Find a friend of a follower to bother long[] followerIDs = twit.getFollowersIDs(twit.getId(), cursor, 30).getIDs(); if (followerIDs.length == 0) { resp.getWriter().println("Cannot find any followers \n"); return; } //Load the potential victim IDs long[] friendIDs = twit.getFollowersIDs(followerIDs[r.nextInt(followerIDs.length)], cursor).getIDs(); if (friendIDs.length == 0) { resp.getWriter().println("Cannot find any followers to bother \n"); return; } //Get a new friend friend = twit.showUser(friendIDs[r.nextInt(friendIDs.length)]); twit.createFriendship(friend.getId()); resp.getWriter().println("Made a new friend with @" + friend.getScreenName()); //Write to our new friend StatusUpdate status = new StatusUpdate(writeToFriend(friend.getScreenName(), resp)); twit.updateStatus(status); resp.getWriter().println("Tweet posted: " + status.getStatus()); } catch (TwitterException e) { resp.getWriter().println("Problem with Twitter \n"); e.printStackTrace(resp.getWriter()); } }
From source file:kerguelenpetrel.RespondServlet.java
License:Apache License
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { StringBuilder builder = new StringBuilder(); resp.setContentType("text/plain; charset=UTF-8"); try {//from w w w . j a va2 s . c o m //Get the Twitter object Twitter twit = TwitterFactory.getSingleton(); ResponseList<Status> mentions = twit.getMentionsTimeline(); lastPostIdEntity = datastore.get(KeyFactory.createKey("lastPostIDEntity", "ID")); lastPostId = Long.parseLong(lastPostIdEntity.getProperty("lastPostID").toString()); if (mentions.size() == 0) { resp.getWriter().println("No mentions so far...\n"); return; } for (Status mention : mentions) { if (lastPostId < mention.getId()) { if (mention.getUser().getId() == twit.getId()) ; //don't respond to myself else if (mention.isRetweeted()) mention = twit.createFavorite(mention.getId()); //mark the retweet as a favourite else if (mention.getText().toLowerCase().contains("bye")) { builder.setLength(0); builder.append("@"); builder.append(mention.getUser().getScreenName()); builder.append(" Bye"); } else { builder.setLength(0); //Add the screen name of the person we are responding to builder.append("@"); builder.append(mention.getUser().getScreenName() + " "); //Get feed title as content builder.append(getFeedTitle(resp)); //Get a Wordnik trend builder.append(getWordnikTrend(resp)); /* Tweets are maximum 280 characters */ if (builder.length() > 280) { builder.setLength(builder.lastIndexOf(" ", 270)); builder.append(end[(r.nextInt(end.length))]); } } //Set the status StatusUpdate status = new StatusUpdate(builder.toString()); //Post the status twit.updateStatus(status); resp.getWriter().println("Tweet posted: " + status.getStatus()); } } //Save last post ID lastPostIdEntity.setProperty("lastPostID", (Long.toString(mentions.get(0).getId()))); datastore.put(lastPostIdEntity); } catch (EntityNotFoundException e) { // Make new ResponseIDentity lastPostIdEntity = new Entity("lastPostIDEntity", "ID"); lastPostIdEntity.setProperty("lastPostID", "0"); datastore.put(lastPostIdEntity); resp.getWriter() .println("Made new lastPostId " + lastPostIdEntity.getProperty("lastPostID").toString()); } catch (TwitterException e) { resp.getWriter().println("Problem with Twitter \n"); e.printStackTrace(resp.getWriter()); } }
From source file:kerguelenpetrel.UpdateStatusServlet.java
License:Apache License
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { StringBuilder builder = new StringBuilder(); resp.setContentType("text/plain; charset=UTF-8"); resp.getWriter().println("Updating status..."); try {//ww w. j a va 2 s .co m //Append feed title builder.append(getFeedTitle(resp)); //Append Wordnik example sentence builder.append(getWordnikSentence(resp)); /* Tweets are maximum 280 characters, so trim our sentence appropriately */ if (builder.length() > 280) { if (builder.lastIndexOf(";", 220) > 0) builder.setLength(builder.lastIndexOf(";", 220)); else if (builder.lastIndexOf(":", 220) > 0) builder.setLength(builder.lastIndexOf(":", 220)); else if (builder.lastIndexOf(",", 220) > 0) builder.setLength(builder.lastIndexOf(",", 220)); else builder.setLength(220); } //Append a Global trend Twitter twit = TwitterFactory.getSingleton(); builder.append( " " + twit.getPlaceTrends(1).getTrends()[r.nextInt(twit.getPlaceTrends(1).getTrends().length)] .getName()); // Append a Wordnik trend builder.append(getWordnikTrend(resp)); if (builder.length() > 280) builder.setLength(280); //Tweets are limited to 280 characters //Set the status StatusUpdate status = new StatusUpdate(builder.toString()); /* Add an image from Flickr for small status */ if (builder.length() < 180) status.setMediaIds(addFlickrImg(twit, resp)); twit.updateStatus(status); resp.getWriter().println("Tweet posted: " + status.getStatus()); } catch (TwitterException e) { resp.getWriter().println("Problem with Twitter \n"); e.printStackTrace(resp.getWriter()); } }
From source file:main.RelativeTwitter.java
License:Creative Commons License
public static void tweet() { Frame.tweetStatsLabel.setForeground(Color.red); Frame.tweetStatsLabel.setText("?????????????"); StatusUpdate statusUpdate = new StatusUpdate( Frame.bodyTextArea.getText() + " " + Frame.tagsTextArer.getText()); ImageIcon postimg = new ImageIcon(FileCheck.selectedImg().getPath()); Image instImg = postimg.getImage(); BufferedImage thmb = new BufferedImage(instImg.getWidth(null), instImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics g = thmb.getGraphics(); g.drawImage(instImg, 0, 0, null);/*from w ww. ja v a 2 s .c o m*/ File resizedImg; try { resizedImg = File.createTempFile("temp", ".jpg"); ImageIO.write(thmb, "JPG", resizedImg); statusUpdate.setMedia(resizedImg); } catch (IOException e2) { e2.printStackTrace(); } try { Frame.twitter.updateStatus(statusUpdate); Frame.tweetStatsLabel.setForeground(Color.BLUE); Frame.tweetStatsLabel.setText("???????"); } catch (TwitterException e1) { Frame.tweetStatsLabel.setForeground(Color.RED); Frame.tweetStatsLabel .setText("?????????????"); e1.printStackTrace(); } }
From source file:net.bluemix.droneselfie.TwitterUtilities.java
License:Open Source License
private String tweet(String pictureId, String message) { String output = null;//from w w w . j av a 2 s. c o m if (message == null) return null; if (message.equalsIgnoreCase("")) return null; try { String consumerKey = ConfigUtilities.getSingleton().getTwitterConsumerKey(); String consumerSecret = ConfigUtilities.getSingleton().getTwitterConsumerSecret(); String accessToken = ConfigUtilities.getSingleton().getTwitterAccessToken(); String accessTokenSecret = ConfigUtilities.getSingleton().getTwitterAccessTokenSecret(); TwitterFactory twitterFactory = new TwitterFactory(); Twitter twitter = twitterFactory.getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret)); StatusUpdate statusUpdate = new StatusUpdate(message); AttachmentInputStream data = DatabaseUtilities.getSingleton().getDB().getAttachment(pictureId, pictureId); statusUpdate.setMedia("picture", data); Status status = twitter.updateStatus(statusUpdate); if (status == null) return null; output = "https://twitter.com/bluedroneselfie/status/" + String.valueOf(status.getId()); return output; } catch (Exception e) { e.printStackTrace(); } return output; }
From source file:net.bluemix.newsaggregator.TwitterUtilities.java
License:Open Source License
private String tweet(String message) { String output = null;/*from www . j a v a 2 s .com*/ if (message == null) return null; if (message.equalsIgnoreCase("")) return null; try { String consumerKey = ConfigUtilities.getSingleton().getTwitterConsumerKey(); String consumerSecret = ConfigUtilities.getSingleton().getTwitterConsumerSecret(); String accessToken = ConfigUtilities.getSingleton().getTwitterAccessToken(); String accessTokenSecret = ConfigUtilities.getSingleton().getTwitterAccessTokenSecret(); TwitterFactory twitterFactory = new TwitterFactory(); Twitter twitter = twitterFactory.getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret)); StatusUpdate statusUpdate = new StatusUpdate(message); Status status = twitter.updateStatus(statusUpdate); if (status == null) return null; output = "https://twitter.com/BluemixInfo/status/" + String.valueOf(status.getId()); return output; } catch (Exception e) { e.printStackTrace(); } return output; }
From source file:net.hekatoncheir.speechtweet.SpeechTweet.java
License:Apache License
private void finishVoiceRecognize(String message) { // ?I/*from w ww . j a v a 2 s. c o m*/ finalizeSpeechRecognizer(); String plusMessage = " //tweet with speech recognition"; int len = message.length() + plusMessage.length(); if (len > 140) { message = message.substring(0, 140 - plusMessage.length()); } message += plusMessage; final StatusUpdate status = new StatusUpdate(message); new Thread(new Runnable() { @Override public void run() { Boolean isSuccess = false; try { mTwitter.updateStatus(status); isSuccess = true; } catch (TwitterException e) { } final Boolean s = isSuccess; mHandler.post(new Runnable() { public void run() { SpeechTweet.this.finishTweet(s); } }); } }).start(); }
From source file:net.lacolaco.smileessence.twitter.TweetBuilder.java
License:Open Source License
public StatusUpdate build() { StatusUpdate statusUpdate = new StatusUpdate(buildText()); if (inReplyToStatusID >= 0) { statusUpdate.setInReplyToStatusId(inReplyToStatusID); }//from ww w .java 2 s .c o m if (!TextUtils.isEmpty(mediaPath)) { File media = new File(mediaPath); if (media.exists()) { statusUpdate.setMedia(media); } } return statusUpdate; }