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
.Progress
;
17 import be
.nikiroo
.utils
.StringUtils
;
20 * Support class for <a href="http://e621.net/">e621.net</a> and <a
21 * href="http://e926.net/">e926.net</a>, a Furry website supporting comics,
22 * including some of MLP.
24 * <a href="http://e926.net/">e926.net</a> only shows the "clean" images and
25 * comics, but it can be difficult to browse.
29 class E621
extends BasicSupport
{
31 public String
getSourceName() {
36 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
37 MetaData meta
= new MetaData();
39 meta
.setTitle(getTitle(reset(in
)));
40 meta
.setAuthor(getAuthor(source
, reset(in
)));
42 meta
.setTags(new ArrayList
<String
>()); // TODDO ???
43 meta
.setSource(getSourceName());
44 meta
.setUrl(source
.toString());
45 meta
.setPublisher(getSourceName());
46 meta
.setUuid(source
.toString());
49 meta
.setSubject("Furry");
50 meta
.setType(getType().toString());
51 meta
.setImageDocument(true);
52 meta
.setCover(getCover(source
));
58 public Story
process(URL url
, Progress pg
) throws IOException
{
59 // There is no chapters on e621, just pagination...
60 Story story
= super.process(url
, pg
);
62 Chapter only
= new Chapter(1, null);
63 for (Chapter chap
: story
) {
64 only
.getParagraphs().addAll(chap
.getParagraphs());
67 story
.getChapters().clear();
68 story
.getChapters().add(only
);
74 protected boolean supports(URL url
) {
75 String host
= url
.getHost();
76 if (host
.startsWith("www.")) {
77 host
= host
.substring("www.".length());
80 return ("e621.net".equals(host
) || "e926.net".equals(host
))
81 && url
.getPath().startsWith("/pool/");
85 protected boolean isHtml() {
89 private BufferedImage
getCover(URL source
) throws IOException
{
90 InputStream in
= Instance
.getCache().open(source
, this, true);
91 String images
= getChapterContent(new URL(source
.toString() + "?page="
93 if (!images
.isEmpty()) {
94 int pos
= images
.indexOf('\n');
96 images
= images
.substring(1, pos
- 1);
97 return getImage(this, null, images
);
104 private String
getAuthor(URL source
, InputStream in
) throws IOException
{
105 String author
= getLine(in
, "href=\"/post/show/", 0);
106 if (author
!= null) {
107 String key
= "href=\"";
108 int pos
= author
.indexOf(key
);
110 author
= author
.substring(pos
+ key
.length());
111 pos
= author
.indexOf("\"");
113 author
= author
.substring(0, pos
- 1);
114 String page
= source
.getProtocol() + "://"
115 + source
.getHost() + author
;
117 InputStream pageIn
= Instance
.getCache().open(
118 new URL(page
), this, false);
120 key
= "class=\"tag-type-artist\"";
121 author
= getLine(pageIn
, key
, 0);
122 if (author
!= null) {
123 pos
= author
.indexOf("<a href=\"");
125 author
= author
.substring(pos
);
126 pos
= author
.indexOf("</a>");
128 author
= author
.substring(0, pos
);
129 return StringUtils
.unhtml(author
);
136 } catch (Exception e
) {
146 private String
getTitle(InputStream in
) throws IOException
{
147 String title
= getLine(in
, "<title>", 0);
149 int pos
= title
.indexOf('>');
151 title
= title
.substring(pos
+ 1);
152 pos
= title
.indexOf('<');
154 title
= title
.substring(0, pos
);
158 if (title
.startsWith("Pool:")) {
159 title
= title
.substring("Pool:".length());
162 title
= StringUtils
.unhtml(title
).trim();
169 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
170 String desc
= getLine(in
, "margin-bottom: 2em;", 0);
173 StringBuilder builder
= new StringBuilder();
175 boolean inTags
= false;
176 for (char car
: desc
.toCharArray()) {
177 if ((inTags
&& car
== '>') || (!inTags
&& car
== '<')) {
186 return builder
.toString().trim();
193 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
)
195 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
196 int last
= 1; // no pool/show when only one page
198 @SuppressWarnings("resource")
199 Scanner scan
= new Scanner(in
, "UTF-8");
200 scan
.useDelimiter("\\n");
201 while (scan
.hasNext()) {
202 String line
= scan
.next();
203 for (int pos
= line
.indexOf(source
.getPath()); pos
>= 0; pos
= line
204 .indexOf(source
.getPath(), pos
+ source
.getPath().length())) {
205 int equalPos
= line
.indexOf("=", pos
);
206 int quotePos
= line
.indexOf("\"", pos
);
207 if (equalPos
>= 0 && quotePos
> equalPos
) {
208 String snum
= line
.substring(equalPos
+ 1, quotePos
);
210 int num
= Integer
.parseInt(snum
);
214 } catch (NumberFormatException e
) {
220 for (int i
= 1; i
<= last
; i
++) {
221 final String key
= Integer
.toString(i
);
222 final URL value
= new URL(source
.toString() + "?page=" + i
);
223 urls
.add(new Entry
<String
, URL
>() {
224 public URL
setValue(URL value
) {
228 public URL
getValue() {
232 public String
getKey() {
242 protected String
getChapterContent(URL source
, InputStream in
, int number
)
244 StringBuilder builder
= new StringBuilder();
245 String staticSite
= "https://static1.e621.net";
246 if (source
.getHost().contains("e926")) {
247 staticSite
= staticSite
.replace("e621", "e926");
250 String key
= staticSite
+ "/data/preview/";
252 @SuppressWarnings("resource")
253 Scanner scan
= new Scanner(in
, "UTF-8");
254 scan
.useDelimiter("\\n");
255 while (scan
.hasNext()) {
256 String line
= scan
.next();
257 if (line
.contains("class=\"preview")) {
258 for (int pos
= line
.indexOf(key
); pos
>= 0; pos
= line
.indexOf(
259 key
, pos
+ key
.length())) {
260 int endPos
= line
.indexOf("\"", pos
);
262 String id
= line
.substring(pos
+ key
.length(), endPos
);
263 id
= staticSite
+ "/data/" + id
;
265 int dotPos
= id
.lastIndexOf(".");
267 id
= id
.substring(0, dotPos
);
270 builder
.append("]\n");
277 return builder
.toString();