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
.setUrl(getSource().toString());
43 meta
.setUuid(getSource().toString());
46 meta
.setSubject("manga");
47 meta
.setImageDocument(true);
48 meta
.setCover(getCover());
53 private String
getTitle() {
54 Element doc
= getSourceNode();
56 Element el
= doc
.getElementsByTag("h1").first();
58 return StringUtils
.unhtml(el
.text()).trim();
64 private String
getAuthor() {
67 Element el
= getSourceNode().select("h1+div span:not([class])").first();
69 author
= StringUtils
.unhtml(el
.text()).trim();
73 private List
<String
> getTags() {
74 return getListA("genre-label");
77 private List
<String
> getListA(String uniqueClass
) {
78 List
<String
> list
= new ArrayList
<String
>();
80 Element doc
= getSourceNode();
81 Element el
= doc
.getElementsByClass(uniqueClass
).first();
83 for (Element valueA
: el
.getElementsByTag("a")) {
84 list
.add(StringUtils
.unhtml(valueA
.text()).trim());
92 protected String
getDesc() {
93 Element doc
= getSourceNode();
94 Element title
= doc
.getElementsByClass("fullcontent").first();
96 return StringUtils
.unhtml(title
.text()).trim();
102 private Image
getCover() {
103 Element doc
= getSourceNode();
104 Element cover
= doc
.getElementsByClass("manga-thumb").first();
107 return bsImages
.getImage(this, new URL(cover
.absUrl("src")));
108 } catch (MalformedURLException e
) {
109 Instance
.getInstance().getTraceHandler().error(e
);
117 protected List
<Entry
<String
, URL
>> getChapters(Progress pg
) {
118 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
120 Element doc
= getSourceNode();
121 for (Element el
: doc
.getElementsByClass("list-group-item")) {
122 Element urlEl
= el
.getElementsByTag("a").first();
126 String url
= urlEl
.absUrl("href");
129 el
= el
.getElementsByClass("text-secondary").first();
131 title
= StringUtils
.unhtml(el
.text()).trim();
135 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(title
, new URL(url
)));
136 } catch (Exception e
) {
137 Instance
.getInstance().getTraceHandler().error(e
);
141 // by default, the chapters are in reversed order
142 Collections
.reverse(urls
);
148 protected String
getChapterContent(URL chapUrl
, int number
, Progress pg
) throws IOException
{
153 // 1. Get the title and chapter url part
154 String path
= chapUrl
.getPath();
155 if (path
.endsWith("/")) {
156 path
= path
.substring(0, path
.length() - "/".length());
158 String tab
[] = path
.split("/");
159 String chap
= tab
[tab
.length
- 1];
160 String title
= tab
[tab
.length
- 2];
162 if (chap
.startsWith("chapter-")) {
163 chap
= chap
.substring("chapter-".length());
166 // 2. generate an image base
167 String base
= "https://img.mghubcdn.com/file/imghub/" + title
+ "/" + chap
+ "/";
169 // 3. add each chapter
170 StringBuilder builder
= new StringBuilder();
173 String url
= base
+ i
+ ".jpg";
174 while (getHttpStatus(new URL(url
)) != 404) {
177 builder
.append("]<br/>");
180 url
= base
+ i
+ ".jpg";
183 return builder
.toString();
186 // HTTP response code, or -1 if other error
187 // TODO: move that to Downloader?
188 private int getHttpStatus(URL url
) {
190 HttpURLConnection conn
= (HttpURLConnection
) url
.openConnection();
192 conn
.setRequestMethod("HEAD");
193 conn
.setRequestProperty("User-Agent", Instance
.getInstance().getConfig().getString(Config
.NETWORK_USER_AGENT
));
194 conn
.setRequestProperty("Accept-Encoding", "gzip");
195 conn
.setRequestProperty("Accept", "*/*");
196 conn
.setRequestProperty("Charset", "utf-8");
198 return conn
.getResponseCode();
202 } catch (Exception e
) {
208 protected boolean supports(URL url
) {
209 return "mangahub.io".equals(url
.getHost()) || "www.mangahub.io".equals(url
.getHost());