2 * Reactor - NewTorrentTrigger.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.reactor.triggers;
20 import static com.google.common.base.Preconditions.checkState;
22 import java.util.List;
24 import net.pterodactylus.reactor.State;
25 import net.pterodactylus.reactor.Trigger;
26 import net.pterodactylus.reactor.output.DefaultOutput;
27 import net.pterodactylus.reactor.output.Output;
28 import net.pterodactylus.reactor.states.TorrentState;
29 import net.pterodactylus.reactor.states.TorrentState.TorrentFile;
31 import org.apache.commons.lang3.StringEscapeUtils;
33 import com.google.common.collect.Lists;
36 * {@link Trigger} implementation that is triggered by {@link TorrentFile}s that
37 * appear in the current {@link TorrentState} but not in the previous one.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class NewTorrentTrigger implements Trigger {
43 /** The newly detected torrent files. */
44 private List<TorrentFile> torrentFiles = Lists.newArrayList();
54 public boolean triggers(State currentState, State previousState) {
55 checkState(currentState instanceof TorrentState, "currentState is not a TorrentState but a %s", currentState.getClass().getName());
56 checkState(previousState instanceof TorrentState, "previousState is not a TorrentState but a %s", currentState.getClass().getName());
57 TorrentState currentTorrentState = (TorrentState) currentState;
58 TorrentState previousTorrentState = (TorrentState) previousState;
60 for (TorrentFile torrentFile : currentTorrentState) {
61 torrentFiles.add(torrentFile);
63 for (TorrentFile torrentFile : previousTorrentState) {
64 torrentFiles.remove(torrentFile);
66 return !torrentFiles.isEmpty();
73 public Output output() {
74 DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s)!", torrentFiles.size()));
75 output.addText("text/plain", getPlainTextList(torrentFiles));
76 output.addText("text/html", getHtmlTextList(torrentFiles));
85 * Generates a plain text list of torrent files.
88 * The torrent files to list
89 * @return The generated plain text
91 private static String getPlainTextList(List<TorrentFile> torrentFiles) {
92 StringBuilder plainText = new StringBuilder();
93 plainText.append("New Torrents:\n\n");
94 for (TorrentFile torrentFile : torrentFiles) {
95 plainText.append(torrentFile.name()).append('\n');
96 plainText.append('\t').append(torrentFile.size()).append(" in ").append(torrentFile.fileCount()).append(" file(s)\n");
97 plainText.append('\t').append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)\n");
98 plainText.append('\t').append(torrentFile.magnetUri()).append('\n');
99 plainText.append('\t').append(torrentFile.downloadUri()).append('\n');
100 plainText.append('\n');
102 return plainText.toString();
106 * Generates an HTML list of the given torrent files.
108 * @param torrentFiles
109 * The torrent files to list
110 * @return The generated HTML
112 private static String getHtmlTextList(List<TorrentFile> torrentFiles) {
113 StringBuilder htmlText = new StringBuilder();
114 htmlText.append("<html><body>\n");
115 htmlText.append("<h1>New Torrents</h1>\n");
116 htmlText.append("<ul>\n");
117 for (TorrentFile torrentFile : torrentFiles) {
118 htmlText.append("<li><strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</strong></li>");
119 htmlText.append("<div>Size: <strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong> in <strong>").append(torrentFile.fileCount()).append("</strong> file(s)</div>");
120 htmlText.append("<div><strong>").append(torrentFile.seedCount()).append("</strong> seed(s), <strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>");
121 htmlText.append(String.format("<div><a href=\"%s\">Magnet URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())));
122 htmlText.append(String.format("<div><a href=\"%s\">Download URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())));
124 htmlText.append("</ul>\n");
125 htmlText.append("</body></html>\n");
126 return htmlText.toString();