2 * Rhynodge - 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.rhynodge.triggers;
20 import static com.google.common.base.Preconditions.checkState;
22 import java.util.List;
25 import net.pterodactylus.rhynodge.Reaction;
26 import net.pterodactylus.rhynodge.State;
27 import net.pterodactylus.rhynodge.Trigger;
28 import net.pterodactylus.rhynodge.output.DefaultOutput;
29 import net.pterodactylus.rhynodge.output.Output;
30 import net.pterodactylus.rhynodge.states.TorrentState;
31 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
33 import org.apache.commons.lang3.StringEscapeUtils;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Sets;
39 * {@link Trigger} implementation that is triggered by {@link TorrentFile}s that
40 * appear in the current {@link TorrentState} but not in the previous one.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class NewTorrentTrigger implements Trigger {
46 /** All known torrents. */
47 private final Set<TorrentFile> allTorrentFiles = Sets.newHashSet();
49 /** The newly detected torrent files. */
50 private final List<TorrentFile> newTorrentFiles = Lists.newArrayList();
60 public State mergeStates(State previousState, State currentState) {
61 checkState(currentState instanceof TorrentState, "currentState is not a TorrentState but a %s", currentState.getClass().getName());
62 checkState(previousState instanceof TorrentState, "previousState is not a TorrentState but a %s", currentState.getClass().getName());
63 TorrentState currentTorrentState = (TorrentState) currentState;
64 TorrentState previousTorrentState = (TorrentState) previousState;
66 allTorrentFiles.clear();
67 newTorrentFiles.clear();
68 allTorrentFiles.addAll(previousTorrentState.torrentFiles());
69 for (TorrentFile torrentFile : currentTorrentState) {
70 if (allTorrentFiles.add(torrentFile)) {
71 newTorrentFiles.add(torrentFile);
75 return new TorrentState(allTorrentFiles);
82 public boolean triggers() {
83 return !newTorrentFiles.isEmpty();
90 public Output output(Reaction reaction) {
91 DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", newTorrentFiles.size(), reaction.name()));
92 output.addText("text/plain", getPlainTextList(newTorrentFiles));
93 output.addText("text/html", getHtmlTextList(newTorrentFiles));
102 * Generates a plain text list of torrent files.
104 * @param torrentFiles
105 * The torrent files to list
106 * @return The generated plain text
108 private static String getPlainTextList(List<TorrentFile> torrentFiles) {
109 StringBuilder plainText = new StringBuilder();
110 plainText.append("New Torrents:\n\n");
111 for (TorrentFile torrentFile : torrentFiles) {
112 plainText.append(torrentFile.name()).append('\n');
113 plainText.append('\t').append(torrentFile.size()).append(" in ").append(torrentFile.fileCount()).append(" file(s)\n");
114 plainText.append('\t').append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)\n");
115 if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
116 plainText.append('\t').append(torrentFile.magnetUri()).append('\n');
118 if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
119 plainText.append('\t').append(torrentFile.downloadUri()).append('\n');
121 plainText.append('\n');
123 return plainText.toString();
127 * Generates an HTML list of the given torrent files.
129 * @param torrentFiles
130 * The torrent files to list
131 * @return The generated HTML
133 private static String getHtmlTextList(List<TorrentFile> torrentFiles) {
134 StringBuilder htmlText = new StringBuilder();
135 htmlText.append("<html><body>\n");
136 htmlText.append("<h1>New Torrents</h1>\n");
137 htmlText.append("<ul>\n");
138 for (TorrentFile torrentFile : torrentFiles) {
139 htmlText.append("<li><strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</strong></li>");
140 htmlText.append("<div>Size: <strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong> in <strong>").append(torrentFile.fileCount()).append("</strong> file(s)</div>");
141 htmlText.append("<div><strong>").append(torrentFile.seedCount()).append("</strong> seed(s), <strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>");
142 if ((torrentFile.magnetUri() != null) && (torrentFile.magnetUri().length() > 0)) {
143 htmlText.append(String.format("<div><a href=\"%s\">Magnet URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())));
145 if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
146 htmlText.append(String.format("<div><a href=\"%s\">Download URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())));
149 htmlText.append("</ul>\n");
150 htmlText.append("</body></html>\n");
151 return htmlText.toString();