Add watcher implementation for LICD.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 26 Feb 2013 20:57:45 +0000 (21:57 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 26 Feb 2013 20:57:45 +0000 (21:57 +0100)
src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java
new file mode 100644 (file)
index 0000000..406f1cb
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * rhynodge - LeastICouldDoWatcher.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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.rhynodge.watchers;
+
+import java.util.List;
+
+import net.pterodactylus.rhynodge.Filter;
+import net.pterodactylus.rhynodge.Watcher;
+import net.pterodactylus.rhynodge.filters.ExtractUrlFilter;
+import net.pterodactylus.rhynodge.filters.HtmlFilter;
+import net.pterodactylus.rhynodge.filters.HttpQueryFilter;
+import net.pterodactylus.rhynodge.filters.comics.LeastICouldDoComicFilter;
+import net.pterodactylus.rhynodge.queries.HttpQuery;
+import net.pterodactylus.rhynodge.triggers.NewComicTrigger;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import org.jsoup.nodes.Document;
+import org.jsoup.select.Elements;
+
+/**
+ * {@link Watcher} implementation that watched “Least I Could Do” for new
+ * comics.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class LeastICouldDoWatcher extends DefaultWatcher {
+
+       /** Creates a new “Least I Could Do” watcher. */
+       public LeastICouldDoWatcher() {
+               super(new HttpQuery("http://www.leasticoulddo.com/"), createFilters(), new NewComicTrigger());
+       }
+
+       //
+       // STATIC METHODS
+       //
+
+       /**
+        * Creates the filters to parse LICD. Because LICD does not show the comic on
+        * the front page, and there’s no fixed URL for the latest comic, LICD requires
+        * a two-step process: first get the main page, parse the URL of the comic page
+        * from it, then get the comic page and parse the comic from that.
+        *
+        * @return The filters to parse LICD
+        */
+       private static List<Filter> createFilters() {
+               ImmutableList.Builder<Filter> filters = ImmutableList.builder();
+
+               filters.add(new HtmlFilter());
+               filters.add(new ExtractUrlFilter() {
+
+                       @Override
+                       protected Optional<String> extractUrl(Document document) {
+                               Elements linkTag = document.select("a#feature-comic");
+                               return linkTag.hasAttr("href") ? Optional.of(linkTag.attr("href")) : Optional.<String>absent();
+                       }
+               });
+               filters.add(new HttpQueryFilter());
+               filters.add(new HtmlFilter());
+               filters.add(new LeastICouldDoComicFilter());
+
+               return filters.build();
+       }
+
+}