From d98b6e32d1d5f31dfd6abd050c88ff7767cfc3f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 25 Feb 2013 07:11:13 +0100 Subject: [PATCH] Add XKCD comic filter and watcher. --- .../rhynodge/filters/comics/XkcdComicFilter.java | 87 ++++++++++++++++++++++ .../rhynodge/watchers/XkcdWatcher.java | 40 ++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/main/java/net/pterodactylus/rhynodge/filters/comics/XkcdComicFilter.java create mode 100644 src/main/java/net/pterodactylus/rhynodge/watchers/XkcdWatcher.java diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/comics/XkcdComicFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/comics/XkcdComicFilter.java new file mode 100644 index 0000000..ae80654 --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/filters/comics/XkcdComicFilter.java @@ -0,0 +1,87 @@ +/* + * rhynodge - XkcdFilter.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.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 XKCD comics. + * + * @author David ‘Bombe’ Roden + */ +public class XkcdComicFilter extends ComicSiteFilter { + + @Override + protected Optional extractTitle(Document document) { + Elements titleElement = document.select("div#ctitle"); + return titleElement.hasText() ? Optional.of(titleElement.text()) : Optional.absent(); + } + + @Override + protected List extractImageUrls(Document document) { + return extractImages(document).transform(new Function() { + + @Override + public String apply(String[] input) { + return input[0]; + } + }).toList(); + } + + @Override + protected List extractImageComments(Document document) { + return extractImages(document).transform(new Function() { + + @Override + public String apply(String[] input) { + return input[1]; + } + }).toList(); + } + + // + // PRIVATE METHODS + // + + /** + * Extracts pairs of image URLs and image comments from the given document. + * + * @param document + * The document to extract the images from + * @return An iterable containing all image URL and comment pairs + */ + private FluentIterable extractImages(Document document) { + return FluentIterable.from(document.select("div#comic img")).transform(new Function() { + + @Override + public String[] apply(Element image) { + return new String[] { image.attr("src"), image.attr("title") }; + } + }); + } + +} diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/XkcdWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/XkcdWatcher.java new file mode 100644 index 0000000..360652a --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/XkcdWatcher.java @@ -0,0 +1,40 @@ +/* + * rhynodge - SinfestWacher.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.Arrays; + +import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.comics.XkcdComicFilter; +import net.pterodactylus.rhynodge.queries.HttpQuery; +import net.pterodactylus.rhynodge.triggers.NewComicTrigger; + +/** + * {@link net.pterodactylus.rhynodge.Watcher} implementation that watches XKCD + * for new comics. + * + * @author David ‘Bombe’ Roden + */ +public class XkcdWatcher extends DefaultWatcher { + + /** Creates a new XKCD watcher. */ + public XkcdWatcher() { + super(new HttpQuery("http://xkcd.com/"), Arrays.asList(new HtmlFilter(), new XkcdComicFilter()), new NewComicTrigger()); + } + +} -- 2.7.4