Change comments.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewTorrentTrigger.java
1 /*
2  * Rhynodge - NewTorrentTrigger.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.rhynodge.triggers;
19
20 import static com.google.common.base.Preconditions.checkState;
21
22 import java.util.List;
23 import java.util.Set;
24
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;
32
33 import org.apache.commons.lang3.StringEscapeUtils;
34
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Sets;
37
38 /**
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.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class NewTorrentTrigger implements Trigger {
45
46         /** All known torrents. */
47         private final Set<TorrentFile> allTorrentFiles = Sets.newHashSet();
48
49         /** The newly detected torrent files. */
50         private final List<TorrentFile> newTorrentFiles = Lists.newArrayList();
51
52         //
53         // TRIGGER METHODS
54         //
55
56         /**
57          * {@inheritDocs}
58          */
59         @Override
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;
65
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);
72                         }
73                 }
74
75                 return new TorrentState(allTorrentFiles);
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         @Override
82         public boolean triggers() {
83                 return !newTorrentFiles.isEmpty();
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         @Override
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));
94                 return output;
95         }
96
97         //
98         // PRIVATE METHODS
99         //
100
101         /**
102          * Generates a plain text list of torrent files.
103          *
104          * @param reaction
105          *            The reaction that was triggered
106          * @return The generated plain text
107          */
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');
117                         }
118                         if ((torrentFile.downloadUri() != null) && (torrentFile.downloadUri().length() > 0)) {
119                                 plainText.append('\t').append(torrentFile.downloadUri()).append('\n');
120                         }
121                         plainText.append('\n');
122                 }
123                 return plainText.toString();
124         }
125
126         /**
127          * Generates an HTML list of the given torrent files.
128          *
129          * @param reaction
130          *            The reaction that was triggered
131          * @return The generated HTML
132          */
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;\">");
152                         } else {
153                                 htmlBuilder.append("<tr>");
154                         }
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");
163                 }
164                 htmlBuilder.append("</tbody>\n");
165                 htmlBuilder.append("</table>\n");
166                 htmlBuilder.append("</body></html>\n");
167                 return htmlBuilder.toString();
168         }
169
170 }