From aec4aefd336118ee348d27cfe3a8a315303e9eb3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 26 Feb 2013 21:57:45 +0100 Subject: [PATCH] Add watcher implementation for LICD. --- .../rhynodge/watchers/LeastICouldDoWatcher.java | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java 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 index 0000000..406f1cb --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/LeastICouldDoWatcher.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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 createFilters() { + ImmutableList.Builder filters = ImmutableList.builder(); + + filters.add(new HtmlFilter()); + filters.add(new ExtractUrlFilter() { + + @Override + protected Optional extractUrl(Document document) { + Elements linkTag = document.select("a#feature-comic"); + return linkTag.hasAttr("href") ? Optional.of(linkTag.attr("href")) : Optional.absent(); + } + }); + filters.add(new HttpQueryFilter()); + filters.add(new HtmlFilter()); + filters.add(new LeastICouldDoComicFilter()); + + return filters.build(); + } + +} -- 2.7.4