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