Rename project to “Rhynodge.”
[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
24 import net.pterodactylus.rhynodge.Reaction;
25 import net.pterodactylus.rhynodge.State;
26 import net.pterodactylus.rhynodge.Trigger;
27 import net.pterodactylus.rhynodge.output.DefaultOutput;
28 import net.pterodactylus.rhynodge.output.Output;
29 import net.pterodactylus.rhynodge.states.TorrentState;
30 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
31
32 import org.apache.commons.lang3.StringEscapeUtils;
33
34 import com.google.common.collect.Lists;
35
36 /**
37  * {@link Trigger} implementation that is triggered by {@link TorrentFile}s that
38  * appear in the current {@link TorrentState} but not in the previous one.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class NewTorrentTrigger implements Trigger {
43
44         /** The newly detected torrent files. */
45         private List<TorrentFile> torrentFiles = Lists.newArrayList();
46
47         //
48         // TRIGGER METHODS
49         //
50
51         /**
52          * {@inheritDoc}
53          */
54         @Override
55         public boolean triggers(State currentState, State previousState) {
56                 checkState(currentState instanceof TorrentState, "currentState is not a TorrentState but a %s", currentState.getClass().getName());
57                 checkState(previousState instanceof TorrentState, "previousState is not a TorrentState but a %s", currentState.getClass().getName());
58                 TorrentState currentTorrentState = (TorrentState) currentState;
59                 TorrentState previousTorrentState = (TorrentState) previousState;
60                 torrentFiles.clear();
61                 for (TorrentFile torrentFile : currentTorrentState) {
62                         torrentFiles.add(torrentFile);
63                 }
64                 for (TorrentFile torrentFile : previousTorrentState) {
65                         torrentFiles.remove(torrentFile);
66                 }
67                 return !torrentFiles.isEmpty();
68         }
69
70         /**
71          * {@inheritDoc}
72          */
73         @Override
74         public Output output(Reaction reaction) {
75                 DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", torrentFiles.size(), reaction.name()));
76                 output.addText("text/plain", getPlainTextList(torrentFiles));
77                 output.addText("text/html", getHtmlTextList(torrentFiles));
78                 return output;
79         }
80
81         //
82         // STATIC METHODS
83         //
84
85         /**
86          * Generates a plain text list of torrent files.
87          *
88          * @param torrentFiles
89          *            The torrent files to list
90          * @return The generated plain text
91          */
92         private static String getPlainTextList(List<TorrentFile> torrentFiles) {
93                 StringBuilder plainText = new StringBuilder();
94                 plainText.append("New Torrents:\n\n");
95                 for (TorrentFile torrentFile : torrentFiles) {
96                         plainText.append(torrentFile.name()).append('\n');
97                         plainText.append('\t').append(torrentFile.size()).append(" in ").append(torrentFile.fileCount()).append(" file(s)\n");
98                         plainText.append('\t').append(torrentFile.seedCount()).append(" seed(s), ").append(torrentFile.leechCount()).append(" leecher(s)\n");
99                         plainText.append('\t').append(torrentFile.magnetUri()).append('\n');
100                         plainText.append('\t').append(torrentFile.downloadUri()).append('\n');
101                         plainText.append('\n');
102                 }
103                 return plainText.toString();
104         }
105
106         /**
107          * Generates an HTML list of the given torrent files.
108          *
109          * @param torrentFiles
110          *            The torrent files to list
111          * @return The generated HTML
112          */
113         private static String getHtmlTextList(List<TorrentFile> torrentFiles) {
114                 StringBuilder htmlText = new StringBuilder();
115                 htmlText.append("<html><body>\n");
116                 htmlText.append("<h1>New Torrents</h1>\n");
117                 htmlText.append("<ul>\n");
118                 for (TorrentFile torrentFile : torrentFiles) {
119                         htmlText.append("<li><strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.name())).append("</strong></li>");
120                         htmlText.append("<div>Size: <strong>").append(StringEscapeUtils.escapeHtml4(torrentFile.size())).append("</strong> in <strong>").append(torrentFile.fileCount()).append("</strong> file(s)</div>");
121                         htmlText.append("<div><strong>").append(torrentFile.seedCount()).append("</strong> seed(s), <strong>").append(torrentFile.leechCount()).append("</strong> leecher(s)</div>");
122                         htmlText.append(String.format("<div><a href=\"%s\">Magnet URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.magnetUri())));
123                         htmlText.append(String.format("<div><a href=\"%s\">Download URI</a></div>", StringEscapeUtils.escapeHtml4(torrentFile.downloadUri())));
124                 }
125                 htmlText.append("</ul>\n");
126                 htmlText.append("</body></html>\n");
127                 return htmlText.toString();
128         }
129
130 }