0b52045f241758b98b8742b5af9f87348a5d1bab
[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.checkArgument;
21
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.util.List;
25
26 import net.pterodactylus.rhynodge.Filter;
27 import net.pterodactylus.rhynodge.State;
28 import net.pterodactylus.rhynodge.states.ComicState;
29 import net.pterodactylus.rhynodge.states.ComicState.Comic;
30 import net.pterodactylus.rhynodge.states.ComicState.Strip;
31 import net.pterodactylus.rhynodge.states.HtmlState;
32
33 import com.google.common.base.Optional;
34 import org.jsoup.nodes.Document;
35
36 /**
37  * {@link Filter} implementation that can extract {@link ComicState}s from
38  * {@link HtmlState}s.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public abstract class ComicSiteFilter implements Filter {
43
44         @Override
45         public State filter(State state) {
46                 checkArgument(state instanceof HtmlState, "state must be an HTML state");
47
48                 /* initialize states: */
49                 HtmlState htmlState = (HtmlState) state;
50                 ComicState comicState = new ComicState();
51
52                 /* extract comics. */
53                 Optional<String> title = extractTitle(htmlState.document());
54                 List<String> imageUrls = extractImageUrls(htmlState.document());
55                 List<String> imageComments = extractImageComments(htmlState.document());
56
57                 /* store comic, if found, into state. */
58                 if (title.isPresent() && !imageUrls.isEmpty()) {
59                         Comic comic = new Comic(title.get());
60                         int imageCounter = 0;
61                         for (String imageUrl : imageUrls) {
62                                 String imageComment = (imageCounter < imageComments.size()) ? imageComments.get(imageCounter) : "";
63                                 try {
64                                         URI stripUri = new URI(htmlState.uri()).resolve(imageUrl);
65                                         Strip strip = new Strip(stripUri.toString(), imageComment);
66                                         imageCounter++;
67                                         comic.add(strip);
68                                 } catch (URISyntaxException use1) {
69                                         throw new IllegalStateException(String.format("Could not resolve image URL “%s” against base URL “%s”.", imageUrl, htmlState.uri()), use1);
70                                 }
71                         }
72                         comicState.add(comic);
73                 }
74
75                 return comicState;
76         }
77
78         //
79         // PROTECTED METHODS
80         //
81
82         /**
83          * Extracts the title of the comic from the given document.
84          *
85          * @param document
86          *              The document to extract the title from
87          * @return The extracted title, or {@link Optional#absent()}} if no title could
88          *         be found
89          */
90         protected abstract Optional<String> extractTitle(Document document);
91
92         /**
93          * Extracts the image URLs from the given document.
94          *
95          * @param document
96          *              The document to extract the image URLs from
97          * @return The extracted image URLs, or an empty list if no URLs could be
98          *         found
99          */
100         protected abstract List<String> extractImageUrls(Document document);
101
102         /**
103          * Extracts the image comments from the given document. The elements of this
104          * last and of the list returned by {@link #extractImageUrls(org.jsoup.nodes.Document)}
105          * are paired up and added as {@link Strip}s. If the list returned by this
106          * method has less elements, an empty string is used for the remaining images.
107          *
108          * @param document
109          *              The document to extract the image comments from
110          * @return The extracted image comments
111          */
112         protected abstract List<String> extractImageComments(Document document);
113
114 }