🐛 Fix broken change detection
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / watchers / LeastICouldDoWatcher.java
1 /*
2  * rhynodge - LeastICouldDoWatcher.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.watchers;
19
20 import java.util.List;
21
22 import net.pterodactylus.rhynodge.Filter;
23 import net.pterodactylus.rhynodge.Watcher;
24 import net.pterodactylus.rhynodge.filters.ExtractUrlFilter;
25 import net.pterodactylus.rhynodge.filters.HtmlFilter;
26 import net.pterodactylus.rhynodge.filters.HttpQueryFilter;
27 import net.pterodactylus.rhynodge.filters.comics.LeastICouldDoComicFilter;
28 import net.pterodactylus.rhynodge.mergers.ComicMerger;
29 import net.pterodactylus.rhynodge.queries.HttpQuery;
30
31 import com.google.common.base.Optional;
32 import com.google.common.collect.ImmutableList;
33 import org.jsoup.nodes.Document;
34 import org.jsoup.select.Elements;
35
36 /**
37  * {@link Watcher} implementation that watched “Least I Could Do” for new
38  * comics.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class LeastICouldDoWatcher extends DefaultWatcher {
43
44         /** Creates a new “Least I Could Do” watcher. */
45         public LeastICouldDoWatcher() {
46                 super(new HttpQuery("http://www.leasticoulddo.com/"), createFilters(), new ComicMerger());
47         }
48
49         //
50         // STATIC METHODS
51         //
52
53         /**
54          * Creates the filters to parse LICD. Because LICD does not show the comic on
55          * the front page, and there’s no fixed URL for the latest comic, LICD requires
56          * a two-step process: first get the main page, parse the URL of the comic page
57          * from it, then get the comic page and parse the comic from that.
58          *
59          * @return The filters to parse LICD
60          */
61         private static List<Filter> createFilters() {
62                 ImmutableList.Builder<Filter> filters = ImmutableList.builder();
63
64                 filters.add(new HtmlFilter());
65                 filters.add(new ExtractUrlFilter() {
66
67                         @Override
68                         protected Optional<String> extractUrl(Document document) {
69                                 Elements linkTag = document.select("a#latest-comic");
70                                 return linkTag.hasAttr("href") ? Optional.of(linkTag.attr("href")) : Optional.<String>absent();
71                         }
72                 });
73                 filters.add(new HttpQueryFilter());
74                 filters.add(new HtmlFilter());
75                 filters.add(new LeastICouldDoComicFilter());
76
77                 return filters.build();
78         }
79
80 }