1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.AbstractMap
;
8 import java
.util
.ArrayList
;
11 import java
.util
.Map
.Entry
;
12 import java
.util
.Scanner
;
14 import be
.nikiroo
.fanfix
.Instance
;
15 import be
.nikiroo
.fanfix
.data
.Chapter
;
16 import be
.nikiroo
.fanfix
.data
.MetaData
;
17 import be
.nikiroo
.fanfix
.data
.Story
;
18 import be
.nikiroo
.utils
.Image
;
19 import be
.nikiroo
.utils
.Progress
;
20 import be
.nikiroo
.utils
.StringUtils
;
23 * Support class for <a href="https://e-hentai.org/">e-hentai.org</a>, a website
24 * supporting mostly but not always NSFW comics, including some of MLP.
28 class EHentai
extends BasicSupport_Deprecated
{
30 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
31 MetaData meta
= new MetaData();
33 meta
.setTitle(getTitle(reset(in
)));
34 meta
.setAuthor(getAuthor(reset(in
)));
35 meta
.setDate(getDate(reset(in
)));
36 meta
.setTags(getTags(reset(in
)));
37 meta
.setSource(getType().getSourceName());
38 meta
.setUrl(source
.toString());
39 meta
.setPublisher(getType().getSourceName());
40 meta
.setUuid(source
.toString());
42 meta
.setLang(getLang(reset(in
)));
43 meta
.setSubject("Hentai");
44 meta
.setType(getType().toString());
45 meta
.setImageDocument(true);
46 meta
.setCover(getCover(source
, reset(in
)));
47 meta
.setFakeCover(true);
53 public Story
process(URL url
, Progress pg
) throws IOException
{
54 // There is no chapters on e621, just pagination...
55 Story story
= super.process(url
, pg
);
57 Chapter only
= new Chapter(1, null);
58 for (Chapter chap
: story
) {
59 only
.getParagraphs().addAll(chap
.getParagraphs());
62 story
.getChapters().clear();
63 story
.getChapters().add(only
);
69 protected boolean supports(URL url
) {
70 return "e-hentai.org".equals(url
.getHost());
74 protected boolean isHtml() {
79 public Map
<String
, String
> getCookies() {
80 Map
<String
, String
> cookies
= super.getCookies();
81 cookies
.put("nw", "1");
85 private Image
getCover(URL source
, InputStream in
) {
87 String coverLine
= getKeyLine(in
, "<div id=\"gd1\"", " url(", ")");
88 if (coverLine
!= null) {
89 coverLine
= StringUtils
.unhtml(coverLine
).trim();
90 author
= getImage(this, source
, coverLine
);
96 private String
getAuthor(InputStream in
) {
99 List
<String
> tagsAuthor
= getTagsAuthor(in
);
100 if (!tagsAuthor
.isEmpty()) {
101 author
= tagsAuthor
.get(0);
107 private String
getLang(InputStream in
) {
110 String langLine
= getKeyLine(in
, "class=\"gdt1\">Language",
111 "class=\"gdt2\"", "</td>");
112 if (langLine
!= null) {
113 langLine
= StringUtils
.unhtml(langLine
).trim();
114 if (langLine
.equalsIgnoreCase("English")) {
116 } else if (langLine
.equalsIgnoreCase("Japanese")) {
118 } else if (langLine
.equalsIgnoreCase("French")) {
121 // TODO find the code?
129 private String
getDate(InputStream in
) {
132 String dateLine
= getKeyLine(in
, "class=\"gdt1\">Posted",
133 "class=\"gdt2\"", "</td>");
134 if (dateLine
!= null) {
135 dateLine
= StringUtils
.unhtml(dateLine
).trim();
136 if (dateLine
.length() > 10) {
137 dateLine
= dateLine
.substring(0, 10).trim();
146 private List
<String
> getTags(InputStream in
) {
147 List
<String
> tags
= new ArrayList
<String
>();
148 List
<String
> tagsAuthor
= getTagsAuthor(in
);
150 for (int i
= 1; i
< tagsAuthor
.size(); i
++) {
151 tags
.add(tagsAuthor
.get(i
));
157 private List
<String
> getTagsAuthor(InputStream in
) {
158 List
<String
> tags
= new ArrayList
<String
>();
159 String tagLine
= getKeyLine(in
, "<meta name=\"description\"", "Tags: ",
161 if (tagLine
!= null) {
162 for (String tag
: tagLine
.split(",")) {
163 String candi
= tag
.trim();
164 if (!candi
.isEmpty() && !tags
.contains(candi
)) {
173 private String
getTitle(InputStream in
) {
174 String siteName
= " - E-Hentai Galleries";
176 String title
= getLine(in
, "<title>", 0);
178 title
= StringUtils
.unhtml(title
).trim();
179 if (title
.endsWith(siteName
)) {
180 title
= title
.substring(0, title
.length() - siteName
.length())
189 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
192 String descLine
= getKeyLine(in
, "Uploader Comment", null,
193 "<div class=\"c7\"");
194 if (descLine
!= null) {
195 desc
= StringUtils
.unhtml(descLine
);
202 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
203 Progress pg
) throws IOException
{
204 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
205 int last
= 0; // no pool/show when only one page, first page == page 0
207 @SuppressWarnings("resource")
208 Scanner scan
= new Scanner(in
, "UTF-8");
209 scan
.useDelimiter(">");
210 while (scan
.hasNext()) {
211 String line
= scan
.next();
212 if (line
.contains(source
.toString())) {
213 String page
= line
.substring(line
.indexOf(source
.toString()));
215 if (page
.contains(pkey
)) {
216 page
= page
.substring(page
.indexOf(pkey
) + pkey
.length());
218 while (!page
.isEmpty() && page
.charAt(0) >= '0'
219 && page
.charAt(0) <= '9') {
220 number
+= page
.charAt(0);
221 page
= page
.substring(1);
223 if (number
.isEmpty()) {
227 int current
= Integer
.parseInt(number
);
228 if (last
< current
) {
235 for (int i
= 0; i
<= last
; i
++) {
236 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(Integer
237 .toString(i
+ 1), new URL(source
.toString() + "?p=" + i
)));
244 protected String
getChapterContent(URL source
, InputStream in
, int number
,
245 Progress pg
) throws IOException
{
246 String staticSite
= "https://e-hentai.org/s/";
247 List
<URL
> pages
= new ArrayList
<URL
>();
249 @SuppressWarnings("resource")
250 Scanner scan
= new Scanner(in
, "UTF-8");
251 scan
.useDelimiter("\"");
252 while (scan
.hasNext()) {
253 String line
= scan
.next();
254 if (line
.startsWith(staticSite
)) {
256 pages
.add(new URL(line
));
257 } catch (MalformedURLException e
) {
258 Instance
.getInstance().getTraceHandler()
259 .error(new IOException("Parsing error, a link is not correctly parsed: " + line
, e
));
267 pg
.setMinMax(0, pages
.size());
270 StringBuilder builder
= new StringBuilder();
272 for (URL page
: pages
) {
273 InputStream pageIn
= Instance
.getInstance().getCache().open(page
, this, false);
275 String link
= getKeyLine(pageIn
, "id=\"img\"", "src=\"", "\"");
276 if (link
!= null && !link
.isEmpty()) {
278 builder
.append(link
);
279 builder
.append("]<br/>");
283 if (pageIn
!= null) {
290 return builder
.toString();