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.Reaction;
25 import net.pterodactylus.reactor.State;
26 import net.pterodactylus.reactor.Trigger;
27 import net.pterodactylus.reactor.output.DefaultOutput;
28 import net.pterodactylus.reactor.output.Output;
29 import net.pterodactylus.reactor.states.TorrentState;
30 import net.pterodactylus.reactor.states.TorrentState.TorrentFile;
32 import org.apache.commons.lang3.StringEscapeUtils;
34 import com.google.common.collect.Lists;
37 * {@link Trigger} implementation that is triggered by {@link TorrentFile}s that
38 * appear in the current {@link TorrentState} but not in the previous one.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class NewTorrentTrigger implements Trigger {
44 /** The newly detected torrent files. */
45 private List<TorrentFile> torrentFiles = Lists.newArrayList();
55 public boolean triggers(State currentState, State previousState) {
56 checkState(currentState instanceof TorrentState, "currentState is not a TorrentState but a %s", currentState.getClass().getName());
57 checkState(previousState instanceof TorrentState, "previousState is not a TorrentState but a %s", currentState.getClass().getName());
58 TorrentState currentTorrentState = (TorrentState) currentState;
59 TorrentState previousTorrentState = (TorrentState) previousState;
61 for (TorrentFile torrentFile : currentTorrentState) {
62 torrentFiles.add(torrentFile);
64 for (TorrentFile torrentFile : previousTorrentState) {
65 torrentFiles.remove(torrentFile);
67 return !torrentFiles.isEmpty();
74 public Output output(Reaction reaction) {
75 DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", torrentFiles.size(), reaction.name()));
76 output.addText("text/plain", getPlainTextList(torrentFiles));
77 output.addText("text/html", getHtmlTextList(torrentFiles));
86 * Generates a plain text list of torrent files.
89 * The torrent files to list
90 * @return The generated plain text
92 private static String getPlainTextList(List<TorrentFile> torrentFiles) {
93 StringBuilder plainText = new StringBuilder();
94 plainText.append("New Torrents:\n\n");
95 for (TorrentFile torrentFile : torrentFiles) {
96 plainText.append(torrentFile.name()).append('\n');
97 plainText.append('\t').append(torrentFile.size()).append(" in ").append(torrentFile.fileCount()).append(" file(s)\n");
98 plainText.append('\t').append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)\n");
99 if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
100 plainText.append('\t').append(torrentFile.magnetUri()).append('\n');
102 if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
103 plainText.append('\t').append(torrentFile.downloadUri()).append('\n');
105 plainText.append('\n');
107 return plainText.toString();
111 * Generates an HTML list of the given torrent files.
113 * @param torrentFiles
114 * The torrent files to list
115 * @return The generated HTML
117 private static String getHtmlTextList(List<TorrentFile> torrentFiles) {
118 StringBuilder htmlText = new StringBuilder();
119 htmlText.append("<html><body>\n");
120 htmlText.append("<h1>New Torrents</h1>\n");
121 htmlText.append("<ul>\n");
122 for (TorrentFile torrentFile : torrentFiles) {
123 htmlText.append("<li><strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</strong></li>");
124 htmlText.append("<div>Size: <strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong> in <strong>").append(torrentFile.fileCount()).append("</strong> file(s)</div>");
125 htmlText.append("<div><strong>").append(torrentFile.seedCount()).append("</strong> seed(s), <strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>");
126 if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
127 htmlText.append(String.format("<div><a href=\"%s\">Magnet URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())));
129 if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
130 htmlText.append(String.format("<div><a href=\"%s\">Download URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())));
133 htmlText.append("</ul>\n");
134 htmlText.append("</body></html>\n");
135 return htmlText.toString();