doc
[nikiroo-utils.git] / searchable / MangaLel.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.searchable;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.URL;
6import java.net.URLEncoder;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.List;
10
11import org.jsoup.helper.DataUtil;
12import org.jsoup.nodes.Document;
13import org.jsoup.nodes.Element;
14import org.jsoup.select.Elements;
15
16import be.nikiroo.fanfix.Instance;
17import be.nikiroo.fanfix.bundles.StringId;
18import be.nikiroo.fanfix.data.MetaData;
19import be.nikiroo.fanfix.supported.SupportType;
20import be.nikiroo.utils.Image;
21import be.nikiroo.utils.StringUtils;
22
23class MangaLel extends BasicSearchable {
24 private String BASE_URL = "http://mangas-lecture-en-ligne.fr/index_lel.php";
25
26 public MangaLel() {
27 super(SupportType.MANGA_LEL);
28 }
29
30 @Override
31 public List<SearchableTag> getTags() throws IOException {
32 List<SearchableTag> tags = new ArrayList<SearchableTag>();
33
34 String url = BASE_URL + "?page=recherche";
35 Document doc = load(url, false);
36
37 Element genre = doc.getElementsByClass("genre").first();
38 if (genre != null) {
39 for (Element el : genre.getElementsByAttributeValueStarting("for",
40 "genre")) {
41 tags.add(new SearchableTag(el.attr("for"), el.text(), true));
42 }
43 }
44
45 return tags;
46 }
47
48 @Override
49 public void fillTag(SearchableTag tag) throws IOException {
50 // Tags are always complete
51 }
52
53 @Override
54 public List<MetaData> search(String search, int page) throws IOException {
55 String url = BASE_URL + "?nomProjet="
56 + URLEncoder.encode(search, "utf-8")
57 + "&nomAuteur=&nomTeam=&page=recherche&truc=truc";
58
59 // No pagination
60 return getResults(url);
61 }
62
63 @Override
64 public List<MetaData> search(SearchableTag tag, int page)
65 throws IOException {
66 String url = BASE_URL + "?nomProjet=&nomAuteur=&nomTeam=&"
67 + tag.getId() + "=on&page=recherche&truc=truc";
68
69 // No pagination
70 return getResults(url);
71 }
72
73 @Override
74 public int searchPages(String search) throws IOException {
75 // No pagination
76 return 1;
77 }
78
79 @Override
80 public int searchPages(SearchableTag tag) throws IOException {
81 if (tag.isLeaf()) {
82 // No pagination
83 return 1;
84 }
85
86 return 0;
87 }
88
89 private List<MetaData> getResults(String sourceUrl) throws IOException {
90 List<MetaData> metas = new ArrayList<MetaData>();
91
92 Document doc = DataUtil.load(
93 Instance.getCache().open(new URL(sourceUrl), getSupport(),
94 false), "UTF-8", sourceUrl);
95
96 for (Element result : doc.getElementsByClass("rechercheAffichage")) {
97 Element a = result.getElementsByTag("a").first();
98 if (a != null) {
99 int projectId = -1;
100
101 MetaData meta = new MetaData();
102
103 // Target:
104 // http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet=218
105
106 // a.absUrl("href"):
107 // http://mangas-lecture-en-ligne.fr/index_lel?onCommence=oui&idChapitre=2805
108
109 // ...but we need the PROJECT id, not the CHAPTER id -> use
110 // <IMG>
111
112 Elements infos = result.getElementsByClass("texte");
113 if (infos != null) {
114 String[] tab = infos.outerHtml().split("<br>");
115
116 meta.setLang("fr");
117 meta.setSource(getType().getSourceName());
118 meta.setPublisher(getType().getSourceName());
119 meta.setType(getType().toString());
120 meta.setSubject("manga");
121 meta.setImageDocument(true);
122 meta.setTitle(getVal(tab, 0));
123 meta.setAuthor(getVal(tab, 1));
124 meta.setTags(Arrays.asList(getVal(tab, 2).split(" ")));
125
126 meta.setResume(getSupport()
127 .makeChapter(
128 new URL(sourceUrl),
129 0,
130 Instance.getTrans().getString(
131 StringId.DESCRIPTION),
132 getVal(tab, 5)));
133 }
134
135 Element img = result.getElementsByTag("img").first();
136 if (img != null) {
137 try {
138 String[] tab = img.attr("src").split("/");
139 String str = tab[tab.length - 1];
140 tab = str.split("\\.");
141 str = tab[0];
142 projectId = Integer.parseInt(str);
143
144 String coverUrl = img.absUrl("src");
145 try {
146 InputStream in = Instance.getCache().open(
147 new URL(coverUrl), getSupport(), true);
148 try {
149 meta.setCover(new Image(in));
150 } finally {
151 in.close();
152 }
153 } catch (Exception e) {
154 // Happen often on MangaLEL...
155 Instance.getTraceHandler().trace(
156 "Cannot download cover for MangaLEL story in search mode: "
157 + meta.getTitle());
158 }
159 } catch (Exception e) {
160 // no project id... cannot use the story :(
161 Instance.getTraceHandler().error(
162 "Cannot find ProjectId for MangaLEL story in search mode: "
163 + meta.getTitle());
164 }
165 }
166
167 if (projectId >= 0) {
168 meta.setUrl("http://mangas-lecture-en-ligne.fr/index_lel.php?page=presentationProjet&idProjet="
169 + projectId);
170 meta.setUuid(meta.getUrl());
171 metas.add(meta);
172 }
173 }
174 }
175
176 return metas;
177 }
178
179 private String getVal(String[] tab, int i) {
180 String val = "";
181
182 if (i < tab.length) {
183 val = StringUtils.unhtml(tab[i]);
184 int pos = val.indexOf(":");
185 if (pos >= 0) {
186 val = val.substring(pos + 1).trim();
187 }
188 }
189
190 return val;
191 }
192}