2 * rhynodge - NewComicTrigger.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.rhynodge.triggers;
20 import static com.google.common.base.Preconditions.*;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
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;
35 import com.google.common.collect.Lists;
36 import org.apache.commons.lang3.StringEscapeUtils;
37 import org.apache.commons.lang3.StringUtils;
40 * {@link Trigger} implementation that detects the presence of new {@link
41 * Comic}s in a {@link ComicState}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class NewComicTrigger implements Trigger {
47 /** The new comics. */
48 private final List<Comic> newComics = Lists.newArrayList();
50 /** The latest comic state. */
51 private ComicState mergedComicState;
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");
58 ComicState previousComicState = (ComicState) previousState;
59 ComicState currentComicState = (ComicState) currentState;
61 /* copy old state into new state. */
62 mergedComicState = new ComicState();
63 for (Comic comic : previousComicState) {
64 mergedComicState.add(comic);
68 for (Comic comic : currentComicState) {
69 if (!mergedComicState.comics().contains(comic)) {
71 mergedComicState.add(comic);
75 return mergedComicState;
79 public boolean triggers() {
80 return !newComics.isEmpty();
84 public Output output(Reaction reaction) {
85 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
87 output.addText("text/plain", generatePlainText());
88 output.addText("text/html", generateHtmlText());
98 * Generates a list of the new comics in plain text format.
100 * @return The list of new comics in plain text format
102 private String generatePlainText() {
103 StringBuilder text = new StringBuilder();
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");
116 return text.toString();
120 * Generates a list of new comics in HTML format.
122 * @return The list of new comics in HTML format
124 private String generateHtmlText() {
125 StringBuilder html = new StringBuilder();
126 html.append("<body>");
128 for (Comic newComic : newComics) {
129 generateComicHtml(html, newComic);
132 List<Comic> latestComics = new ArrayList<Comic>(mergedComicState.comics());
133 Collections.reverse(latestComics);
135 for (Comic comic : latestComics) {
136 if (newComics.contains(comic)) {
139 generateComicHtml(html, comic);
140 if (++comicCount == 7) {
145 return html.append("</body>").toString();
149 * Generates the HTML for a single comic.
152 * The string builder to append the HTML to
154 * The comic to render
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");