update from master
[fanfix.git] / 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.setSource(getType().getSourceName());
38 meta.setUrl(getSource().toString());
39 meta.setPublisher(getType().getSourceName());
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();
53 Element h4 = doc.getElementsByTag("h4").first();
54 if (h4 != null) {
55 return StringUtils.unhtml(h4.text()).trim();
56 }
57
58 return null;
59 }
60
61 private String getAuthor() {
62 Element doc = getSourceNode();
63 Element tabEls = doc.getElementsByClass("presentation-projet").first();
64 if (tabEls != null) {
65 String[] tab = tabEls.outerHtml().split("<br>");
66 return getVal(tab, 1);
67 }
68
69 return "";
70 }
71
72 private List<String> getTags() {
73 Element doc = getSourceNode();
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);
80 }
81 return tags;
82 }
83
84 return new ArrayList<String>();
85
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();
101 }
102 }
103 }
104
105 return value;
106 }
107
108 @Override
109 protected String getDesc() {
110 Element doc = getSourceNode();
111 Element tabEls = doc.getElementsByClass("presentation-projet").first();
112 if (tabEls != null) {
113 String[] tab = tabEls.outerHtml().split("<br>");
114 return getVal(tab, 4);
115 }
116
117 return "";
118 }
119
120 private Image getCover() {
121 Element doc = getSourceNode();
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 }
137
138 if (img != null) {
139 String coverUrl = img.absUrl("src");
140
141 try {
142 InputStream coverIn = Instance.getInstance().getCache()
143 .open(new URL(coverUrl), this, true);
144 try {
145 Image ii = new Image(coverIn);
146 if (ii.getSize() == 0) {
147 ii.close();
148 throw new IOException("Empty image not accepted");
149 }
150
151 return ii;
152 } finally {
153 coverIn.close();
154 }
155 } catch (IOException e) {
156 Instance.getInstance().getTraceHandler().error(e);
157 }
158 }
159 }
160
161 return null;
162 }
163
164 private String getVal(String[] tab, int i) {
165 String val = "";
166
167 if (i < tab.length) {
168 val = StringUtils.unhtml(tab[i]);
169 int pos = val.indexOf(":");
170 if (pos >= 0) {
171 val = val.substring(pos + 1).trim();
172 }
173 }
174
175 return val;
176 }
177
178 @Override
179 protected List<Entry<String, URL>> getChapters(Progress pg)
180 throws IOException {
181 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
182
183 Element doc = getSourceNode();
184 Element table = doc.getElementsByClass("table").first();
185 if (table != null) {
186 for (Element tr : table.getElementsByTag("tr")) {
187 Element a = tr.getElementsByTag("a").first();
188 if (a != null) {
189 String name = StringUtils.unhtml(a.text()).trim();
190 URL url = new URL(a.absUrl("href"));
191 urls.add(new AbstractMap.SimpleEntry<String, URL>(name, url));
192 }
193 }
194 }
195
196 return urls;
197 }
198
199 @Override
200 protected String getChapterContent(URL chapUrl, int number, Progress pg)
201 throws IOException {
202 if (pg == null) {
203 pg = new Progress();
204 }
205
206 StringBuilder builder = new StringBuilder();
207
208 InputStream in = Instance.getInstance().getCache().open(chapUrl, this, false);
209 try {
210 Element pageDoc = DataUtil.load(in, "UTF-8", chapUrl.toString());
211 Element content = pageDoc.getElementById("content");
212 Elements linkEls = content.getElementsByTag("img");
213 for (Element linkEl : linkEls) {
214 if (linkEl.absUrl("src").isEmpty()) {
215 continue;
216 }
217
218 builder.append("[");
219 builder.append(linkEl.absUrl("src"));
220 builder.append("]<br/>");
221 }
222
223 } finally {
224 in.close();
225 }
226
227 return builder.toString();
228 }
229
230 @Override
231 protected boolean supports(URL url) {
232 // URL structure (the projectId is the manga key):
233 // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=999
234
235 return "mangas-lecture-en-ligne.fr".equals(url.getHost());
236 }
237 }