Add filter for comic sites.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / filters / ComicSiteFilter.java
1 /*
2  * rhynodge - ComicFilter.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.filters;
19
20 import static com.google.common.base.Preconditions.*;
21
22 import java.util.List;
23
24 import net.pterodactylus.rhynodge.Filter;
25 import net.pterodactylus.rhynodge.State;
26 import net.pterodactylus.rhynodge.states.ComicState;
27 import net.pterodactylus.rhynodge.states.ComicState.Comic;
28 import net.pterodactylus.rhynodge.states.HtmlState;
29
30 import com.google.common.base.Optional;
31 import org.jsoup.nodes.Document;
32
33 /**
34  * {@link Filter} implementation that can extract {@link ComicState}s from
35  * {@link HtmlState}s.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public abstract class ComicSiteFilter implements Filter {
40
41         @Override
42         public State filter(State state) {
43                 checkArgument(state instanceof HtmlState, "state must be an HTML state");
44
45                 /* initialize states: */
46                 HtmlState htmlState = (HtmlState) state;
47                 ComicState comicState = new ComicState();
48
49                 /* extract comics. */
50                 Optional<String> title = extractTitle(htmlState.document());
51                 List<String> imageUrls = extractImageUrls(htmlState.document());
52
53                 /* store comic, if found, into state. */
54                 if (title.isPresent() && !imageUrls.isEmpty()) {
55                         Comic comic = new Comic(title.get());
56                         for (String imageUrl : imageUrls) {
57                                 comic.addImageUrl(imageUrl);
58                         }
59                 }
60
61                 return comicState;
62         }
63
64         //
65         // PROTECTED METHODS
66         //
67
68         /**
69          * Extracts the title of the comic from the given document.
70          *
71          * @param document
72          *              The document to extract the title from
73          * @return The extracted title, or {@link Optional#absent()}} if no title could
74          *         be found
75          */
76         protected abstract Optional<String> extractTitle(Document document);
77
78         /**
79          * Extracts the image URLs from the given document.
80          *
81          * @param document
82          *              The document to extract the image URLs from
83          * @return The extracted image URLs, or an empty list if no URLs could be
84          *         found
85          */
86         protected abstract List<String> extractImageUrls(Document document);
87
88 }