Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewzbinCommand.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo.commands;
  6. import com.dmdirc.util.Downloader;
  7. import com.dmdirc.util.MapList;
  8. import com.md87.charliebravo.Command;
  9. import com.md87.charliebravo.Followup;
  10. import com.md87.charliebravo.InputHandler;
  11. import com.md87.charliebravo.Response;
  12. import java.io.StringReader;
  13. import java.net.URLEncoder;
  14. import java.nio.charset.Charset;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.LinkedList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.jdom.Document;
  21. import org.jdom.Element;
  22. import org.jdom.Namespace;
  23. import org.jdom.input.SAXBuilder;
  24. /**
  25. *
  26. * @author chris
  27. */
  28. public class NewzbinCommand implements Command {
  29. protected static final String URL = "/search/query/?q=%s" +
  30. "&area=-1&fpn=p&searchaction=Go&areadone=-1&sort=date&order=desc" +
  31. "&feed=rss";
  32. protected static final String LOGIN_URL = "https://www.newzbin.com/account/login";
  33. @SuppressWarnings("unchecked")
  34. public void execute(InputHandler handler, Response response, String line) throws Exception {
  35. if (line.isEmpty()) {
  36. response.sendMessage("You need to specify a search query", true);
  37. } else {
  38. final Map<String, String> args = new HashMap<String, String>();
  39. args.put("ret_url", String.format(URL,
  40. URLEncoder.encode(line, Charset.defaultCharset().name())));
  41. args.put("username", "dataforce");
  42. args.put("password", "dfrox");
  43. final List<String> result = Downloader.getPage(LOGIN_URL, args);
  44. final StringBuilder data = new StringBuilder();
  45. for (String resultline : result) {
  46. data.append(resultline);
  47. data.append('\n');
  48. }
  49. System.out.println(data);
  50. final Document document = new SAXBuilder().build(new StringReader(data.toString()));
  51. final List<Result> results = new LinkedList<Result>();
  52. for (Element item : (List<Element>) document.getRootElement()
  53. .getChild("channel").getChildren("item")) {
  54. results.add(new Result(item));
  55. }
  56. response.sendMessage("there " + (results.size() == 1 ? "was" : "were")
  57. + " " + results.size() + " result" + (results.size() == 1 ? "s" : "")
  58. + " returned." + (results.size() == 1 ? "It is: " : "The first one is: ")
  59. + results.get(0).getSummary());
  60. response.addFollowup(new AttributesFollowup(results.get(0)));
  61. response.addFollowup(new NextFollowup(results, 1));
  62. }
  63. }
  64. protected static class NextFollowup implements Followup {
  65. private final List<Result> results;
  66. private final int next;
  67. public NextFollowup(List<Result> results, int next) {
  68. this.results = results;
  69. this.next = next;
  70. }
  71. public boolean matches(String line) {
  72. return next < results.size() && line.equalsIgnoreCase("next");
  73. }
  74. public void execute(InputHandler handler, Response response, String line) throws Exception {
  75. response.sendMessage("result " + next + "/" + results.size() + "is: "
  76. + results.get(next).getSummary());
  77. response.addFollowup(new AttributesFollowup(results.get(next)));
  78. response.addFollowup(new NextFollowup(results, next + 1));
  79. response.addFollowup(new PreviousFollowup(results, next - 1));
  80. }
  81. }
  82. protected static class PreviousFollowup implements Followup {
  83. private final List<Result> results;
  84. private final int prev;
  85. public PreviousFollowup(List<Result> results, int next) {
  86. this.results = results;
  87. this.prev = next;
  88. }
  89. public boolean matches(String line) {
  90. return prev > 0 && line.equalsIgnoreCase("previous");
  91. }
  92. public void execute(InputHandler handler, Response response, String line) throws Exception {
  93. response.sendMessage("result " + prev + "/" + results.size() + "is: "
  94. + results.get(prev).getSummary());
  95. response.addFollowup(new AttributesFollowup(results.get(prev)));
  96. response.addFollowup(new NextFollowup(results, prev + 1));
  97. response.addFollowup(new PreviousFollowup(results, prev - 1));
  98. }
  99. }
  100. protected static class AttributesFollowup implements Followup {
  101. private final Result result;
  102. public AttributesFollowup(Result result) {
  103. this.result = result;
  104. }
  105. public boolean matches(String line) {
  106. return line.equalsIgnoreCase("attributes");
  107. }
  108. public void execute(InputHandler handler, Response response, String line) throws Exception {
  109. final StringBuilder builder = new StringBuilder();
  110. for (Map.Entry<String, List<String>> entry : result.getAttributes().entrySet()) {
  111. if (builder.length() > 0) {
  112. builder.append("; ");
  113. }
  114. builder.append(entry.getKey() + ": ");
  115. boolean first = true;
  116. for (String value : entry.getValue()) {
  117. if (first) {
  118. first = false;
  119. } else {
  120. builder.append(", ");
  121. }
  122. builder.append(value);
  123. }
  124. }
  125. response.setInheritFollows(true);
  126. response.sendMessage("that result has the following attributes: " + builder.toString());
  127. }
  128. }
  129. protected static class Result {
  130. protected static final String REPORT_NS = "http://www.newzbin.com/DTD/2007/feeds/report/";
  131. protected static final String NZB_URL = "http://www.newzbin.com/browse/post/%s/nzb/";
  132. private final String title, category, moreinfo, nfolink, poster, date;
  133. private final MapList<String, String> attributes = new MapList<String, String>();
  134. private final List<String> groups = new ArrayList<String>();
  135. private final int id, nfoid, views, comments;
  136. private final long size;
  137. public Result(final Element element) {
  138. final Namespace namespace = Namespace.getNamespace(REPORT_NS);
  139. title = element.getChildTextTrim("title");
  140. id = Integer.parseInt(element.getChildTextTrim("id", namespace));
  141. category = element.getChildTextTrim("category", namespace);
  142. for (Object attribute : element.getChildren("attributes", namespace)) {
  143. final String type = ((Element) attribute).getAttributeValue("type");
  144. final String value = ((Element) attribute).getTextTrim();
  145. attributes.add(type, value);
  146. }
  147. for (Object group : element.getChildren("groups", namespace)) {
  148. groups.add(((Element) group).getTextTrim());
  149. }
  150. moreinfo = element.getChildTextTrim("moreinfo", namespace);
  151. nfoid = Integer.parseInt(element.getChild("nfo", namespace)
  152. .getChildText("fileid", namespace));
  153. nfolink = element.getChild("nfo", namespace)
  154. .getChildText("link", namespace);
  155. poster = element.getChildText("poster", namespace);
  156. size = Long.parseLong(element.getChildTextTrim("size", namespace));
  157. date = element.getChildText("postdate", namespace);
  158. views = Integer.parseInt(element.getChild("stats", namespace)
  159. .getChildText("views", namespace));
  160. comments = Integer.parseInt(element.getChild("stats", namespace)
  161. .getChildText("comments", namespace));
  162. }
  163. public String getSummary() {
  164. return "'" + title + "' (" + getSizeMB() + "MiB), nzb link: "
  165. + String.format(NZB_URL, id);
  166. }
  167. public String getSizeMB() {
  168. return String.format("%,.2f", size / (1024 * 1024));
  169. }
  170. public MapList<String, String> getAttributes() {
  171. return attributes;
  172. }
  173. public String getCategory() {
  174. return category;
  175. }
  176. public int getComments() {
  177. return comments;
  178. }
  179. public String getDate() {
  180. return date;
  181. }
  182. public List<String> getGroups() {
  183. return groups;
  184. }
  185. public int getId() {
  186. return id;
  187. }
  188. public String getMoreinfo() {
  189. return moreinfo;
  190. }
  191. public int getNfoid() {
  192. return nfoid;
  193. }
  194. public String getNfolink() {
  195. return nfolink;
  196. }
  197. public String getPoster() {
  198. return poster;
  199. }
  200. public long getSize() {
  201. return size;
  202. }
  203. public String getTitle() {
  204. return title;
  205. }
  206. public int getViews() {
  207. return views;
  208. }
  209. }
  210. }