Commit | Line | Data |
---|---|---|
08fe2e33 NR |
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.util.ArrayList; | |
7 | import java.util.List; | |
8 | import java.util.Map.Entry; | |
9 | import java.util.Scanner; | |
10 | ||
11 | import be.nikiroo.fanfix.Instance; | |
12 | import be.nikiroo.fanfix.data.Chapter; | |
13 | import be.nikiroo.fanfix.data.Story; | |
14 | import be.nikiroo.utils.StringUtils; | |
15 | ||
16 | /** | |
17 | * Support class for <a href="http://e621.net/">e621.net</a> and <a | |
18 | * href="http://e926.net/">e926.net</a>, a Furry website supporting comics, | |
19 | * including some of MLP. | |
20 | * <p> | |
21 | * <a href="http://e926.net/">e926.net</a> only shows the "clean" images and | |
22 | * comics, but it can be difficult to browse. | |
23 | * | |
24 | * @author niki | |
25 | */ | |
26 | class E621 extends BasicSupport { | |
27 | @Override | |
28 | public String getSourceName() { | |
29 | return "e621.net"; | |
30 | } | |
31 | ||
32 | @Override | |
33 | public boolean isImageDocument(URL source, InputStream in) { | |
34 | return true; | |
35 | } | |
36 | ||
37 | @Override | |
38 | public Story process(URL url) throws IOException { | |
39 | // There is no chapters on e621, just pagination... | |
40 | Story story = super.process(url); | |
41 | ||
42 | Chapter only = new Chapter(1, null); | |
43 | for (Chapter chap : story) { | |
44 | only.getParagraphs().addAll(chap.getParagraphs()); | |
45 | } | |
46 | ||
47 | story.getChapters().clear(); | |
48 | story.getChapters().add(only); | |
49 | ||
50 | return story; | |
51 | } | |
52 | ||
53 | @Override | |
54 | protected boolean supports(URL url) { | |
55 | String host = url.getHost(); | |
56 | if (host.startsWith("www.")) { | |
57 | host = host.substring("www.".length()); | |
58 | } | |
59 | ||
60 | return ("e621.net".equals(host) || "e926.net".equals(host)) | |
61 | && url.getPath().startsWith("/pool/"); | |
62 | } | |
63 | ||
64 | @Override | |
65 | protected boolean isHtml() { | |
66 | return true; | |
67 | } | |
68 | ||
69 | @Override | |
70 | protected String getAuthor(URL source, InputStream in) throws IOException { | |
71 | String author = getLine(in, "href=\"/post/show/", 0); | |
72 | if (author != null) { | |
73 | String key = "href=\""; | |
74 | int pos = author.indexOf(key); | |
75 | if (pos >= 0) { | |
76 | author = author.substring(pos + key.length()); | |
77 | pos = author.indexOf("\""); | |
78 | if (pos >= 0) { | |
79 | author = author.substring(0, pos - 1); | |
80 | String page = source.getProtocol() + "://" | |
81 | + source.getHost() + author; | |
82 | InputStream pageIn = Instance.getCache().open( | |
83 | new URL(page), this, false); | |
84 | try { | |
85 | key = "class=\"tag-type-artist\""; | |
86 | author = getLine(pageIn, key, 0); | |
87 | if (author != null) { | |
88 | pos = author.indexOf("<a href=\""); | |
89 | if (pos >= 0) { | |
90 | author = author.substring(pos); | |
91 | pos = author.indexOf("</a>"); | |
92 | if (pos >= 0) { | |
93 | author = author.substring(0, pos); | |
94 | return StringUtils.unhtml(author); | |
95 | } | |
96 | } | |
97 | } | |
98 | } finally { | |
99 | pageIn.close(); | |
100 | } | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | return null; | |
106 | } | |
107 | ||
108 | @Override | |
109 | protected String getDate(URL source, InputStream in) throws IOException { | |
110 | return null; | |
111 | } | |
112 | ||
113 | @Override | |
114 | protected String getSubject(URL source, InputStream in) throws IOException { | |
115 | return null; | |
116 | } | |
117 | ||
118 | @Override | |
119 | protected URL getCover(URL source, InputStream in) throws IOException { | |
120 | return null; | |
121 | } | |
122 | ||
123 | @Override | |
124 | protected String getTitle(URL source, InputStream in) throws IOException { | |
125 | String title = getLine(in, "<title>", 0); | |
126 | if (title != null) { | |
127 | int pos = title.indexOf('>'); | |
128 | if (pos >= 0) { | |
129 | title = title.substring(pos + 1); | |
130 | pos = title.indexOf('<'); | |
131 | if (pos >= 0) { | |
132 | title = title.substring(0, pos); | |
133 | } | |
134 | } | |
135 | ||
136 | if (title.startsWith("Pool:")) { | |
137 | title = title.substring("Pool:".length()); | |
138 | } | |
139 | ||
140 | title = title.trim(); | |
141 | } | |
142 | ||
143 | return title; | |
144 | } | |
145 | ||
146 | @Override | |
147 | protected String getDesc(URL source, InputStream in) throws IOException { | |
148 | String desc = getLine(in, "margin-bottom: 2em;", 0); | |
149 | ||
150 | if (desc != null) { | |
151 | StringBuilder builder = new StringBuilder(); | |
152 | ||
153 | boolean inTags = false; | |
154 | for (char car : desc.toCharArray()) { | |
155 | if ((inTags && car == '>') || (!inTags && car == '<')) { | |
156 | inTags = !inTags; | |
157 | } | |
158 | ||
159 | if (inTags) { | |
160 | builder.append(car); | |
161 | } | |
162 | } | |
163 | ||
164 | return builder.toString().trim(); | |
165 | } | |
166 | ||
167 | return null; | |
168 | } | |
169 | ||
170 | @Override | |
171 | protected List<Entry<String, URL>> getChapters(URL source, InputStream in) | |
172 | throws IOException { | |
173 | List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>(); | |
174 | int last = 1; // no pool/show when only one page | |
175 | ||
176 | @SuppressWarnings("resource") | |
177 | Scanner scan = new Scanner(in, "UTF-8"); | |
178 | scan.useDelimiter("\\n"); | |
179 | while (scan.hasNext()) { | |
180 | String line = scan.next(); | |
181 | for (int pos = line.indexOf(source.getPath()); pos >= 0; pos = line | |
182 | .indexOf(source.getPath(), pos + source.getPath().length())) { | |
183 | int equalPos = line.indexOf("=", pos); | |
184 | int quotePos = line.indexOf("\"", pos); | |
185 | if (equalPos >= 0 && quotePos > equalPos) { | |
186 | String snum = line.substring(equalPos + 1, quotePos); | |
187 | try { | |
188 | int num = Integer.parseInt(snum); | |
189 | if (num > last) { | |
190 | last = num; | |
191 | } | |
192 | } catch (NumberFormatException e) { | |
193 | } | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | for (int i = 1; i <= last; i++) { | |
199 | final String key = Integer.toString(i); | |
200 | final URL value = new URL(source.toString() + "?page=" + i); | |
201 | urls.add(new Entry<String, URL>() { | |
202 | public URL setValue(URL value) { | |
203 | return null; | |
204 | } | |
205 | ||
206 | public URL getValue() { | |
207 | return value; | |
208 | } | |
209 | ||
210 | public String getKey() { | |
211 | return key; | |
212 | } | |
213 | }); | |
214 | } | |
215 | ||
216 | return urls; | |
217 | } | |
218 | ||
219 | @Override | |
220 | protected String getChapterContent(URL source, InputStream in, int number) | |
221 | throws IOException { | |
222 | StringBuilder builder = new StringBuilder(); | |
223 | String staticSite = "https://static1.e621.net"; | |
224 | if (source.getHost().contains("e926")) { | |
225 | staticSite = staticSite.replace("e621", "e926"); | |
226 | } | |
227 | ||
228 | String key = staticSite + "/data/preview/"; | |
229 | ||
230 | @SuppressWarnings("resource") | |
231 | Scanner scan = new Scanner(in, "UTF-8"); | |
232 | scan.useDelimiter("\\n"); | |
233 | while (scan.hasNext()) { | |
234 | String line = scan.next(); | |
235 | if (line.contains("class=\"preview\"")) { | |
236 | for (int pos = line.indexOf(key); pos >= 0; pos = line.indexOf( | |
237 | key, pos + key.length())) { | |
238 | int endPos = line.indexOf("\"", pos); | |
239 | if (endPos >= 0) { | |
240 | String id = line.substring(pos + key.length(), endPos); | |
241 | id = staticSite + "/data/" + id; | |
242 | ||
243 | int dotPos = id.lastIndexOf("."); | |
244 | if (dotPos >= 0) { | |
245 | id = id.substring(0, dotPos); | |
246 | builder.append("["); | |
247 | builder.append(id); | |
248 | builder.append("]\n"); | |
249 | } | |
250 | } | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
255 | return builder.toString(); | |
256 | } | |
257 | } |