eae377c81d9feb53d5bdb2dcdda21623227d348d
[fanfix.git] / src / be / nikiroo / fanfix / supported / EHentai.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map.Entry;
11 import java.util.Scanner;
12
13 import be.nikiroo.fanfix.Instance;
14 import be.nikiroo.fanfix.data.Chapter;
15 import be.nikiroo.fanfix.data.MetaData;
16 import be.nikiroo.fanfix.data.Story;
17 import be.nikiroo.utils.Progress;
18 import be.nikiroo.utils.StringUtils;
19
20 /**
21 * Support class for <a href="https://e-hentai.org/">e-hentai.org</a>, a website
22 * supporting mostly but not always NSFW comics, including some of MLP.
23 *
24 * @author niki
25 */
26 class EHentai extends BasicSupport {
27 @Override
28 public String getSourceName() {
29 return "e-hentai.org";
30 }
31
32 @Override
33 protected MetaData getMeta(URL source, InputStream in) throws IOException {
34 MetaData meta = new MetaData();
35
36 meta.setTitle(getTitle(reset(in)));
37 meta.setAuthor(getAuthor(reset(in)));
38 meta.setDate("");
39 meta.setTags(getTags(reset(in)));
40 meta.setSource(getSourceName());
41 meta.setUrl(source.toString());
42 meta.setPublisher(getSourceName());
43 meta.setUuid(source.toString());
44 meta.setLuid("");
45 meta.setLang("EN");
46 meta.setSubject("Furry");
47 meta.setType(getType().toString());
48 meta.setImageDocument(true);
49 meta.setCover(getCover(source, reset(in)));
50 meta.setFakeCover(true);
51
52 return meta;
53 }
54
55 @Override
56 public Story process(URL url, Progress pg) throws IOException {
57 // There is no chapters on e621, just pagination...
58 Story story = super.process(url, pg);
59
60 Chapter only = new Chapter(1, null);
61 for (Chapter chap : story) {
62 only.getParagraphs().addAll(chap.getParagraphs());
63 }
64
65 story.getChapters().clear();
66 story.getChapters().add(only);
67
68 return story;
69 }
70
71 @Override
72 protected boolean supports(URL url) {
73 return "e-hentai.org".equals(url.getHost());
74 }
75
76 @Override
77 protected boolean isHtml() {
78 return true;
79 }
80
81 private BufferedImage getCover(URL source, InputStream in)
82 throws IOException {
83 BufferedImage author = null;
84 String coverLine = getKeyLine(in, "<div id=\"gd1\"", " url(", ")");
85 if (coverLine != null) {
86 coverLine = StringUtils.unhtml(coverLine).trim();
87 author = getImage(this, source, coverLine);
88 }
89
90 return author;
91 }
92
93 private String getAuthor(InputStream in) {
94 String author = null;
95
96 List<String> tagsAuthor = getTagsAuthor(in);
97 if (!tagsAuthor.isEmpty()) {
98 author = tagsAuthor.get(0);
99 }
100
101 return author;
102 }
103
104 private List<String> getTags(InputStream in) {
105 List<String> tags = new ArrayList<String>();
106 List<String> tagsAuthor = getTagsAuthor(in);
107
108 for (int i = 1; i < tagsAuthor.size(); i++) {
109 tags.add(tagsAuthor.get(i));
110 }
111
112 return tags;
113 }
114
115 private List<String> getTagsAuthor(InputStream in) {
116 List<String> tags = new ArrayList<String>();
117 String tagLine = getKeyLine(in, "<meta name=\"description\"", "Tags: ",
118 null);
119 if (tagLine != null) {
120 for (String tag : tagLine.split(",")) {
121 String candi = tag.trim();
122 if (!candi.isEmpty() && !tags.contains(candi)) {
123 tags.add(candi);
124 }
125 }
126 }
127
128 return tags;
129 }
130
131 private String getTitle(InputStream in) throws IOException {
132 String siteName = " - E-Hentai Galleries";
133
134 String title = getLine(in, "<title>", 0);
135 if (title != null) {
136 title = StringUtils.unhtml(title).trim();
137 if (title.endsWith(siteName)) {
138 title = title.substring(0, title.length() - siteName.length())
139 .trim();
140 }
141 }
142
143 return title;
144 }
145
146 @Override
147 protected String getDesc(URL source, InputStream in) throws IOException {
148 String desc = null;
149
150 String descLine = getKeyLine(in, "Uploader Comment", null,
151 "<div class=\"c7\"");
152 if (descLine != null) {
153 desc = StringUtils.unhtml(descLine);
154 }
155
156 return desc;
157 }
158
159 @Override
160 protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
161 Progress pg) throws IOException {
162 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
163 int last = 0; // no pool/show when only one page, first page == page 0
164
165 @SuppressWarnings("resource")
166 Scanner scan = new Scanner(in, "UTF-8");
167 scan.useDelimiter(">");
168 while (scan.hasNext()) {
169 String line = scan.next();
170 if (line.contains(source.toString())) {
171 String page = line.substring(line.indexOf(source.toString()));
172 String pkey = "?p=";
173 if (page.contains(pkey)) {
174 page = page.substring(page.indexOf(pkey) + pkey.length());
175 String number = "";
176 while (!page.isEmpty() && page.charAt(0) >= '0'
177 && page.charAt(0) <= '9') {
178 number += page.charAt(0);
179 page = page.substring(1);
180 }
181 if (number.isEmpty()) {
182 number = "0";
183 }
184
185 int current = Integer.parseInt(number);
186 if (last < current) {
187 last = current;
188 }
189 }
190 }
191 }
192
193 for (int i = 0; i <= last; i++) {
194 final String key = Integer.toString(i + 1);
195 final URL value = new URL(source.toString() + "?p=" + i);
196 urls.add(new Entry<String, URL>() {
197 public URL setValue(URL value) {
198 return null;
199 }
200
201 public URL getValue() {
202 return value;
203 }
204
205 public String getKey() {
206 return key;
207 }
208 });
209 }
210
211 return urls;
212 }
213
214 @Override
215 protected String getChapterContent(URL source, InputStream in, int number,
216 Progress pg) throws IOException {
217 String staticSite = "https://e-hentai.org/s/";
218 List<URL> pages = new ArrayList<URL>();
219
220 @SuppressWarnings("resource")
221 Scanner scan = new Scanner(in, "UTF-8");
222 scan.useDelimiter("\"");
223 while (scan.hasNext()) {
224 String line = scan.next();
225 if (line.startsWith(staticSite)) {
226 try {
227 pages.add(new URL(line));
228 } catch (MalformedURLException e) {
229 Instance.syserr(new IOException(
230 "Parsing error, a link is not correctly parsed: "
231 + line, e));
232 }
233 }
234 }
235
236 if (pg == null) {
237 pg = new Progress();
238 }
239 pg.setMinMax(0, pages.size());
240 pg.setProgress(0);
241
242 StringBuilder builder = new StringBuilder();
243
244 for (URL page : pages) {
245 InputStream pageIn = Instance.getCache().open(page, this, false);
246 try {
247 String link = getKeyLine(pageIn, "id=\"img\"", "src=\"", "\"");
248 if (link != null && !link.isEmpty()) {
249 builder.append("[");
250 builder.append(link);
251 builder.append("]<br/>");
252 }
253 pg.add(1);
254 } finally {
255 if (pageIn != null) {
256 pageIn.close();
257 }
258 }
259 }
260
261 pg.done();
262 return builder.toString();
263 }
264 }