From 0ceb55ed4845ebd24c04b62671147fd76a901193 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 11 Nov 2017 10:37:11 +0100 Subject: [PATCH] Replace LICD filter with Kotlin version, add test --- .../filters/comics/LeastICouldDoComicFilter.java | 61 --- .../rhynodge/watchers/LeastICouldDoWatcher.java | 2 +- .../filters/comics/LeastICouldDoComicFilter.kt | 37 ++ .../filters/comics/LeastICouldDoComicFilterTest.kt | 33 ++ .../rhynodge/filters/comics/least-i-could-do.html | 595 +++++++++++++++++++++ 5 files changed, 666 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.java create mode 100644 src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.kt create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilterTest.kt create mode 100644 src/test/resources/net/pterodactylus/rhynodge/filters/comics/least-i-could-do.html diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.java deleted file mode 100644 index d45526a..0000000 --- a/src/main/java/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * rhynodge - LeastICouldDoComicFilter.java - Copyright © 2013 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.rhynodge.filters.comics; - -import java.util.Collections; -import java.util.List; - -import net.pterodactylus.rhynodge.filters.ComicSiteFilter; - -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.common.collect.FluentIterable; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; - -/** - * {@link ComicSiteFilter} implementation that can parse “Least I Could Do.” - * - * @author David ‘Bombe’ Roden - */ -public class LeastICouldDoComicFilter extends ComicSiteFilter { - - @Override - protected Optional extractTitle(Document document) { - return Optional.of(""); - } - - @Override - protected List extractImageUrls(Document document) { - Elements imageTag = document.select("#comic-full img"); - return FluentIterable.from(imageTag).transform(new Function() { - - @Override - public String apply(Element input) { - return input.attr("src"); - } - }).toList(); - } - - @Override - protected List extractImageComments(Document document) { - return Collections.emptyList(); - } - -} diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java index 406f1cb..d88f574 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java @@ -66,7 +66,7 @@ public class LeastICouldDoWatcher extends DefaultWatcher { @Override protected Optional extractUrl(Document document) { - Elements linkTag = document.select("a#feature-comic"); + Elements linkTag = document.select("a#latest-comic"); return linkTag.hasAttr("href") ? Optional.of(linkTag.attr("href")) : Optional.absent(); } }); diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.kt b/src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.kt new file mode 100644 index 0000000..4f29a23 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilter.kt @@ -0,0 +1,37 @@ +/* + * rhynodge - LeastICouldDoComicFilter.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.rhynodge.filters.comics + +import com.google.common.base.Optional +import net.pterodactylus.rhynodge.filters.ComicSiteFilter +import org.jsoup.nodes.Document + +/** + * [ComicSiteFilter] implementation that can parse “Least I Could Do.” + */ +class LeastICouldDoComicFilter : ComicSiteFilter() { + + override fun extractTitle(document: Document) = Optional.of("")!! + + override fun extractImageUrls(document: Document) = + document.select("#content-comic img.comic") + .map { it.attr("src") } + + override fun extractImageComments(document: Document) = emptyList() + +} diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilterTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilterTest.kt new file mode 100644 index 0000000..0d88973 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/rhynodge/filters/comics/LeastICouldDoComicFilterTest.kt @@ -0,0 +1,33 @@ +package net.pterodactylus.rhynodge.filters.comics + +import net.pterodactylus.rhynodge.filters.ResourceLoader +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 org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.junit.Test + +/** + * Unit test for [LeastICouldDoComicFilterTest]. + */ +class LeastICouldDoComicFilterTest { + + private val filter = LeastICouldDoComicFilter() + private val htmlState: HtmlState = ResourceLoader + .loadDocument(javaClass, "least-i-could-do.html", baseUrl) + .let { HtmlState(baseUrl, it) } + + @Test + fun `comic is extracted correctly`() { + val comicState = filter.filter(htmlState) as ComicState + assertThat(comicState.comics(), contains( + Comic(""). + add(Strip("http://leasticoulddo.com/wp-content/uploads/2017/11/528B9685-DA9C-4320-8AC6-C6871DF81C3F.jpeg", "")) + )) + } + +} + +private val baseUrl = "http://leasticoulddo.com/comic/20171111" diff --git a/src/test/resources/net/pterodactylus/rhynodge/filters/comics/least-i-could-do.html b/src/test/resources/net/pterodactylus/rhynodge/filters/comics/least-i-could-do.html new file mode 100644 index 0000000..3879937 --- /dev/null +++ b/src/test/resources/net/pterodactylus/rhynodge/filters/comics/least-i-could-do.html @@ -0,0 +1,595 @@ + + + + + + + Least I Could Do: the Comic + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ +
+ Storyline Progress +
+ + + + + + + + +
 
+ +
+
+
+ +
+
+ + +
+ +
+ +
+
+
+
+
+
+
+ +
+
+ +
+ +
+
+
+ +
+ +
+ +
+ +
+
+
+ + + +
+
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + -- 2.7.4