Add plain text and HTML output to new-torrent trigger.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / triggers / NewTorrentTrigger.java
1 /*
2  * Reactor - 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.reactor.triggers;
19
20 import static com.google.common.base.Preconditions.checkState;
21
22 import java.util.List;
23
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;
30
31 import org.apache.commons.lang3.StringEscapeUtils;
32
33 import com.google.common.collect.Lists;
34
35 /**
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.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class NewTorrentTrigger implements Trigger {
42
43         /** The newly detected torrent files. */
44         private List<TorrentFile> torrentFiles = Lists.newArrayList();
45
46         //
47         // TRIGGER METHODS
48         //
49
50         /**
51          * {@inheritDoc}
52          */
53         @Override
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;
59                 torrentFiles.clear();
60                 for (TorrentFile torrentFile : currentTorrentState) {
61                         torrentFiles.add(torrentFile);
62                 }
63                 for (TorrentFile torrentFile : previousTorrentState) {
64                         torrentFiles.remove(torrentFile);
65                 }
66                 return !torrentFiles.isEmpty();
67         }
68
69         /**
70          * {@inheritDoc}
71          */
72         @Override
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));
77                 return output;
78         }
79
80         //
81         // STATIC METHODS
82         //
83
84         /**
85          * Generates a plain text list of torrent files.
86          *
87          * @param torrentFiles
88          *            The torrent files to list
89          * @return The generated plain text
90          */
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('\n');
97                         plainText.append('\t').append(torrentFile.magnetUri()).append('\n');
98                         plainText.append('\t').append(torrentFile.downloadUri()).append('\n');
99                         plainText.append('\n');
100                 }
101                 return plainText.toString();
102         }
103
104         /**
105          * Generates an HTML list of the given torrent files.
106          *
107          * @param torrentFiles
108          *            The torrent files to list
109          * @return The generated HTML
110          */
111         private static String getHtmlTextList(List<TorrentFile> torrentFiles) {
112                 StringBuilder htmlText = new StringBuilder();
113                 htmlText.append("<html><body>\n");
114                 htmlText.append("<h1>New Torrents</h1>\n");
115                 htmlText.append("<ul>\n");
116                 for (TorrentFile torrentFile : torrentFiles) {
117                         htmlText.append("<li><strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</strong></li>");
118                         htmlText.append("<div>Size: ").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</div>");
119                         htmlText.append(String.format("<div><a href=\"%s\">Magnet URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())));
120                         htmlText.append(String.format("<div><a href=\"%s\">Download URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())));
121                 }
122                 htmlText.append("</ul>\n");
123                 htmlText.append("</body></html>\n");
124                 return htmlText.toString();
125         }
126
127 }