Commit | Line | Data |
---|---|---|
af1f506f NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.io.InputStream; | |
af1f506f | 5 | import java.net.URL; |
6fc76bae NR |
6 | import java.text.ParseException; |
7 | import java.text.SimpleDateFormat; | |
af1f506f NR |
8 | import java.util.AbstractMap; |
9 | import java.util.ArrayList; | |
af1f506f NR |
10 | import java.util.List; |
11 | import java.util.Map.Entry; | |
12 | ||
13 | import org.jsoup.helper.DataUtil; | |
14 | import org.jsoup.nodes.Element; | |
15 | import org.jsoup.select.Elements; | |
16 | ||
17 | import be.nikiroo.fanfix.Instance; | |
18 | import be.nikiroo.fanfix.data.MetaData; | |
19 | import be.nikiroo.utils.Image; | |
20 | import be.nikiroo.utils.Progress; | |
21 | import be.nikiroo.utils.StringUtils; | |
22 | ||
23 | class MangaLel extends BasicSupport { | |
24 | @Override | |
25 | protected boolean isHtml() { | |
26 | return true; | |
27 | } | |
28 | ||
af1f506f NR |
29 | @Override |
30 | protected MetaData getMeta() throws IOException { | |
31 | MetaData meta = new MetaData(); | |
32 | ||
af1f506f | 33 | meta.setTitle(getTitle()); |
6fc76bae | 34 | meta.setAuthor(getAuthor()); |
bff19b54 | 35 | meta.setDate(bsHelper.formatDate(getDate())); |
6fc76bae | 36 | meta.setTags(getTags()); |
727108fe | 37 | meta.setSource(getType().getSourceName()); |
af1f506f | 38 | meta.setUrl(getSource().toString()); |
727108fe | 39 | meta.setPublisher(getType().getSourceName()); |
af1f506f NR |
40 | meta.setUuid(getSource().toString()); |
41 | meta.setLuid(""); | |
42 | meta.setLang("fr"); | |
43 | meta.setSubject("manga"); | |
44 | meta.setType(getType().toString()); | |
45 | meta.setImageDocument(true); | |
46 | meta.setCover(getCover()); | |
47 | ||
48 | return meta; | |
49 | } | |
50 | ||
51 | private String getTitle() { | |
52 | Element doc = getSourceNode(); | |
6fc76bae NR |
53 | Element h4 = doc.getElementsByTag("h4").first(); |
54 | if (h4 != null) { | |
55 | return StringUtils.unhtml(h4.text()).trim(); | |
af1f506f NR |
56 | } |
57 | ||
58 | return null; | |
59 | } | |
60 | ||
6fc76bae NR |
61 | private String getAuthor() { |
62 | Element doc = getSourceNode(); | |
12443642 NR |
63 | Element tabEls = doc.getElementsByClass("presentation-projet").first(); |
64 | if (tabEls != null) { | |
65 | String[] tab = tabEls.outerHtml().split("<br>"); | |
66 | return getVal(tab, 1); | |
6fc76bae NR |
67 | } |
68 | ||
12443642 | 69 | return ""; |
6fc76bae NR |
70 | } |
71 | ||
72 | private List<String> getTags() { | |
af1f506f | 73 | Element doc = getSourceNode(); |
12443642 NR |
74 | Element tabEls = doc.getElementsByClass("presentation-projet").first(); |
75 | if (tabEls != null) { | |
76 | String[] tab = tabEls.outerHtml().split("<br>"); | |
77 | List<String> tags = new ArrayList<String>(); | |
78 | for (String tag : getVal(tab, 3).split(" ")) { | |
79 | tags.add(tag); | |
af1f506f | 80 | } |
12443642 | 81 | return tags; |
af1f506f NR |
82 | } |
83 | ||
12443642 NR |
84 | return new ArrayList<String>(); |
85 | ||
6fc76bae NR |
86 | } |
87 | ||
88 | private String getDate() { | |
89 | Element doc = getSourceNode(); | |
90 | Element table = doc.getElementsByClass("table").first(); | |
91 | ||
92 | // We take the first date we find | |
93 | String value = ""; | |
94 | if (table != null) { | |
95 | Elements els; | |
96 | els = table.getElementsByTag("tr"); | |
97 | if (els.size() >= 2) { | |
98 | els = els.get(1).getElementsByTag("td"); | |
99 | if (els.size() >= 3) { | |
100 | value = StringUtils.unhtml(els.get(2).text()).trim(); | |
af1f506f | 101 | } |
af1f506f | 102 | } |
af1f506f NR |
103 | } |
104 | ||
6fc76bae | 105 | return value; |
af1f506f NR |
106 | } |
107 | ||
108 | @Override | |
109 | protected String getDesc() { | |
af1f506f | 110 | Element doc = getSourceNode(); |
12443642 NR |
111 | Element tabEls = doc.getElementsByClass("presentation-projet").first(); |
112 | if (tabEls != null) { | |
113 | String[] tab = tabEls.outerHtml().split("<br>"); | |
114 | return getVal(tab, 4); | |
af1f506f NR |
115 | } |
116 | ||
12443642 | 117 | return ""; |
af1f506f NR |
118 | } |
119 | ||
120 | private Image getCover() { | |
121 | Element doc = getSourceNode(); | |
6fc76bae NR |
122 | Element container = doc.getElementsByClass("container").first(); |
123 | ||
124 | if (container != null) { | |
125 | ||
126 | Elements imgs = container.getElementsByTag("img"); | |
127 | Element img = null; | |
128 | if (imgs.size() >= 1) { | |
129 | img = imgs.get(0); | |
130 | if (img.hasClass("banniere-team-projet")) { | |
131 | img = null; | |
132 | if (imgs.size() >= 2) { | |
133 | img = imgs.get(1); | |
134 | } | |
135 | } | |
136 | } | |
af1f506f | 137 | |
6fc76bae NR |
138 | if (img != null) { |
139 | String coverUrl = img.absUrl("src"); | |
af1f506f | 140 | |
6fc76bae | 141 | InputStream coverIn; |
af1f506f | 142 | try { |
d66deb8d | 143 | coverIn = Instance.getInstance().getCache().open(new URL(coverUrl), this, true); |
6fc76bae NR |
144 | try { |
145 | return new Image(coverIn); | |
146 | } finally { | |
147 | coverIn.close(); | |
148 | } | |
149 | } catch (IOException e) { | |
d66deb8d | 150 | Instance.getInstance().getTraceHandler().error(e); |
af1f506f | 151 | } |
af1f506f NR |
152 | } |
153 | } | |
154 | ||
155 | return null; | |
156 | } | |
157 | ||
12443642 NR |
158 | private String getVal(String[] tab, int i) { |
159 | String val = ""; | |
160 | ||
161 | if (i < tab.length) { | |
162 | val = StringUtils.unhtml(tab[i]); | |
163 | int pos = val.indexOf(":"); | |
164 | if (pos >= 0) { | |
165 | val = val.substring(pos + 1).trim(); | |
166 | } | |
167 | } | |
168 | ||
169 | return val; | |
170 | } | |
171 | ||
af1f506f | 172 | @Override |
6fc76bae NR |
173 | protected List<Entry<String, URL>> getChapters(Progress pg) |
174 | throws IOException { | |
af1f506f NR |
175 | List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>(); |
176 | ||
af1f506f | 177 | Element doc = getSourceNode(); |
6fc76bae NR |
178 | Element table = doc.getElementsByClass("table").first(); |
179 | if (table != null) { | |
180 | for (Element tr : table.getElementsByTag("tr")) { | |
181 | Element a = tr.getElementsByTag("a").first(); | |
182 | if (a != null) { | |
183 | String name = StringUtils.unhtml(a.text()).trim(); | |
184 | URL url = new URL(a.absUrl("href")); | |
185 | urls.add(new AbstractMap.SimpleEntry<String, URL>(name, url)); | |
186 | } | |
af1f506f NR |
187 | } |
188 | } | |
189 | ||
af1f506f NR |
190 | return urls; |
191 | } | |
192 | ||
193 | @Override | |
194 | protected String getChapterContent(URL chapUrl, int number, Progress pg) | |
195 | throws IOException { | |
196 | if (pg == null) { | |
197 | pg = new Progress(); | |
198 | } | |
199 | ||
200 | StringBuilder builder = new StringBuilder(); | |
201 | ||
d66deb8d | 202 | InputStream in = Instance.getInstance().getCache().open(chapUrl, this, false); |
af1f506f NR |
203 | try { |
204 | Element pageDoc = DataUtil.load(in, "UTF-8", chapUrl.toString()); | |
6fc76bae NR |
205 | Element content = pageDoc.getElementById("content"); |
206 | Elements linkEls = content.getElementsByTag("img"); | |
af1f506f | 207 | for (Element linkEl : linkEls) { |
12443642 | 208 | if (linkEl.absUrl("src").isEmpty()) { |
6fc76bae | 209 | continue; |
af1f506f | 210 | } |
6fc76bae NR |
211 | |
212 | builder.append("["); | |
12443642 | 213 | builder.append(linkEl.absUrl("src")); |
6fc76bae | 214 | builder.append("]<br/>"); |
af1f506f NR |
215 | } |
216 | ||
217 | } finally { | |
218 | in.close(); | |
219 | } | |
220 | ||
221 | return builder.toString(); | |
222 | } | |
223 | ||
af1f506f NR |
224 | @Override |
225 | protected boolean supports(URL url) { | |
6fc76bae NR |
226 | // URL structure (the projectId is the manga key): |
227 | // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=999 | |
228 | ||
229 | return "mangas-lecture-en-ligne.fr".equals(url.getHost()); | |
af1f506f NR |
230 | } |
231 | } |