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(reaction));
93 output.addText("text/html", getHtmlTextList(reaction));
102 * Generates a plain text list of torrent files.
105 * The reaction that was triggered
106 * @return The generated plain text
108 private String getPlainTextList(Reaction reaction) {
109 StringBuilder plainText = new StringBuilder();
110 plainText.append("New Torrents:\n\n");
111 for (TorrentFile torrentFile : newTorrentFiles) {
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.
130 * The reaction that was triggered
131 * @return The generated HTML
133 private String getHtmlTextList(Reaction reaction) {
134 StringBuilder htmlBuilder = new StringBuilder();
135 htmlBuilder.append("<html><body>\n");
136 htmlBuilder.append("<table>\n<caption>All Known Torrents</caption>\n");
137 htmlBuilder.append("<thead>\n");
138 htmlBuilder.append("<tr>");
139 htmlBuilder.append("<th>Filename</th>");
140 htmlBuilder.append("<th>Size</th>");
141 htmlBuilder.append("<th>File(s)</th>");
142 htmlBuilder.append("<th>Seeds</th>");
143 htmlBuilder.append("<th>Leechers</th>");
144 htmlBuilder.append("<th>Magnet</th>");
145 htmlBuilder.append("<th>Download</th>");
146 htmlBuilder.append("</tr>\n");
147 htmlBuilder.append("</thead>\n");
148 htmlBuilder.append("<tbody>\n");
149 for (TorrentFile torrentFile : allTorrentFiles) {
150 if (newTorrentFiles.contains(torrentFile)) {
151 htmlBuilder.append("<tr style=\"color: #008000; font-weight: bold;\">");
153 htmlBuilder.append("<tr>");
155 htmlBuilder.append("<td>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</td>");
156 htmlBuilder.append("<td>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</td>");
157 htmlBuilder.append("<td>").append(torrentFile.fileCount()).append("</td>");
158 htmlBuilder.append("<td>").append(torrentFile.seedCount()).append("</td>");
159 htmlBuilder.append("<td>").append(torrentFile.leechCount()).append("</td>");
160 htmlBuilder.append("<td><a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())).append("\">Link</a></td>");
161 htmlBuilder.append("<td><a href=\"").append(StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())).append("\">Link</a></td>");
162 htmlBuilder.append("</tr>\n");
164 htmlBuilder.append("</tbody>\n");
165 htmlBuilder.append("</table>\n");
166 htmlBuilder.append("</body></html>\n");
167 return htmlBuilder.toString();