| 1 | package be.nikiroo.fanfix.supported; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.net.HttpURLConnection; |
| 5 | import java.net.MalformedURLException; |
| 6 | import java.net.URL; |
| 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; |
| 12 | |
| 13 | import org.jsoup.nodes.Element; |
| 14 | |
| 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; |
| 21 | |
| 22 | /** |
| 23 | * Support class for <a href="https://mangahub.io/">MangaHub</a>, a website |
| 24 | * dedicated to Manga. |
| 25 | * |
| 26 | * @author niki |
| 27 | */ |
| 28 | class MangaHub extends BasicSupport { |
| 29 | @Override |
| 30 | protected boolean isHtml() { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | protected MetaData getMeta() throws IOException { |
| 36 | MetaData meta = new MetaData(); |
| 37 | |
| 38 | meta.setTitle(getTitle()); |
| 39 | meta.setDate(""); |
| 40 | meta.setAuthor(getAuthor()); |
| 41 | meta.setTags(getTags()); |
| 42 | meta.setUrl(getSource().toString()); |
| 43 | meta.setUuid(getSource().toString()); |
| 44 | meta.setLuid(""); |
| 45 | meta.setLang("en"); |
| 46 | meta.setSubject("manga"); |
| 47 | meta.setImageDocument(true); |
| 48 | meta.setCover(getCover()); |
| 49 | |
| 50 | return meta; |
| 51 | } |
| 52 | |
| 53 | private String getTitle() { |
| 54 | Element doc = getSourceNode(); |
| 55 | |
| 56 | Element el = doc.getElementsByTag("h1").first(); |
| 57 | if (el != null) { |
| 58 | return StringUtils.unhtml(el.text()).trim(); |
| 59 | } |
| 60 | |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | private String getAuthor() { |
| 65 | String author = ""; |
| 66 | |
| 67 | Element el = getSourceNode().select("h1+div span:not([class])").first(); |
| 68 | if (el != null) |
| 69 | author = StringUtils.unhtml(el.text()).trim(); |
| 70 | return author; |
| 71 | } |
| 72 | |
| 73 | private List<String> getTags() { |
| 74 | return getListA("genre-label"); |
| 75 | } |
| 76 | |
| 77 | private List<String> getListA(String uniqueClass) { |
| 78 | List<String> list = new ArrayList<String>(); |
| 79 | |
| 80 | Element doc = getSourceNode(); |
| 81 | Element el = doc.getElementsByClass(uniqueClass).first(); |
| 82 | if (el != null) { |
| 83 | for (Element valueA : el.getElementsByTag("a")) { |
| 84 | list.add(StringUtils.unhtml(valueA.text()).trim()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return list; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | protected String getDesc() { |
| 93 | Element doc = getSourceNode(); |
| 94 | Element title = doc.getElementsByClass("fullcontent").first(); |
| 95 | if (title != null) { |
| 96 | return StringUtils.unhtml(title.text()).trim(); |
| 97 | } |
| 98 | |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | private Image getCover() { |
| 103 | Element doc = getSourceNode(); |
| 104 | Element cover = doc.getElementsByClass("manga-thumb").first(); |
| 105 | if (cover != null) { |
| 106 | try { |
| 107 | return bsImages.getImage(this, new URL(cover.absUrl("src"))); |
| 108 | } catch (MalformedURLException e) { |
| 109 | Instance.getInstance().getTraceHandler().error(e); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected List<Entry<String, URL>> getChapters(Progress pg) { |
| 118 | List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>(); |
| 119 | |
| 120 | Element doc = getSourceNode(); |
| 121 | for (Element el : doc.getElementsByClass("list-group-item")) { |
| 122 | Element urlEl = el.getElementsByTag("a").first(); |
| 123 | if (urlEl == null) |
| 124 | continue; |
| 125 | |
| 126 | String url = urlEl.absUrl("href"); |
| 127 | |
| 128 | String title = ""; |
| 129 | el = el.getElementsByClass("text-secondary").first(); |
| 130 | if (el != null) { |
| 131 | title = StringUtils.unhtml(el.text()).trim(); |
| 132 | } |
| 133 | |
| 134 | try { |
| 135 | urls.add(new AbstractMap.SimpleEntry<String, URL>(title, new URL(url))); |
| 136 | } catch (Exception e) { |
| 137 | Instance.getInstance().getTraceHandler().error(e); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // by default, the chapters are in reversed order |
| 142 | Collections.reverse(urls); |
| 143 | |
| 144 | return urls; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | protected String getChapterContent(URL chapUrl, int number, Progress pg) throws IOException { |
| 149 | if (pg == null) { |
| 150 | pg = new Progress(); |
| 151 | } |
| 152 | |
| 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()); |
| 157 | } |
| 158 | String tab[] = path.split("/"); |
| 159 | String chap = tab[tab.length - 1]; |
| 160 | String title = tab[tab.length - 2]; |
| 161 | |
| 162 | if (chap.startsWith("chapter-")) { |
| 163 | chap = chap.substring("chapter-".length()); |
| 164 | } |
| 165 | |
| 166 | // 2. generate an image base |
| 167 | String base = "https://img.mghubcdn.com/file/imghub/" + title + "/" + chap + "/"; |
| 168 | |
| 169 | // 3. add each chapter |
| 170 | StringBuilder builder = new StringBuilder(); |
| 171 | |
| 172 | int i = 1; |
| 173 | String url = base + i + ".jpg"; |
| 174 | while (getHttpStatus(new URL(url)) != 404) { |
| 175 | builder.append("["); |
| 176 | builder.append(url); |
| 177 | builder.append("]<br/>"); |
| 178 | |
| 179 | i++; |
| 180 | url = base + i + ".jpg"; |
| 181 | } |
| 182 | |
| 183 | return builder.toString(); |
| 184 | } |
| 185 | |
| 186 | // HTTP response code, or -1 if other error |
| 187 | // TODO: move that to Downloader? |
| 188 | private int getHttpStatus(URL url) { |
| 189 | try { |
| 190 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 191 | try { |
| 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"); |
| 197 | |
| 198 | return conn.getResponseCode(); |
| 199 | } finally { |
| 200 | conn.disconnect(); |
| 201 | } |
| 202 | } catch (Exception e) { |
| 203 | return -1; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | protected boolean supports(URL url) { |
| 209 | return "mangahub.io".equals(url.getHost()) || "www.mangahub.io".equals(url.getHost()); |
| 210 | } |
| 211 | } |