List of usage examples for twitter4j Twitter updateStatus
Status updateStatus(String status) throws TwitterException;
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 {//ww w . j a v a2s . 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 {//from w w w . ja v a 2 s. c om //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:mineTwit.Main.java
License:Open Source License
private void updateStatus(Twitter twitter, String newMessage) { if (twitter != null) { // Check newMessage try {// ww w . j ava 2 s . c o m // Debug code to check twitter rate limits Map<String, RateLimitStatus> rateLimit = twitter.getRateLimitStatus(); for (String endpoint : rateLimit.keySet()) { RateLimitStatus status = rateLimit.get(endpoint); //Test line to remove later //getLogger().info("Got rateLimits.endpoints"); //Omit any endpoints that haven't moved from default limit if (status.getRemaining() != status.getLimit()) { getLogger().info("Endpoint: " + endpoint); getLogger().info(" Limit: " + status.getLimit()); getLogger().info(" Remaining: " + status.getRemaining()); getLogger().info(" ResetTimeInSeconds: " + status.getResetTimeInSeconds()); getLogger().info(" SecondsUntilReset: " + status.getSecondsUntilReset()); } } boolean rateLimited = false; //Test line for debugging getLogger().info(" Duplicate Array value is : " + myNotifications[8].status); // Check if rateLimited by any particular endpoint. if (!rateLimited) { //Tweet if duplicates are off AND not duplicate AND not rate limited if (myNotifications[8].status) { getLogger().info("Duplicates are true.\n Who cares what the new message is."); twitter.updateStatus(newMessage + "\n" + new Date()); // Tweet anyway if duplicates are on AND not ratelimited } else if (!myNotifications[8].status && !newMessage.equals(getCurrentStatus(twitter))) { getLogger().info("Duplicates are false."); getLogger().info("Latest is ''" + newMessage + "''"); getLogger().info("Last was ''" + getCurrentStatus(twitter) + "''"); twitter.updateStatus(newMessage + "\n" + new Date()); } else { getLogger().info("Duplicates are false and message is duplicate"); } } else { getLogger().info("Twitter is rate limited, not tweeting"); } } catch (TwitterException e) { getLogger().info("Twitter is broken because of " + e); throw new RuntimeException(e); } } }
From source file:miproyectolunadepluton.MiProyectoLunaDePluton.java
/** * @param args the command line arguments *///from ww w . j a v a 2s . c o m public static void main(String[] args) throws TwitterException { /* ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) .setOAuthConsumerKey("BNmJ2oaezMC2qciIgkpC33kYq") .setOAuthConsumerSecret("1b0xCDy2Qh5nFWeuJjdF1MDcjpVJTh4LSpjTfUKeabacirUOn4") .setOAuthAccessToken("708080587068858368-FxuwrHLsox7xVSQRbwjDV14vOpSxNyN") .setOAuthAccessTokenSecret("c4gBOfVZL7MwyhI7jVPVrGupnNJAiZAJ2cDE9qp48OVl1");*/ TwitterFactory tf = new TwitterFactory(); Twitter mitwitter = tf.getInstance(); for (int i = 0; i < 11150; i++) { Status mistatus = mitwitter.updateStatus("Me la bufa"); System.out.println(mistatus.getText()); } }
From source file:mitwitter.MiTwitter.java
/** * @param args the command line arguments *//*from ww w. j ava2s . c o m*/ public static void main(String[] args) throws TwitterException { // TODO code application logic here //camobios Twitter twitter = new TwitterFactory().getInstance(); //CODIGO CAMBIAR ESTADO Status miStatus = twitter.updateStatus("Tercer ejemplo");//cambia el estado de twitter System.out.println(miStatus.getText()); //CODIGO TIMELINE List<Status> statuses = twitter.getHomeTimeline(); System.out.println("Showing home timeline.");//aparecen los ultimos twitts for (Status status : statuses) { System.out.println(status.getUser().getName() + ":" + status.getText()); } //CODIGO BUSCAR TAGS Query query = new Query("Chelsea"); //Dentro del String va el tag que quieres buscar QueryResult result = twitter.search(query); for (Status status : result.getTweets()) { System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); } }
From source file:net.bluemix.droneselfie.TwitterUtilities.java
License:Open Source License
private String tweet(String pictureId, String message) { String output = null;//from ww w. j a va 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 w w w . j a va2 s . co 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); 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.nitram509.twitter.TwitterService.java
License:Open Source License
public void postMessage(UserId userId, Tweet tweet) throws TwitterException { AccessToken accessToken = createAccessTokenFor(userId); Twitter twitter = twitterClientToolbox.getTwitterFor(accessToken); String message = twitterTextHelper.appendDefaultHashtag(tweet.getText(), tweet.getSuffix()); System.out.println(message);/*from ww w. j a v a 2s. c om*/ twitter.updateStatus(new StatusUpdate(formatMessage(message))); }
From source file:net.smileengine.tweettest.Main.java
public static void main(String[] args) throws TwitterException { Twitter twitter = new TwitterFactory().getInstance(); Status status = twitter.updateStatus("Jenkins"); }
From source file:noki.preciousshot.helper.TwitterHelper.java
License:Apache License
public static void tweetMedia(String text, File file) { Thread thread = new Thread() { private String text; private File file; public Thread setArgs(String text, File file) { this.text = text; this.file = file; return this; }/*w w w . j ava2s. c o m*/ @Override public void run() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(PreciousShotData.twitterKeys[0]) .setOAuthConsumerSecret(PreciousShotData.twitterKeys[1]) .setOAuthAccessToken(PreciousShotData.twitterKeys[2]) .setOAuthAccessTokenSecret(PreciousShotData.twitterKeys[3]); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); try { Status status = twitter.updateStatus(new StatusUpdate(this.text).media(this.file)); if (status != null && status.getId() != 0) { String url = String.format("https://twitter.com/%s/status/%s", twitter.getScreenName(), status.getId()); PreciousShotCore.log("the url is %s.", url); LangHelper.sendChatWithUrl(LangKey.TWITTER_SUCCESS, LangKey.TWITTER_URL, url); } else { LangHelper.sendChat(LangKey.TWITTER_FAILED); } } catch (TwitterException e) { LangHelper.sendChat(LangKey.TWITTER_FAILED); } } }.setArgs(text, file); thread.start(); }