Print the strip’s comment right below the strip for everyone to see.
[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.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import net.pterodactylus.rhynodge.Reaction;
27 import net.pterodactylus.rhynodge.State;
28 import net.pterodactylus.rhynodge.Trigger;
29 import net.pterodactylus.rhynodge.output.DefaultOutput;
30 import net.pterodactylus.rhynodge.output.Output;
31 import net.pterodactylus.rhynodge.states.ComicState;
32 import net.pterodactylus.rhynodge.states.ComicState.Comic;
33 import net.pterodactylus.rhynodge.states.ComicState.Strip;
34
35 import com.google.common.collect.Lists;
36 import org.apache.commons.lang3.StringEscapeUtils;
37 import org.apache.commons.lang3.StringUtils;
38
39 /**
40  * {@link Trigger} implementation that detects the presence of new {@link
41  * Comic}s in a {@link ComicState}.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class NewComicTrigger implements Trigger {
46
47         /** The new comics. */
48         private final List<Comic> newComics = Lists.newArrayList();
49
50         /** The latest comic state. */
51         private ComicState mergedComicState;
52
53         @Override
54         public State mergeStates(State previousState, State currentState) {
55                 checkArgument(previousState instanceof ComicState, "previous state must be a comic state");
56                 checkArgument(currentState instanceof ComicState, "current state must be a comic state");
57
58                 ComicState previousComicState = (ComicState) previousState;
59                 ComicState currentComicState = (ComicState) currentState;
60
61                 /* copy old state into new state. */
62                 mergedComicState = new ComicState();
63                 for (Comic comic : previousComicState) {
64                         mergedComicState.add(comic);
65                 }
66
67                 newComics.clear();
68                 for (Comic comic : currentComicState) {
69                         if (!mergedComicState.comics().contains(comic)) {
70                                 newComics.add(comic);
71                                 mergedComicState.add(comic);
72                         }
73                 }
74
75                 return mergedComicState;
76         }
77
78         @Override
79         public boolean triggers() {
80                 return !newComics.isEmpty();
81         }
82
83         @Override
84         public Output output(Reaction reaction) {
85                 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
86
87                 output.addText("text/plain", generatePlainText());
88                 output.addText("text/html", generateHtmlText());
89
90                 return output;
91         }
92
93         //
94         // PRIVATE METHODS
95         //
96
97         /**
98          * Generates a list of the new comics in plain text format.
99          *
100          * @return The list of new comics in plain text format
101          */
102         private String generatePlainText() {
103                 StringBuilder text = new StringBuilder();
104
105                 for (Comic newComic : newComics) {
106                         text.append("Comic Found: ").append(newComic.title()).append("\n\n");
107                         for (Strip strip : newComic) {
108                                 text.append("Image: ").append(strip.imageUrl()).append("\n");
109                                 if (!StringUtils.isBlank(strip.comment())) {
110                                         text.append("Comment: ").append(strip.comment()).append("\n");
111                                 }
112                         }
113                         text.append("\n\n");
114                 }
115
116                 return text.toString();
117         }
118
119         /**
120          * Generates a list of new comics in HTML format.
121          *
122          * @return The list of new comics in HTML format
123          */
124         private String generateHtmlText() {
125                 StringBuilder html = new StringBuilder();
126                 html.append("<body>");
127
128                 for (Comic newComic : newComics) {
129                         generateComicHtml(html, newComic);
130                 }
131
132                 List<Comic> latestComics = new ArrayList<Comic>(mergedComicState.comics());
133                 Collections.reverse(latestComics);
134                 int comicCount = 0;
135                 for (Comic comic : latestComics) {
136                         if (newComics.contains(comic)) {
137                                 continue;
138                         }
139                         generateComicHtml(html, comic);
140                         if (++comicCount == 7) {
141                                 break;
142                         }
143                 }
144
145                 return html.append("</body>").toString();
146         }
147
148         /**
149          * Generates the HTML for a single comic.
150          *
151          * @param html
152          *              The string builder to append the HTML to
153          * @param comic
154          *              The comic to render
155          */
156         private void generateComicHtml(StringBuilder html, Comic comic) {
157                 html.append("<h1>").append(StringEscapeUtils.escapeHtml4(comic.title())).append("</h1>\n");
158                 for (Strip strip : comic) {
159                         html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
160                         html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
161                         html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
162                         html.append("\"></div>\n");
163                         html.append("<div>").append(StringEscapeUtils.escapeHtml4(strip.comment())).append("</div>\n");
164                 }
165         }
166
167 }