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