1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.util
.ArrayList
;
9 import java
.util
.Map
.Entry
;
10 import java
.util
.Scanner
;
12 import be
.nikiroo
.fanfix
.Instance
;
13 import be
.nikiroo
.fanfix
.data
.Chapter
;
14 import be
.nikiroo
.fanfix
.data
.MetaData
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
16 import be
.nikiroo
.utils
.StringUtils
;
19 * Support class for <a href="http://e621.net/">e621.net</a> and <a
20 * href="http://e926.net/">e926.net</a>, a Furry website supporting comics,
21 * including some of MLP.
23 * <a href="http://e926.net/">e926.net</a> only shows the "clean" images and
24 * comics, but it can be difficult to browse.
28 class E621
extends BasicSupport
{
30 public String
getSourceName() {
35 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
36 MetaData meta
= new MetaData();
38 meta
.setTitle(getTitle(reset(in
)));
39 meta
.setAuthor(getAuthor(source
, reset(in
)));
41 meta
.setTags(new ArrayList
<String
>()); // TODDO ???
42 meta
.setSource(getSourceName());
43 meta
.setUrl(source
.toString());
44 meta
.setPublisher(getSourceName());
45 meta
.setUuid(source
.toString());
49 meta
.setType(getType().toString());
50 meta
.setImageDocument(true);
51 meta
.setCover(getCover(source
));
57 public Story
process(URL url
) throws IOException
{
58 // There is no chapters on e621, just pagination...
59 Story story
= super.process(url
);
61 Chapter only
= new Chapter(1, null);
62 for (Chapter chap
: story
) {
63 only
.getParagraphs().addAll(chap
.getParagraphs());
66 story
.getChapters().clear();
67 story
.getChapters().add(only
);
73 protected boolean supports(URL url
) {
74 String host
= url
.getHost();
75 if (host
.startsWith("www.")) {
76 host
= host
.substring("www.".length());
79 return ("e621.net".equals(host
) || "e926.net".equals(host
))
80 && url
.getPath().startsWith("/pool/");
84 protected boolean isHtml() {
88 private BufferedImage
getCover(URL source
) throws IOException
{
89 InputStream in
= Instance
.getCache().open(source
, this, true);
90 String images
= getChapterContent(new URL(source
.toString() + "?page="
92 if (!images
.isEmpty()) {
93 int pos
= images
.indexOf('\n');
95 images
= images
.substring(1, pos
- 1);
96 return getImage(this, null, images
);
103 private String
getAuthor(URL source
, InputStream in
) throws IOException
{
104 String author
= getLine(in
, "href=\"/post/show/", 0);
105 if (author
!= null) {
106 String key
= "href=\"";
107 int pos
= author
.indexOf(key
);
109 author
= author
.substring(pos
+ key
.length());
110 pos
= author
.indexOf("\"");
112 author
= author
.substring(0, pos
- 1);
113 String page
= source
.getProtocol() + "://"
114 + source
.getHost() + author
;
116 InputStream pageIn
= Instance
.getCache().open(
117 new URL(page
), this, false);
119 key
= "class=\"tag-type-artist\"";
120 author
= getLine(pageIn
, key
, 0);
121 if (author
!= null) {
122 pos
= author
.indexOf("<a href=\"");
124 author
= author
.substring(pos
);
125 pos
= author
.indexOf("</a>");
127 author
= author
.substring(0, pos
);
128 return StringUtils
.unhtml(author
);
135 } catch (Exception e
) {
145 private String
getTitle(InputStream in
) throws IOException
{
146 String title
= getLine(in
, "<title>", 0);
148 int pos
= title
.indexOf('>');
150 title
= title
.substring(pos
+ 1);
151 pos
= title
.indexOf('<');
153 title
= title
.substring(0, pos
);
157 if (title
.startsWith("Pool:")) {
158 title
= title
.substring("Pool:".length());
161 title
= StringUtils
.unhtml(title
).trim();
168 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
169 String desc
= getLine(in
, "margin-bottom: 2em;", 0);
172 StringBuilder builder
= new StringBuilder();
174 boolean inTags
= false;
175 for (char car
: desc
.toCharArray()) {
176 if ((inTags
&& car
== '>') || (!inTags
&& car
== '<')) {
185 return builder
.toString().trim();
192 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
)
194 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
195 int last
= 1; // no pool/show when only one page
197 @SuppressWarnings("resource")
198 Scanner scan
= new Scanner(in
, "UTF-8");
199 scan
.useDelimiter("\\n");
200 while (scan
.hasNext()) {
201 String line
= scan
.next();
202 for (int pos
= line
.indexOf(source
.getPath()); pos
>= 0; pos
= line
203 .indexOf(source
.getPath(), pos
+ source
.getPath().length())) {
204 int equalPos
= line
.indexOf("=", pos
);
205 int quotePos
= line
.indexOf("\"", pos
);
206 if (equalPos
>= 0 && quotePos
> equalPos
) {
207 String snum
= line
.substring(equalPos
+ 1, quotePos
);
209 int num
= Integer
.parseInt(snum
);
213 } catch (NumberFormatException e
) {
219 for (int i
= 1; i
<= last
; i
++) {
220 final String key
= Integer
.toString(i
);
221 final URL value
= new URL(source
.toString() + "?page=" + i
);
222 urls
.add(new Entry
<String
, URL
>() {
223 public URL
setValue(URL value
) {
227 public URL
getValue() {
231 public String
getKey() {
241 protected String
getChapterContent(URL source
, InputStream in
, int number
)
243 StringBuilder builder
= new StringBuilder();
244 String staticSite
= "https://static1.e621.net";
245 if (source
.getHost().contains("e926")) {
246 staticSite
= staticSite
.replace("e621", "e926");
249 String key
= staticSite
+ "/data/preview/";
251 @SuppressWarnings("resource")
252 Scanner scan
= new Scanner(in
, "UTF-8");
253 scan
.useDelimiter("\\n");
254 while (scan
.hasNext()) {
255 String line
= scan
.next();
256 if (line
.contains("class=\"preview\"")) {
257 for (int pos
= line
.indexOf(key
); pos
>= 0; pos
= line
.indexOf(
258 key
, pos
+ key
.length())) {
259 int endPos
= line
.indexOf("\"", pos
);
261 String id
= line
.substring(pos
+ key
.length(), endPos
);
262 id
= staticSite
+ "/data/" + id
;
264 int dotPos
= id
.lastIndexOf(".");
266 id
= id
.substring(0, dotPos
);
269 builder
.append("]\n");
276 return builder
.toString();