1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.ArrayList
;
8 import java
.util
.Map
.Entry
;
9 import java
.util
.Scanner
;
11 import be
.nikiroo
.fanfix
.Instance
;
12 import be
.nikiroo
.fanfix
.data
.Chapter
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Story
;
15 import be
.nikiroo
.utils
.StringUtils
;
18 * Support class for <a href="http://e621.net/">e621.net</a> and <a
19 * href="http://e926.net/">e926.net</a>, a Furry website supporting comics,
20 * including some of MLP.
22 * <a href="http://e926.net/">e926.net</a> only shows the "clean" images and
23 * comics, but it can be difficult to browse.
27 class E621
extends BasicSupport
{
29 public String
getSourceName() {
34 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
35 MetaData meta
= new MetaData();
37 meta
.setTitle(getTitle(reset(in
)));
38 meta
.setAuthor(getAuthor(source
, reset(in
)));
40 meta
.setTags(new ArrayList
<String
>()); // TODDO ???
41 meta
.setSource(getSourceName());
42 meta
.setUrl(source
.toString());
43 meta
.setPublisher(getSourceName());
44 meta
.setUuid(source
.toString());
48 meta
.setType(getType().toString());
49 meta
.setImageDocument(true);
56 public Story
process(URL url
) throws IOException
{
57 // There is no chapters on e621, just pagination...
58 Story story
= super.process(url
);
60 Chapter only
= new Chapter(1, null);
61 for (Chapter chap
: story
) {
62 only
.getParagraphs().addAll(chap
.getParagraphs());
65 story
.getChapters().clear();
66 story
.getChapters().add(only
);
72 protected boolean supports(URL url
) {
73 String host
= url
.getHost();
74 if (host
.startsWith("www.")) {
75 host
= host
.substring("www.".length());
78 return ("e621.net".equals(host
) || "e926.net".equals(host
))
79 && url
.getPath().startsWith("/pool/");
83 protected boolean isHtml() {
87 private String
getAuthor(URL source
, InputStream in
) throws IOException
{
88 String author
= getLine(in
, "href=\"/post/show/", 0);
90 String key
= "href=\"";
91 int pos
= author
.indexOf(key
);
93 author
= author
.substring(pos
+ key
.length());
94 pos
= author
.indexOf("\"");
96 author
= author
.substring(0, pos
- 1);
97 String page
= source
.getProtocol() + "://"
98 + source
.getHost() + author
;
99 InputStream pageIn
= Instance
.getCache().open(
100 new URL(page
), this, false);
102 key
= "class=\"tag-type-artist\"";
103 author
= getLine(pageIn
, key
, 0);
104 if (author
!= null) {
105 pos
= author
.indexOf("<a href=\"");
107 author
= author
.substring(pos
);
108 pos
= author
.indexOf("</a>");
110 author
= author
.substring(0, pos
);
111 return StringUtils
.unhtml(author
);
125 private String
getTitle(InputStream in
) throws IOException
{
126 String title
= getLine(in
, "<title>", 0);
128 int pos
= title
.indexOf('>');
130 title
= title
.substring(pos
+ 1);
131 pos
= title
.indexOf('<');
133 title
= title
.substring(0, pos
);
137 if (title
.startsWith("Pool:")) {
138 title
= title
.substring("Pool:".length());
141 title
= StringUtils
.unhtml(title
).trim();
148 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
149 String desc
= getLine(in
, "margin-bottom: 2em;", 0);
152 StringBuilder builder
= new StringBuilder();
154 boolean inTags
= false;
155 for (char car
: desc
.toCharArray()) {
156 if ((inTags
&& car
== '>') || (!inTags
&& car
== '<')) {
165 return builder
.toString().trim();
172 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
)
174 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
175 int last
= 1; // no pool/show when only one page
177 @SuppressWarnings("resource")
178 Scanner scan
= new Scanner(in
, "UTF-8");
179 scan
.useDelimiter("\\n");
180 while (scan
.hasNext()) {
181 String line
= scan
.next();
182 for (int pos
= line
.indexOf(source
.getPath()); pos
>= 0; pos
= line
183 .indexOf(source
.getPath(), pos
+ source
.getPath().length())) {
184 int equalPos
= line
.indexOf("=", pos
);
185 int quotePos
= line
.indexOf("\"", pos
);
186 if (equalPos
>= 0 && quotePos
> equalPos
) {
187 String snum
= line
.substring(equalPos
+ 1, quotePos
);
189 int num
= Integer
.parseInt(snum
);
193 } catch (NumberFormatException e
) {
199 for (int i
= 1; i
<= last
; i
++) {
200 final String key
= Integer
.toString(i
);
201 final URL value
= new URL(source
.toString() + "?page=" + i
);
202 urls
.add(new Entry
<String
, URL
>() {
203 public URL
setValue(URL value
) {
207 public URL
getValue() {
211 public String
getKey() {
221 protected String
getChapterContent(URL source
, InputStream in
, int number
)
223 StringBuilder builder
= new StringBuilder();
224 String staticSite
= "https://static1.e621.net";
225 if (source
.getHost().contains("e926")) {
226 staticSite
= staticSite
.replace("e621", "e926");
229 String key
= staticSite
+ "/data/preview/";
231 @SuppressWarnings("resource")
232 Scanner scan
= new Scanner(in
, "UTF-8");
233 scan
.useDelimiter("\\n");
234 while (scan
.hasNext()) {
235 String line
= scan
.next();
236 if (line
.contains("class=\"preview\"")) {
237 for (int pos
= line
.indexOf(key
); pos
>= 0; pos
= line
.indexOf(
238 key
, pos
+ key
.length())) {
239 int endPos
= line
.indexOf("\"", pos
);
241 String id
= line
.substring(pos
+ key
.length(), endPos
);
242 id
= staticSite
+ "/data/" + id
;
244 int dotPos
= id
.lastIndexOf(".");
246 id
= id
.substring(0, dotPos
);
249 builder
.append("]\n");
256 return builder
.toString();