From: David ‘Bombe’ Roden Date: Tue, 10 Feb 2015 20:42:34 +0000 (+0100) Subject: Fix Cyanide & Happiness filter and add unit test X-Git-Tag: v2~205 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c9089f021f7715aa0215e076dfbb0c5da446b39a;p=rhynodge.git Fix Cyanide & Happiness filter and add unit test --- diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/comics/CyanideAndHappinessComicFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/comics/CyanideAndHappinessComicFilter.java index b51e959..8cceb5c 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/comics/CyanideAndHappinessComicFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/comics/CyanideAndHappinessComicFilter.java @@ -17,6 +17,9 @@ package net.pterodactylus.rhynodge.filters.comics; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.util.Collections; import java.util.List; @@ -39,17 +42,27 @@ public class CyanideAndHappinessComicFilter extends ComicSiteFilter { @Override protected Optional extractTitle(Document document) { - return Optional.of(""); + return Optional.absent(); } @Override protected List extractImageUrls(Document document) { - Elements imageTags = document.select("img[alt=Cyanide and Happiness, a daily webcomic]"); + Elements imageTags = document.select("img#main-comic"); return FluentIterable.from(imageTags).transform(new Function() { @Override public String apply(Element input) { - return input.attr("src"); + String imageUrl = input.attr("src"); + try { + return new URI(document.baseUri()).resolve(imageUrl).toString(); + } catch (URISyntaxException e) { + /* ignore. */ + } + if (!imageUrl.startsWith("/")) { + return imageUrl; + } + String protocol = document.baseUri().substring(0, document.baseUri().indexOf('/')); + return protocol + imageUrl; } }).toList(); } diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/comics/CyanideAndHappinessComicFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/comics/CyanideAndHappinessComicFilterTest.java new file mode 100644 index 0000000..2d7a118 --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/comics/CyanideAndHappinessComicFilterTest.java @@ -0,0 +1,40 @@ +package net.pterodactylus.rhynodge.filters.comics; + +import java.io.IOException; + +import com.google.common.base.Optional; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.jsoup.nodes.Document; +import org.junit.Test; + +/** + * Unit test for {@link CyanideAndHappinessComicFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class CyanideAndHappinessComicFilterTest { + + private final CyanideAndHappinessComicFilter comicSiteFilter = new CyanideAndHappinessComicFilter(); + private final Document document; + + public CyanideAndHappinessComicFilterTest() throws IOException { + document = ComicLoader.loadDocument("cyanide-and-happiness.html", "http://www.explosm.net/comics/new/"); + } + + @Test + public void filterCanParseComicTitle() { + MatcherAssert.assertThat(comicSiteFilter.extractTitle(document), Matchers.is(Optional.absent())); + } + + @Test + public void filterCanExtractImageUrls() { + MatcherAssert.assertThat(comicSiteFilter.extractImageUrls(document), Matchers.contains("http://files.explosm.net/comics/Dave/moneyhappiness.png")); + } + + @Test + public void filterExtractNoImageComments() { + MatcherAssert.assertThat(comicSiteFilter.extractImageComments(document), Matchers.empty()); + } + +} diff --git a/src/test/resources/net/pterodactylus/rhynodge/filters/comics/cyanide-and-happiness.html b/src/test/resources/net/pterodactylus/rhynodge/filters/comics/cyanide-and-happiness.html new file mode 100644 index 0000000..dee38e2 --- /dev/null +++ b/src/test/resources/net/pterodactylus/rhynodge/filters/comics/cyanide-and-happiness.html @@ -0,0 +1,480 @@ + + + +Explosm.net - Home of Cyanide and Happiness + + + + + + + + + + + + + + + + + + +
+
+
+
+
+ +
+
+
    +
  • +
  • +
  • +
  • +
  • +
  • +
+
+
+
+
+
+
+Register  |  Sign In +
+
+ +
+ +
+
+
+
+ +
+ +
+ +
+ +
+
+
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+

More from Explosm

+
+
+

Loading...

+
+
+
+  +
+

Sign in to post a comment

+
+

Loading...

+
+
+
+
+
+
+

Advertisement

+
+ + + +
+
+
+
+
+
+
+

Did you know by creating an account you can favorite comics and shorts, automatically bookmark the last one you saw, and more? Learn more about it here!

+
+ +
+
+ + + + + +
+
+
+
+
+
+

Log in or Register

+
+
+ +
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ +
+
+ +
+
+
+
+
+
+
+
+Don't have an account? Join the cool kids here! +
+
+
+
+
+ +
+ +
+
+ + + + + + + + +