123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
- package com.md87.charliebravo.commands;
-
- import com.dmdirc.util.Downloader;
- import com.dmdirc.util.MapList;
- import com.md87.charliebravo.Command;
- import com.md87.charliebravo.Followup;
- import com.md87.charliebravo.InputHandler;
- import com.md87.charliebravo.Response;
- import java.io.StringReader;
- import java.net.URLEncoder;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.Namespace;
- import org.jdom.input.SAXBuilder;
-
- /**
- *
- * @author chris
- */
- public class NewzbinCommand implements Command {
-
- protected static final String URL = "/search/query/?q=%s" +
- "&area=-1&fpn=p&searchaction=Go&areadone=-1&sort=date&order=desc" +
- "&feed=rss";
-
- protected static final String LOGIN_URL = "https://www.newzbin.com/account/login";
-
- @SuppressWarnings("unchecked")
- public void execute(InputHandler handler, Response response, String line) throws Exception {
- if (line.isEmpty()) {
- response.sendMessage("You need to specify a search query", true);
- } else {
- final Map<String, String> args = new HashMap<String, String>();
- args.put("ret_url", String.format(URL,
- URLEncoder.encode(line, Charset.defaultCharset().name())));
- args.put("username", "dataforce");
- args.put("password", "dfrox");
- final List<String> result = Downloader.getPage(LOGIN_URL, args);
- final StringBuilder data = new StringBuilder();
-
- for (String resultline : result) {
- data.append(resultline);
- data.append('\n');
- }
-
- System.out.println(data);
-
- final Document document = new SAXBuilder().build(new StringReader(data.toString()));
- final List<Result> results = new LinkedList<Result>();
-
- for (Element item : (List<Element>) document.getRootElement()
- .getChild("channel").getChildren("item")) {
- results.add(new Result(item));
- }
-
- response.sendMessage("there " + (results.size() == 1 ? "was" : "were")
- + " " + results.size() + " result" + (results.size() == 1 ? "s" : "")
- + " returned." + (results.size() == 1 ? "It is: " : "The first one is: ")
- + results.get(0).getSummary());
- response.addFollowup(new AttributesFollowup(results.get(0)));
- response.addFollowup(new NextFollowup(results, 1));
- }
-
- }
-
- protected static class NextFollowup implements Followup {
-
- private final List<Result> results;
- private final int next;
-
- public NextFollowup(List<Result> results, int next) {
- this.results = results;
- this.next = next;
- }
-
- public boolean matches(String line) {
- return next < results.size() && line.equalsIgnoreCase("next");
- }
-
- public void execute(InputHandler handler, Response response, String line) throws Exception {
- response.sendMessage("result " + next + "/" + results.size() + "is: "
- + results.get(next).getSummary());
- response.addFollowup(new AttributesFollowup(results.get(next)));
- response.addFollowup(new NextFollowup(results, next + 1));
- response.addFollowup(new PreviousFollowup(results, next - 1));
- }
-
- }
-
- protected static class PreviousFollowup implements Followup {
-
- private final List<Result> results;
- private final int prev;
-
- public PreviousFollowup(List<Result> results, int next) {
- this.results = results;
- this.prev = next;
- }
-
- public boolean matches(String line) {
- return prev > 0 && line.equalsIgnoreCase("previous");
- }
-
- public void execute(InputHandler handler, Response response, String line) throws Exception {
- response.sendMessage("result " + prev + "/" + results.size() + "is: "
- + results.get(prev).getSummary());
- response.addFollowup(new AttributesFollowup(results.get(prev)));
- response.addFollowup(new NextFollowup(results, prev + 1));
- response.addFollowup(new PreviousFollowup(results, prev - 1));
- }
-
- }
-
- protected static class AttributesFollowup implements Followup {
-
- private final Result result;
-
- public AttributesFollowup(Result result) {
- this.result = result;
- }
-
- public boolean matches(String line) {
- return line.equalsIgnoreCase("attributes");
- }
-
- public void execute(InputHandler handler, Response response, String line) throws Exception {
- final StringBuilder builder = new StringBuilder();
-
- for (Map.Entry<String, List<String>> entry : result.getAttributes().entrySet()) {
- if (builder.length() > 0) {
- builder.append("; ");
- }
-
- builder.append(entry.getKey() + ": ");
-
- boolean first = true;
- for (String value : entry.getValue()) {
- if (first) {
- first = false;
- } else {
- builder.append(", ");
- }
-
- builder.append(value);
- }
- }
-
- response.setInheritFollows(true);
- response.sendMessage("that result has the following attributes: " + builder.toString());
- }
-
- }
-
- protected static class Result {
-
- protected static final String REPORT_NS = "http://www.newzbin.com/DTD/2007/feeds/report/";
- protected static final String NZB_URL = "http://www.newzbin.com/browse/post/%s/nzb/";
-
- private final String title, category, moreinfo, nfolink, poster, date;
- private final MapList<String, String> attributes = new MapList<String, String>();
- private final List<String> groups = new ArrayList<String>();
- private final int id, nfoid, views, comments;
- private final long size;
-
- public Result(final Element element) {
- final Namespace namespace = Namespace.getNamespace(REPORT_NS);
-
- title = element.getChildTextTrim("title");
- id = Integer.parseInt(element.getChildTextTrim("id", namespace));
- category = element.getChildTextTrim("category", namespace);
-
- for (Object attribute : element.getChildren("attributes", namespace)) {
- final String type = ((Element) attribute).getAttributeValue("type");
- final String value = ((Element) attribute).getTextTrim();
- attributes.add(type, value);
- }
-
- for (Object group : element.getChildren("groups", namespace)) {
- groups.add(((Element) group).getTextTrim());
- }
-
- moreinfo = element.getChildTextTrim("moreinfo", namespace);
-
- nfoid = Integer.parseInt(element.getChild("nfo", namespace)
- .getChildText("fileid", namespace));
- nfolink = element.getChild("nfo", namespace)
- .getChildText("link", namespace);
- poster = element.getChildText("poster", namespace);
- size = Long.parseLong(element.getChildTextTrim("size", namespace));
- date = element.getChildText("postdate", namespace);
- views = Integer.parseInt(element.getChild("stats", namespace)
- .getChildText("views", namespace));
- comments = Integer.parseInt(element.getChild("stats", namespace)
- .getChildText("comments", namespace));
- }
-
- public String getSummary() {
- return "'" + title + "' (" + getSizeMB() + "MiB), nzb link: "
- + String.format(NZB_URL, id);
- }
-
- public String getSizeMB() {
- return String.format("%,.2f", size / (1024 * 1024));
- }
-
- public MapList<String, String> getAttributes() {
- return attributes;
- }
-
- public String getCategory() {
- return category;
- }
-
- public int getComments() {
- return comments;
- }
-
- public String getDate() {
- return date;
- }
-
- public List<String> getGroups() {
- return groups;
- }
-
- public int getId() {
- return id;
- }
-
- public String getMoreinfo() {
- return moreinfo;
- }
-
- public int getNfoid() {
- return nfoid;
- }
-
- public String getNfolink() {
- return nfolink;
- }
-
- public String getPoster() {
- return poster;
- }
-
- public long getSize() {
- return size;
- }
-
- public String getTitle() {
- return title;
- }
-
- public int getViews() {
- return views;
- }
-
- }
-
- }
|