From e371f97ebb6eaa268326bfc678013dfc96378918 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 25 Feb 2013 07:00:22 +0100 Subject: [PATCH] Add strips to comics. --- .../rhynodge/filters/ComicSiteFilter.java | 8 +- .../pterodactylus/rhynodge/states/ComicState.java | 105 +++++++++++++++++---- .../rhynodge/triggers/NewComicTrigger.java | 16 +++- 3 files changed, 107 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java index fec856c..9b3dfcd 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/ComicSiteFilter.java @@ -25,6 +25,7 @@ import net.pterodactylus.rhynodge.Filter; import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.states.ComicState; import net.pterodactylus.rhynodge.states.ComicState.Comic; +import net.pterodactylus.rhynodge.states.ComicState.Strip; import net.pterodactylus.rhynodge.states.HtmlState; import com.google.common.base.Optional; @@ -49,12 +50,17 @@ public abstract class ComicSiteFilter implements Filter { /* extract comics. */ Optional title = extractTitle(htmlState.document()); List imageUrls = extractImageUrls(htmlState.document()); + List imageComments = extractImageComments(htmlState.document()); /* store comic, if found, into state. */ if (title.isPresent() && !imageUrls.isEmpty()) { Comic comic = new Comic(title.get()); + int imageCounter = 0; for (String imageUrl : imageUrls) { - comic.addImageUrl(imageUrl); + String imageComment = (imageCounter < imageComments.size()) ? imageComments.get(imageCounter) : ""; + Strip strip = new Strip(imageUrl, imageComment); + imageCounter++; + comic.add(strip); } comicState.add(comic); } diff --git a/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java b/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java index fc47de6..ffc38b5 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java @@ -81,15 +81,15 @@ public class ComicState extends AbstractState implements Iterable { * * @author David ‘Bombe’ Roden */ - public static class Comic implements Iterable { + public static class Comic implements Iterable { /** The title of the comic. */ @JsonProperty private final String title; - /** The URLs of the comic’s images. */ + /** The strips of the comic. */ @JsonProperty - private final List imageUrls = Lists.newArrayList(); + private final List strips = Lists.newArrayList(); /** * Creates a new comic with the given title. @@ -111,23 +111,23 @@ public class ComicState extends AbstractState implements Iterable { } /** - * Returns the URLs of this comic’s images. + * Returns the strips of this comic. * - * @return The URLs of this comic’s images + * @return The strips of this comic */ - public List imageUrls() { - return imageUrls; + public List strips() { + return strips; } /** - * Adds an image URL to this comic. + * Adds a strip to this comic. * - * @param imageUrl - * The URL of the comic image to add + * @param strip + * The strip to add * @return This comic */ - public Comic addImageUrl(String imageUrl) { - imageUrls.add(imageUrl); + public Comic add(Strip strip) { + strips.add(strip); return this; } @@ -136,8 +136,8 @@ public class ComicState extends AbstractState implements Iterable { // @Override - public Iterator iterator() { - return imageUrls.iterator(); + public Iterator iterator() { + return strips.iterator(); } // @@ -146,7 +146,7 @@ public class ComicState extends AbstractState implements Iterable { @Override public int hashCode() { - return title.hashCode() ^ imageUrls().hashCode(); + return title.hashCode() ^ strips().hashCode(); } @Override @@ -155,12 +155,83 @@ public class ComicState extends AbstractState implements Iterable { return false; } Comic comic = (Comic) object; - return title().equals(comic.title()) && imageUrls().equals(comic.imageUrls()); + return title().equals(comic.title()) && strips().equals(comic.strips()); + } + + @Override + public String toString() { + return String.format("Comic[title=%s,strips=%s]", title(), strips()); + } + + } + + /** + * A strip is a single image that belongs to a comic. + * + * @author David ‘Bombe’ Roden + */ + public static class Strip { + + /** The URL of the image. */ + @JsonProperty + private final String imageUrl; + + /** The comment of the image. */ + @JsonProperty + private final String comment; + + /** + * Creates a new strip. + * + * @param imageUrl + * The URL of the image + * @param comment + * The comment of the image + */ + public Strip(@JsonProperty("imageUrl") String imageUrl, @JsonProperty("comment") String comment) { + this.imageUrl = imageUrl; + this.comment = comment; + } + + /** + * Returns the URL of the image. + * + * @return The URL of the image + */ + public String imageUrl() { + return imageUrl; + } + + /** + * Returns the comment of the image. + * + * @return The comment of the image + */ + public String comment() { + return comment; + } + + // + // OBJECT METHODS + // + + @Override + public int hashCode() { + return imageUrl().hashCode() ^ comment().hashCode(); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof Strip)) { + return false; + } + Strip strip = (Strip) object; + return imageUrl().equals(strip.imageUrl()) && comment().equals(strip.comment()); } @Override public String toString() { - return String.format("Comic[title=%s,imageUrls=%s]", title(), imageUrls()); + return String.format("Strip[imageUrl=%s,comment=%s]", imageUrl(), comment()); } } diff --git a/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java b/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java index c8852c7..45c290f 100644 --- a/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java @@ -28,9 +28,11 @@ import net.pterodactylus.rhynodge.output.DefaultOutput; import net.pterodactylus.rhynodge.output.Output; import net.pterodactylus.rhynodge.states.ComicState; import net.pterodactylus.rhynodge.states.ComicState.Comic; +import net.pterodactylus.rhynodge.states.ComicState.Strip; import com.google.common.collect.Lists; import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.lang3.StringUtils; /** * {@link Trigger} implementation that detects the presence of new {@link @@ -97,8 +99,11 @@ public class NewComicTrigger implements Trigger { for (Comic newComic : newComics) { text.append("Comic Found: ").append(newComic.title()).append("\n\n"); - for (String imageUrl : newComic) { - text.append("Image: ").append(imageUrl).append("\n"); + for (Strip strip : newComic) { + text.append("Image: ").append(strip.imageUrl()).append("\n"); + if (!StringUtils.isBlank(strip.comment())) { + text.append("Comment: ").append(strip.comment()).append("\n"); + } } text.append("\n\n"); } @@ -117,8 +122,11 @@ public class NewComicTrigger implements Trigger { for (Comic newComic : newComics) { html.append("

").append(StringEscapeUtils.escapeHtml4(newComic.title())).append("

\n"); - for (String imageUrl : newComic) { - html.append("
\n"); + for (Strip strip : newComic) { + html.append("
\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
\n"); } } -- 2.7.4