package net.pterodactylus.rhynodge.filters;
-import static com.google.common.base.Preconditions.*;
+import static com.google.common.base.Preconditions.checkArgument;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.List;
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;
/* extract comics. */
Optional<String> title = extractTitle(htmlState.document());
List<String> imageUrls = extractImageUrls(htmlState.document());
+ List<String> 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) : "";
+ try {
+ URI stripUri = new URI(htmlState.uri()).resolve(imageUrl);
+ Strip strip = new Strip(stripUri.toString(), imageComment);
+ imageCounter++;
+ comic.add(strip);
+ } catch (URISyntaxException use1) {
+ throw new IllegalStateException(String.format("Could not resolve image URL “%s” against base URL “%s”.", imageUrl, htmlState.uri()), use1);
+ }
}
+ comicState.add(comic);
}
return comicState;
*/
protected abstract List<String> extractImageUrls(Document document);
+ /**
+ * Extracts the image comments from the given document. The elements of this
+ * last and of the list returned by {@link #extractImageUrls(org.jsoup.nodes.Document)}
+ * are paired up and added as {@link Strip}s. If the list returned by this
+ * method has less elements, an empty string is used for the remaining images.
+ *
+ * @param document
+ * The document to extract the image comments from
+ * @return The extracted image comments
+ */
+ protected abstract List<String> extractImageComments(Document document);
+
}