update nikiroo-utils
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / MangaLel.java
CommitLineData
af1f506f
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.IOException;
4import java.io.InputStream;
af1f506f 5import java.net.URL;
6fc76bae
NR
6import java.text.ParseException;
7import java.text.SimpleDateFormat;
af1f506f
NR
8import java.util.AbstractMap;
9import java.util.ArrayList;
af1f506f
NR
10import java.util.List;
11import java.util.Map.Entry;
12
13import org.jsoup.helper.DataUtil;
14import org.jsoup.nodes.Element;
15import org.jsoup.select.Elements;
16
17import be.nikiroo.fanfix.Instance;
18import be.nikiroo.fanfix.data.MetaData;
19import be.nikiroo.utils.Image;
20import be.nikiroo.utils.Progress;
21import be.nikiroo.utils.StringUtils;
22
23class 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
NR
34 meta.setAuthor(getAuthor());
35 meta.setDate(getDate());
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();
63 Elements tabEls = doc.getElementsByClass("projet-titre");
64
65 String value = "";
66 if (tabEls.size() >= 2) {
67 value = StringUtils.unhtml(tabEls.get(1).text()).trim();
68 }
69
70 return value;
71 }
72
73 private List<String> getTags() {
74 List<String> tags = new ArrayList<String>();
af1f506f
NR
75
76 Element doc = getSourceNode();
6fc76bae 77 Elements tabEls = doc.getElementsByClass("projet-titre");
af1f506f 78
6fc76bae
NR
79 if (tabEls.size() >= 4) {
80 String values = StringUtils.unhtml(tabEls.get(3).text()).trim();
81 for (String value : values.split(",")) {
82 tags.add(value);
af1f506f
NR
83 }
84 }
85
6fc76bae
NR
86 return tags;
87 }
88
89 private String getDate() {
90 Element doc = getSourceNode();
91 Element table = doc.getElementsByClass("table").first();
92
93 // We take the first date we find
94 String value = "";
95 if (table != null) {
96 Elements els;
97 els = table.getElementsByTag("tr");
98 if (els.size() >= 2) {
99 els = els.get(1).getElementsByTag("td");
100 if (els.size() >= 3) {
101 value = StringUtils.unhtml(els.get(2).text()).trim();
af1f506f 102 }
af1f506f 103 }
af1f506f
NR
104 }
105
6fc76bae
NR
106 if (!value.isEmpty()) {
107 try {
108 long time = StringUtils.toTime(value);
109 value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
110 .format(time);
111 } catch (ParseException e) {
112 }
113 }
114
115 return value;
af1f506f
NR
116 }
117
118 @Override
119 protected String getDesc() {
af1f506f 120 Element doc = getSourceNode();
6fc76bae
NR
121 Elements tabEls = doc.getElementsByClass("projet-titre");
122
123 String value = "";
124 if (tabEls.size() >= 5) {
125 value = StringUtils.unhtml(tabEls.get(4).text()).trim();
af1f506f
NR
126 }
127
6fc76bae 128 return value;
af1f506f
NR
129 }
130
131 private Image getCover() {
132 Element doc = getSourceNode();
6fc76bae
NR
133 Element container = doc.getElementsByClass("container").first();
134
135 if (container != null) {
136
137 Elements imgs = container.getElementsByTag("img");
138 Element img = null;
139 if (imgs.size() >= 1) {
140 img = imgs.get(0);
141 if (img.hasClass("banniere-team-projet")) {
142 img = null;
143 if (imgs.size() >= 2) {
144 img = imgs.get(1);
145 }
146 }
147 }
af1f506f 148
6fc76bae
NR
149 if (img != null) {
150 String coverUrl = img.absUrl("src");
af1f506f 151
6fc76bae 152 InputStream coverIn;
af1f506f 153 try {
6fc76bae
NR
154 coverIn = Instance.getCache().open(new URL(coverUrl), this,
155 true);
156 try {
157 return new Image(coverIn);
158 } finally {
159 coverIn.close();
160 }
161 } catch (IOException e) {
162 Instance.getTraceHandler().error(e);
af1f506f 163 }
af1f506f
NR
164 }
165 }
166
167 return null;
168 }
169
170 @Override
6fc76bae
NR
171 protected List<Entry<String, URL>> getChapters(Progress pg)
172 throws IOException {
af1f506f
NR
173 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
174
af1f506f 175 Element doc = getSourceNode();
6fc76bae
NR
176 Element table = doc.getElementsByClass("table").first();
177 if (table != null) {
178 for (Element tr : table.getElementsByTag("tr")) {
179 Element a = tr.getElementsByTag("a").first();
180 if (a != null) {
181 String name = StringUtils.unhtml(a.text()).trim();
182 URL url = new URL(a.absUrl("href"));
183 urls.add(new AbstractMap.SimpleEntry<String, URL>(name, url));
184 }
af1f506f
NR
185 }
186 }
187
af1f506f
NR
188 return urls;
189 }
190
191 @Override
192 protected String getChapterContent(URL chapUrl, int number, Progress pg)
193 throws IOException {
194 if (pg == null) {
195 pg = new Progress();
196 }
197
198 StringBuilder builder = new StringBuilder();
199
200 InputStream in = Instance.getCache().open(chapUrl, this, false);
201 try {
202 Element pageDoc = DataUtil.load(in, "UTF-8", chapUrl.toString());
6fc76bae
NR
203 Element content = pageDoc.getElementById("content");
204 Elements linkEls = content.getElementsByTag("img");
af1f506f 205 for (Element linkEl : linkEls) {
6fc76bae
NR
206 if (linkEl.attr("src").trim().isEmpty()) {
207 continue;
af1f506f 208 }
6fc76bae
NR
209
210 builder.append("[");
211 builder.append(linkEl.absUrl("src").trim());
212 builder.append("]<br/>");
af1f506f
NR
213 }
214
215 } finally {
216 in.close();
217 }
218
219 return builder.toString();
220 }
221
af1f506f
NR
222 @Override
223 protected boolean supports(URL url) {
6fc76bae
NR
224 // URL structure (the projectId is the manga key):
225 // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=999
226
227 return "mangas-lecture-en-ligne.fr".equals(url.getHost());
af1f506f
NR
228 }
229}