1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.IOException
;
4 import java
.net
.HttpURLConnection
;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.AbstractMap
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Collections
;
10 import java
.util
.List
;
11 import java
.util
.Map
.Entry
;
13 import org
.jsoup
.nodes
.Element
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.bundles
.Config
;
17 import be
.nikiroo
.fanfix
.data
.MetaData
;
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://mangahub.io/">MangaHub</a>, a website
28 class MangaHub
extends BasicSupport
{
30 protected boolean isHtml() {
35 protected MetaData
getMeta() throws IOException
{
36 MetaData meta
= new MetaData();
38 meta
.setTitle(getTitle());
40 meta
.setAuthor(getAuthor());
41 meta
.setTags(getTags());
42 meta
.setSource(getType().getSourceName());
43 meta
.setUrl(getSource().toString());
44 meta
.setPublisher(getType().getSourceName());
45 meta
.setUuid(getSource().toString());
48 meta
.setSubject("manga");
49 meta
.setType(getType().toString());
50 meta
.setImageDocument(true);
51 meta
.setCover(getCover());
56 private String
getTitle() {
57 Element doc
= getSourceNode();
59 Element el
= doc
.getElementsByTag("h1").first();
61 return StringUtils
.unhtml(el
.text()).trim();
67 private String
getAuthor() {
70 Element el
= getSourceNode().select("h1+div span:not([class])").first();
72 author
= StringUtils
.unhtml(el
.text()).trim();
76 private List
<String
> getTags() {
77 return getListA("genre-label");
80 private List
<String
> getListA(String uniqueClass
) {
81 List
<String
> list
= new ArrayList
<String
>();
83 Element doc
= getSourceNode();
84 Element el
= doc
.getElementsByClass(uniqueClass
).first();
86 for (Element valueA
: el
.getElementsByTag("a")) {
87 list
.add(StringUtils
.unhtml(valueA
.text()).trim());
95 protected String
getDesc() {
96 Element doc
= getSourceNode();
97 Element title
= doc
.getElementsByClass("fullcontent").first();
99 return StringUtils
.unhtml(title
.text()).trim();
105 private Image
getCover() {
106 Element doc
= getSourceNode();
107 Element cover
= doc
.getElementsByClass("manga-thumb").first();
110 return bsImages
.getImage(this, new URL(cover
.absUrl("src")));
111 } catch (MalformedURLException e
) {
112 Instance
.getInstance().getTraceHandler().error(e
);
120 protected List
<Entry
<String
, URL
>> getChapters(Progress pg
) {
121 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
123 Element doc
= getSourceNode();
124 for (Element el
: doc
.getElementsByClass("list-group-item")) {
125 Element urlEl
= el
.getElementsByTag("a").first();
129 String url
= urlEl
.absUrl("href");
132 el
= el
.getElementsByClass("text-secondary").first();
134 title
= StringUtils
.unhtml(el
.text()).trim();
138 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(title
, new URL(url
)));
139 } catch (Exception e
) {
140 Instance
.getInstance().getTraceHandler().error(e
);
144 // by default, the chapters are in reversed order
145 Collections
.reverse(urls
);
151 protected String
getChapterContent(URL chapUrl
, int number
, Progress pg
) throws IOException
{
156 // 1. Get the title and chapter url part
157 String path
= chapUrl
.getPath();
158 if (path
.endsWith("/")) {
159 path
= path
.substring(0, path
.length() - "/".length());
161 String tab
[] = path
.split("/");
162 String chap
= tab
[tab
.length
- 1];
163 String title
= tab
[tab
.length
- 2];
165 if (chap
.startsWith("chapter-")) {
166 chap
= chap
.substring("chapter-".length());
169 // 2. generate an image base
170 String base
= "https://img.mghubcdn.com/file/imghub/" + title
+ "/" + chap
+ "/";
172 // 3. add each chapter
173 StringBuilder builder
= new StringBuilder();
176 String url
= base
+ i
+ ".jpg";
177 while (getHttpStatus(new URL(url
)) != 404) {
180 builder
.append("]<br/>");
183 url
= base
+ i
+ ".jpg";
186 return builder
.toString();
189 // HTTP response code, or -1 if other error
190 // TODO: move that to Downloader?
191 private int getHttpStatus(URL url
) {
193 HttpURLConnection conn
= (HttpURLConnection
) url
.openConnection();
195 conn
.setRequestMethod("HEAD");
196 conn
.setRequestProperty("User-Agent", Instance
.getInstance().getConfig().getString(Config
.NETWORK_USER_AGENT
));
197 conn
.setRequestProperty("Accept-Encoding", "gzip");
198 conn
.setRequestProperty("Accept", "*/*");
199 conn
.setRequestProperty("Charset", "utf-8");
201 return conn
.getResponseCode();
205 } catch (Exception e
) {
211 protected boolean supports(URL url
) {
212 return "mangahub.io".equals(url
.getHost()) || "www.mangahub.io".equals(url
.getHost());