Add strips to comics.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewComicTrigger.java
1 /*
2  * rhynodge - NewComicTrigger.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.*;
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.ComicState;
30 import net.pterodactylus.rhynodge.states.ComicState.Comic;
31 import net.pterodactylus.rhynodge.states.ComicState.Strip;
32
33 import com.google.common.collect.Lists;
34 import org.apache.commons.lang3.StringEscapeUtils;
35 import org.apache.commons.lang3.StringUtils;
36
37 /**
38  * {@link Trigger} implementation that detects the presence of new {@link
39  * Comic}s in a {@link ComicState}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class NewComicTrigger implements Trigger {
44
45         /** The new comics. */
46         private final List<Comic> newComics = Lists.newArrayList();
47
48         @Override
49         public State mergeStates(State previousState, State currentState) {
50                 checkArgument(previousState instanceof ComicState, "previous state must be a comic state");
51                 checkArgument(currentState instanceof ComicState, "current state must be a comic state");
52
53                 ComicState previousComicState = (ComicState) previousState;
54                 ComicState currentComicState = (ComicState) currentState;
55
56                 /* copy old state into new state. */
57                 ComicState mergedComicState = new ComicState();
58                 for (Comic comic : previousComicState) {
59                         mergedComicState.add(comic);
60                 }
61
62                 newComics.clear();
63                 for (Comic comic : currentComicState) {
64                         if (!mergedComicState.comics().contains(comic)) {
65                                 newComics.add(comic);
66                                 mergedComicState.add(comic);
67                         }
68                 }
69
70                 return mergedComicState;
71         }
72
73         @Override
74         public boolean triggers() {
75                 return !newComics.isEmpty();
76         }
77
78         @Override
79         public Output output(Reaction reaction) {
80                 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
81
82                 output.addText("text/plain", generatePlainText());
83                 output.addText("text/html", generateHtmlText());
84
85                 return output;
86         }
87
88         //
89         // PRIVATE METHODS
90         //
91
92         /**
93          * Generates a list of the new comics in plain text format.
94          *
95          * @return The list of new comics in plain text format
96          */
97         private String generatePlainText() {
98                 StringBuilder text = new StringBuilder();
99
100                 for (Comic newComic : newComics) {
101                         text.append("Comic Found: ").append(newComic.title()).append("\n\n");
102                         for (Strip strip : newComic) {
103                                 text.append("Image: ").append(strip.imageUrl()).append("\n");
104                                 if (!StringUtils.isBlank(strip.comment())) {
105                                         text.append("Comment: ").append(strip.comment()).append("\n");
106                                 }
107                         }
108                         text.append("\n\n");
109                 }
110
111                 return text.toString();
112         }
113
114         /**
115          * Generates a list of new comics in HTML format.
116          *
117          * @return The list of new comics in HTML format
118          */
119         private String generateHtmlText() {
120                 StringBuilder html = new StringBuilder();
121                 html.append("<body>");
122
123                 for (Comic newComic : newComics) {
124                         html.append("<h1>").append(StringEscapeUtils.escapeHtml4(newComic.title())).append("</h1>\n");
125                         for (Strip strip : newComic) {
126                                 html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
127                                 html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
128                                 html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
129                                 html.append("\"></div>\n");
130                         }
131                 }
132
133                 return html.append("</body>").toString();
134         }
135
136 }