X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Freactor%2Ftriggers%2FNewTorrentTrigger.java;h=ab640c6d2c8257b1764add67a3a0d16e8b997ce2;hb=13a4fe6bece23b3dd561de657cf9bb7ea307e2b6;hp=1eb224769451734b02710a1b8c6bcbd103b2ca95;hpb=61aad345c6ed96f7b5720eddcdd1fd60fba7189b;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java b/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java index 1eb2247..ab640c6 100644 --- a/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java +++ b/src/main/java/net/pterodactylus/reactor/triggers/NewTorrentTrigger.java @@ -21,11 +21,16 @@ import static com.google.common.base.Preconditions.checkState; import java.util.List; +import net.pterodactylus.reactor.Reaction; 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 +44,10 @@ public class NewTorrentTrigger implements Trigger { /** The newly detected torrent files. */ private List torrentFiles = Lists.newArrayList(); + // + // TRIGGER METHODS + // + /** * {@inheritDoc} */ @@ -62,8 +71,60 @@ public class NewTorrentTrigger implements Trigger { * {@inheritDoc} */ @Override - public Object trigger() { - return torrentFiles; + public Output output(Reaction reaction) { + DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", torrentFiles.size(), reaction.name())); + 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(" in ").append(torrentFile.fileCount()).append(" file(s)\n"); + plainText.append('\t').append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)\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(" in ").append(torrentFile.fileCount()).append(" file(s)
    "); + htmlText.append("
    ").append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)
    "); + htmlText.append(String.format("
    Magnet URI
    ", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri()))); + htmlText.append(String.format("
    Download URI
    ", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri()))); + } + htmlText.append("
\n"); + htmlText.append("\n"); + return htmlText.toString(); } }