/src/com/teamboid/twitter/utilities/TwitlongerHelper.java

https://bitbucket.org/thenameisnigel/boid-for-twitter · Java · 164 lines · 101 code · 15 blank · 48 comment · 14 complexity · de8debffb882549f10a1c36a3d5c80c9 MD5 · raw file

  1. package com.teamboid.twitter.utilities;
  2. import java.io.StringReader;
  3. import java.util.ArrayList;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.NameValuePair;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.HttpGet;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.apache.http.message.BasicNameValuePair;
  13. import org.apache.http.util.EntityUtils;
  14. import org.w3c.dom.Element;
  15. import org.xml.sax.InputSource;
  16. /**
  17. * A helper class for using the Twitlonger API.
  18. * @author Aidan Follestad
  19. */
  20. public class TwitlongerHelper {
  21. private TwitlongerHelper(String application, String apiKey, String username) {
  22. _app = application;
  23. _key = apiKey;
  24. _user = username;
  25. }
  26. private String _app;
  27. private String _key;
  28. private String _user;
  29. /**
  30. * Creates a new TwitlongerHelper instance.
  31. * @param application The application identifier given to you by Twitlonger.
  32. * @param apiKey The API key given to you by Twitlonger.
  33. * @param username The screenname of the user that will be posting long tweets.
  34. * @return A new TwitlongerHelper instance ready for posting and reading Twitlonger tweets.
  35. */
  36. public static TwitlongerHelper create(String application, String apiKey, String username) { return new TwitlongerHelper(application, apiKey, username); }
  37. /**
  38. * Makes a post to Twitlonger, returning the text that should be posted in a tweet on Twitter.
  39. * @param message The full content of the tweet, should be over 140 characters since you're using Twitlonger.
  40. * @param inReplyTo The optional ID of the tweet this post is in reply to.
  41. * @param inReplyToName The optional username of the user that posted the tweet that this post is in reply to.
  42. * @return The text that should be posted on Twitter.
  43. * @throws Exception
  44. */
  45. public TwitlongerPostResponse post(String message, long inReplyTo, String inReplyToName) throws Exception {
  46. ArrayList<NameValuePair> args = new ArrayList<NameValuePair>();
  47. args.add(new BasicNameValuePair("application", _app));
  48. args.add(new BasicNameValuePair("api_key", _key));
  49. args.add(new BasicNameValuePair("username", _user));
  50. args.add(new BasicNameValuePair("message", message));
  51. if(inReplyTo > 0) {
  52. args.add(new BasicNameValuePair("in_reply", Long.toString(inReplyTo)));
  53. if(inReplyToName != null && !inReplyToName.trim().isEmpty()) args.add(new BasicNameValuePair("in_reply_user", inReplyToName));
  54. }
  55. Element xml = makeXmlPost("http://www.twitlonger.com/api_post", args);
  56. return new TwitlongerPostResponse(xml);
  57. }
  58. /**
  59. * Performs the Twitlonger callback, should be done after successfully using the 'post' method.
  60. * @param twitterId The ID of the tweet posted directly to Twitter by you using the results of 'post'.
  61. * @param response The TwitlongerPostResponse instance returned from your previous use of 'post'.
  62. * @return True if successful.
  63. * @throws Exception
  64. */
  65. public boolean callback(long twitterId, TwitlongerPostResponse response) throws Exception {
  66. ArrayList<NameValuePair> args = new ArrayList<NameValuePair>(2);
  67. args.add(new BasicNameValuePair("application", _app));
  68. args.add(new BasicNameValuePair("api_key", _key));
  69. args.add(new BasicNameValuePair("message_id", response.getId()));
  70. args.add(new BasicNameValuePair("twitter_id ", Long.toString(twitterId)));
  71. return makePost("http://www.twitlonger.com/api_set_id", args);
  72. }
  73. /**
  74. * Retrieves the full expanded content of a tweet longer post.
  75. * @param id The ID (at the end of a shortened URL such as tl.gd/id).
  76. * @return The full expanded content.
  77. * @throws Exception
  78. */
  79. public String readPost(String id) throws Exception {
  80. return makeGet("http://www.twitlonger.com/api_read/" + id).getElementsByTagName("content").item(0).getTextContent();
  81. }
  82. private Element makeXmlPost(String url, ArrayList<NameValuePair> entities) throws Exception {
  83. HttpClient httpclient = new DefaultHttpClient();
  84. HttpPost httppost = new HttpPost(url);
  85. httppost.setEntity(new UrlEncodedFormEntity(entities, "UTF-8"));
  86. HttpResponse response = httpclient.execute(httppost);
  87. String responseStr = EntityUtils.toString(response.getEntity(), "UTF-8");
  88. Element toReturn = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(responseStr))).getDocumentElement();
  89. if(toReturn.getElementsByTagName("error") != null && toReturn.getElementsByTagName("error").getLength() > 0) {
  90. throw new Exception(toReturn.getElementsByTagName("error").item(0).getTextContent());
  91. }
  92. return toReturn;
  93. }
  94. private boolean makePost(String url, ArrayList<NameValuePair> entities) throws Exception {
  95. HttpClient httpclient = new DefaultHttpClient();
  96. HttpPost httppost = new HttpPost(url);
  97. httppost.setEntity(new UrlEncodedFormEntity(entities, "UTF-8"));
  98. HttpResponse response = httpclient.execute(httppost);
  99. String responseStr = EntityUtils.toString(response.getEntity(), "UTF-8");
  100. try {
  101. Element toReturn = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(responseStr))).getDocumentElement();
  102. if(toReturn.getElementsByTagName("error") != null && toReturn.getElementsByTagName("error").getLength() > 0) {
  103. throw new Exception(toReturn.getElementsByTagName("error").item(0).getTextContent());
  104. }
  105. } catch(Exception e) { return (response.getStatusLine().getStatusCode() == 200); }
  106. return true;
  107. }
  108. private Element makeGet(String url) throws Exception {
  109. HttpClient httpclient = new DefaultHttpClient();
  110. HttpGet httpget = new HttpGet(url);
  111. HttpResponse response = httpclient.execute(httpget);
  112. String responseStr = EntityUtils.toString(response.getEntity(), "UTF-8");
  113. try {
  114. Element toReturn = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(responseStr))).getDocumentElement();
  115. if(toReturn.getElementsByTagName("error") != null && toReturn.getElementsByTagName("error").getLength() > 0) {
  116. throw new Exception(toReturn.getElementsByTagName("error").item(0).getTextContent());
  117. }
  118. return toReturn;
  119. } catch(Exception e) { e.printStackTrace(); }
  120. return null;
  121. }
  122. /**
  123. * Contains a response returned from posting to Twitlonger.
  124. * @author Aidan Follestad
  125. */
  126. public static class TwitlongerPostResponse {
  127. /**
  128. * Initializes a new TwitlongerResponse instance by parsing returned XML.
  129. * @param xml The XML returned from a Twitlonger request.
  130. */
  131. public TwitlongerPostResponse(Element xml) {
  132. Element post = (Element)xml.getElementsByTagName("post").item(0);
  133. content = post.getElementsByTagName("content").item(0).getTextContent();
  134. id = post.getElementsByTagName("id").item(0).getTextContent();
  135. }
  136. private String content;
  137. private String id;
  138. /**
  139. * Gets the content of the Twitlonger post, which is the shortened version of your over-140-character tweet.
  140. * @return Twitlonger post content
  141. */
  142. public String getContent() { return content; }
  143. /**
  144. * Gets the ID of the Twitlonger post.
  145. * @return The Twitlonger post ID.
  146. */
  147. public String getId() { return id; }
  148. }
  149. }