Enhance trigger interface to allow merging states.
[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(newTorrentFiles));
93                 output.addText("text/html", getHtmlTextList(newTorrentFiles));
94                 return output;
95         }
96
97         //
98         // STATIC METHODS
99         //
100
101         /**
102          * Generates a plain text list of torrent files.
103          *
104          * @param torrentFiles
105          *            The torrent files to list
106          * @return The generated plain text
107          */
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');
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 torrentFiles
130          *            The torrent files to list
131          * @return The generated HTML
132          */
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())));
144                         }
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())));
147                         }
148                 }
149                 htmlText.append("</ul>\n");
150                 htmlText.append("</body></html>\n");
151                 return htmlText.toString();
152         }
153
154 }