List of usage examples for twitter4j TwitterFactory getSingleton
public static Twitter getSingleton()
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 v a 2s .co 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.UnfriendSomeoneServlet.java
License:Apache License
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { User unfriend = null;//from w w w.jav a2 s . co m Random r = new Random(); 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).getIDs(); if (followerIDs.length == 0) { resp.getWriter().println("No friends to unfollow"); return; } unfriend = twit.showUser(followerIDs[r.nextInt(followerIDs.length)]); twit.destroyFriendship(unfriend.getId()); resp.getWriter().println("Successfully unfollowed @" + unfriend.getScreenName()); resp.getWriter().println("\n"); } catch (TwitterException e) { resp.getWriter().println("Problem with Twitter \n"); e.printStackTrace(resp.getWriter()); } catch (Exception e) { resp.getWriter().println("Problem! \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 {/* www.ja v a 2s.c o 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 OAuthIssue() { Frame.twitter = TwitterFactory.getSingleton(); // Twitter?//from w ww . j a v a 2 s . co m try { Frame.requestToken = Frame.twitter.getOAuthRequestToken(); } catch (TwitterException e2) { e2.printStackTrace(); } // ? // ? new BufferedReader(new InputStreamReader(System.in)); // ?? Desktop desktop = Desktop.getDesktop(); try { desktop.browse(new URI(Frame.requestToken.getAuthorizationURL())); } catch (IOException | URISyntaxException e2) { e2.printStackTrace(); } }
From source file:net.catchpole.pimpmylight.twitter.TwitterClient.java
License:Apache License
public TwitterClient() throws Exception { this.twitter = (new PropertiesFile("twitter4j.properties").isAvailable()) ? TwitterFactory.getSingleton() : null;// ww w.ja v a 2 s. c om }
From source file:net.jeremybrooks.suprsetr.twitter.TwitterHelper.java
License:Open Source License
/** * Authenticates the user.//w w w . j a v a 2s .co m * <p/> * <p>This method will use the OAuth method of authentication. The user's * browser will be opened, and they will be prompted for the PIN. The * authentication token data will then be saved in the database.</p> * * @throws Exception if there are any errors. */ public static void authenticate() throws Exception { ResourceBundle resourceBundle = ResourceBundle.getBundle("net.jeremybrooks.suprsetr.misc"); twitter = TwitterFactory.getSingleton(); twitter.setOAuthConsumer(Main.getPrivateProperty("TWITTER_CONSUMER_KEY"), Main.getPrivateProperty("TWITTER_CONSUMER_SECRET")); try { RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken; String url = requestToken.getAuthenticationURL(); java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); String pin = JOptionPane.showInputDialog(null, resourceBundle.getString("TwitterHelper.dialog.pin.message"), resourceBundle.getString("TwitterHelper.dialog.pin.title"), JOptionPane.INFORMATION_MESSAGE); accessToken = twitter.getOAuthAccessToken(requestToken, pin); // Save the access token LookupDAO.setKeyAndValue(SSConstants.LOOKUP_KEY_TWITTER_USERID, Long.toString(accessToken.getUserId())); LookupDAO.setKeyAndValue(SSConstants.LOOKUP_KEY_TWITTER_USERNAME, twitter.verifyCredentials().getName()); LookupDAO.setKeyAndValue(SSConstants.LOOKUP_KEY_TWITTER_TOKEN, accessToken.getToken()); LookupDAO.setKeyAndValue(SSConstants.LOOKUP_KEY_TWITTER_TOKEN_SECRET, accessToken.getTokenSecret()); } catch (Exception e) { twitter = null; throw e; } }
From source file:onl.area51.a51li.twitter.TwitterAuth.java
License:Apache License
public static void main(String args[]) throws Exception { final String consumerKey = args[0]; final String consumerSecret = args[1]; // The factory instance is re-useable and thread safe. Twitter twitter = TwitterFactory.getSingleton(); twitter.setOAuthConsumer(consumerKey, consumerSecret); RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null;//from ww w .j av a 2s .co m BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); System.out.println(requestToken.getAuthorizationURL()); System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:"); String pin = br.readLine(); try { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(); } } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); } else { te.printStackTrace(); } } } //persist to the accessToken for future reference. storeAccessToken(twitter.verifyCredentials().getId(), accessToken); Status status = twitter.updateStatus(args[0]); System.out.println("Successfully updated the status to [" + status.getText() + "]."); System.exit(0); }
From source file:org.iadb.knl.twitter.twitterknlapi.Twitterro.java
public static void main(String[] args) { // Config.registerSmbURLHandler(); // Config.setProperty("jcifs.smb.client.domain", domain); // Config.setProperty("jcifs.smb.client.username", user); // Config.setProperty("jcifs.smb.client.password", password); try {//from ww w . j a v a 2 s .c o m // Config.setProperty("jcifs.netbios.hostname", // Config.getProperty("jcifs.netbios.hostname", // InetAddress.getLocalHost().getHostName())); System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", proxyPort); System.setProperty("http.auth.ntlm.domain", domain); Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password.toCharArray()); } }; Authenticator.setDefault(authenticator); Twitter twitter = TwitterFactory.getSingleton(); Status status = twitter.updateStatus("Este arbitro va pitar la final de #bra vrs #uru"); System.out.println("Successfully updated the status to [" + status.getText() + "]."); // Query query = new Query(); // query.setQuery(""); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.kie.twitter.TwitterClient.java
License:Apache License
Twitter twitter() {
return TwitterFactory.getSingleton();
}
From source file:org.komusubi.feeder.sns.twitter.Twitter4j.java
License:Apache License
/** * create new instance. */ public Twitter4j() { this(TwitterFactory.getSingleton()); }