From 18db828b7548bb8a030b64c80bf9e4b81f669b95 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 3 Jan 2013 16:38:51 +0100 Subject: [PATCH] Add plain text and HTML output to new-torrent trigger. --- pom.xml | 5 ++ .../reactor/triggers/NewTorrentTrigger.java | 57 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/pom.xml b/pom.xml index c68a158..643cdb5 100644 --- a/pom.xml +++ b/pom.xml @@ -45,5 +45,10 @@ mail 1.4.6-rc1 + + org.apache.commons + commons-lang3 + 3.1 + diff --git a/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java index f655ff0..65a215e 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java @@ -23,9 +23,13 @@ import java.util.List; import net.pterodactylus.reactor.State; import net.pterodactylus.reactor.Trigger; +import net.pterodactylus.reactor.output.DefaultOutput; +import net.pterodactylus.reactor.output.Output; import net.pterodactylus.reactor.states.TorrentState; import net.pterodactylus.reactor.states.TorrentState.TorrentFile; +import org.apache.commons.lang3.StringEscapeUtils; + import com.google.common.collect.Lists; /** @@ -39,6 +43,10 @@ public class NewTorrentTrigger implements Trigger { /** The newly detected torrent files. */ private List torrentFiles = Lists.newArrayList(); + // + // TRIGGER METHODS + // + /** * {@inheritDoc} */ @@ -64,7 +72,56 @@ public class NewTorrentTrigger implements Trigger { @Override public Output output() { DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s)!", torrentFiles.size())); + output.addText("text/plain", getPlainTextList(torrentFiles)); + output.addText("text/html", getHtmlTextList(torrentFiles)); return output; } + // + // STATIC METHODS + // + + /** + * Generates a plain text list of torrent files. + * + * @param torrentFiles + * The torrent files to list + * @return The generated plain text + */ + private static String getPlainTextList(List torrentFiles) { + StringBuilder plainText = new StringBuilder(); + plainText.append("New Torrents:\n\n"); + for (TorrentFile torrentFile : torrentFiles) { + plainText.append(torrentFile.name()).append('\n'); + plainText.append('\t').append(torrentFile.size()).append('\n'); + plainText.append('\t').append(torrentFile.magnetUri()).append('\n'); + plainText.append('\t').append(torrentFile.downloadUri()).append('\n'); + plainText.append('\n'); + } + return plainText.toString(); + } + + /** + * Generates an HTML list of the given torrent files. + * + * @param torrentFiles + * The torrent files to list + * @return The generated HTML + */ + private static String getHtmlTextList(List torrentFiles) { + StringBuilder htmlText = new StringBuilder(); + htmlText.append("\n"); + htmlText.append("

New Torrents

\n"); + htmlText.append("
    \n"); + for (TorrentFile torrentFile : torrentFiles) { + htmlText.append("
  • ").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("
  • "); + htmlText.append("
    Size: ").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("
    "); + htmlText.append(String.format("", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri()))); + htmlText.append(String.format("", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri()))); + } + htmlText.append("
\n"); + htmlText.append("\n"); + return htmlText.toString(); + } + } -- 2.7.4