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