X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ftriggers%2FNewTorrentTrigger.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ftriggers%2FNewTorrentTrigger.java;h=a21a8d6ea943021bb412dc6b7d0c0743a4eb7dab;hb=6f69aff66ba5617d0bb27874014b4274bc551ab8;hp=0000000000000000000000000000000000000000;hpb=13a4fe6bece23b3dd561de657cf9bb7ea307e2b6;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java b/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java new file mode 100644 index 0000000..a21a8d6 --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewTorrentTrigger.java @@ -0,0 +1,130 @@ +/* + * Rhynodge - NewTorrentTrigger.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.rhynodge.triggers; + +import static com.google.common.base.Preconditions.checkState; + +import java.util.List; + +import net.pterodactylus.rhynodge.Reaction; +import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.Trigger; +import net.pterodactylus.rhynodge.output.DefaultOutput; +import net.pterodactylus.rhynodge.output.Output; +import net.pterodactylus.rhynodge.states.TorrentState; +import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; + +import org.apache.commons.lang3.StringEscapeUtils; + +import com.google.common.collect.Lists; + +/** + * {@link Trigger} implementation that is triggered by {@link TorrentFile}s that + * appear in the current {@link TorrentState} but not in the previous one. + * + * @author David ‘Bombe’ Roden + */ +public class NewTorrentTrigger implements Trigger { + + /** The newly detected torrent files. */ + private List torrentFiles = Lists.newArrayList(); + + // + // TRIGGER METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public boolean triggers(State currentState, State previousState) { + checkState(currentState instanceof TorrentState, "currentState is not a TorrentState but a %s", currentState.getClass().getName()); + checkState(previousState instanceof TorrentState, "previousState is not a TorrentState but a %s", currentState.getClass().getName()); + TorrentState currentTorrentState = (TorrentState) currentState; + TorrentState previousTorrentState = (TorrentState) previousState; + torrentFiles.clear(); + for (TorrentFile torrentFile : currentTorrentState) { + torrentFiles.add(torrentFile); + } + for (TorrentFile torrentFile : previousTorrentState) { + torrentFiles.remove(torrentFile); + } + return !torrentFiles.isEmpty(); + } + + /** + * {@inheritDoc} + */ + @Override + 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("", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri()))); + htmlText.append(String.format("", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri()))); + } + htmlText.append("
\n"); + htmlText.append("\n"); + return htmlText.toString(); + } + +}