Improve parser
This commit is contained in:
1 parent
cd852f4003
commit
89f9df6e44
9 files changed
+172
-149
No files matched your search
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hippo.ehviewer.client;
|
||||
|
||||
import com.hippo.yorozuya.AssertUtils;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DetailUrlParser {
|
||||
|
||||
public int gid;
|
||||
public String token;
|
||||
|
||||
public void parser(String url) throws Exception {
|
||||
AssertUtils.assertNotNull("Url is null when parse detail url", url);
|
||||
|
||||
Pattern p = Pattern.compile("/(\\d+)/(\\w+)");
|
||||
Matcher m = p.matcher(url);
|
||||
if (m.find()) {
|
||||
gid = Integer.parseInt(m.group(1));
|
||||
token = m.group(2);
|
||||
} else {
|
||||
throw new EhException("Can not parse detail url");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,45 @@ public class GalleryDetailParser {
|
||||
private static final DateFormat WEB_COMMENT_DATE_FORMAT = new SimpleDateFormat("dd MMMMM yyyy, HH:mm z", Locale.US);
|
||||
private static final DateFormat OUT_COMMENT_DATE_FORMAT = GalleryBase.DEFAULT_DATE_FORMAT;
|
||||
|
||||
private static final Pattern ERROR_PATTERN = Pattern.compile("<div class=\"d\">\n<p>([^<]+)</p>");
|
||||
private static final Pattern DETAIL_PATTERN = Pattern.compile(
|
||||
"var gid = (\\d+)" // 1 gid
|
||||
+ ".+?"
|
||||
+ "var token = \"([a-z0-9A]+)\"" // 2 token
|
||||
+ ".+?"
|
||||
+ "<div id=\"gd1\"><img src=\"([^\"]+)\"[^<>]+></div>" // 3 thumb
|
||||
+ "</div>"
|
||||
+ "<div id=\"gd2\">"
|
||||
+ "<h1 id=\"gn\">([^<>]+)</h1>" // 4 title
|
||||
+ "<h1 id=\"gj\">([^<>]*)</h1>" // 5 title_jpn might be empty string
|
||||
+ "</div>"
|
||||
+ ".+?"
|
||||
+ "<a[^<>]*onclick=\"return popUp\\('([^']+)'[^)]+\\)\">Torrent Download \\( (\\d+) \\)</a>" // 6 torrentUrl, 7 torrentCount
|
||||
+ ".+?"
|
||||
+ "<div id=\"gdc\"><a[^<>]+><[^<>]*alt=\"([\\w\\-]+)\"[^<>]*></a></div>" // 8 category
|
||||
+ "<div id=\"gdn\"><a[^<>]+>([^<>]+)</a>" // 9 uploader
|
||||
+ ".+?"
|
||||
+ "<tr><td[^<>]*>Posted:</td><td[^<>]*>([\\w\\-\\s:]+)</td></tr>" // 10 posted
|
||||
+ "<tr><td[^<>]*>Parent:</td><td[^<>]*>(?:<a[^<>]*>)?([^<>]+)(?:</a>)?</td></tr>" // 11 parent
|
||||
+ "<tr><td[^<>]*>Visible:</td><td[^<>]*>([^<>]+)</td></tr>" // 12 visible
|
||||
+ "<tr><td[^<>]*>Language:</td><td[^<>]*>([^<>]+)(?:<span[^<>]*>[^<>]*</span>)?</td></tr>" // 13 language
|
||||
+ "<tr><td[^<>]*>File Size:</td><td[^<>]*>([^<>]+)(?:<span[^<>]*>([^<>]+)</span>)?</td></tr>" // 14 File size, 15 resize
|
||||
+ "<tr><td[^<>]*>Length:</td><td[^<>]*>([\\d,]+) pages</td></tr>" // 16 pageCount
|
||||
+ "<tr><td[^<>]*>Favorited:</td><[^<>]*>([^<>]+)</td></tr>" // 17 Favorite times "([\d,]+) times" or "Once" or "Never"
|
||||
+ ".+?"
|
||||
+ "<td id=\"grt3\"><span id=\"rating_count\">([\\d,]+)</span></td>" // 18 ratedTimes
|
||||
+ "</tr>"
|
||||
+ "<tr><td[^<>]*>([^<>]+)</td>" // 19 rating "Average: x.xx" or "Not Yet Rated"
|
||||
+ ".+?"
|
||||
+ "<a id=\"favoritelink\"[^<>]*>(.+?)</a>", Pattern.DOTALL); // 20 isFavored "Favorite Gallery" for favorite
|
||||
private static final Pattern TAG_PATTERN = Pattern.compile("<tr><td[^<>]+>([\\w\\s]+):</td><td>(?:<div[^<>]+><a[^<>]+>[\\w\\s]+</a></div>)+</td></tr>");
|
||||
private static final Pattern COMMENT_PATTERN = Pattern.compile("<div class=\"c3\">Posted on ([^<>]+) by: <a[^<>]+>([^<>]+)</a>.+?<div class=\"c6\"[^>]*>(.+?)</div><div class=\"c[78]\"");
|
||||
private static final Pattern TAG_GROUP_PATTERN = Pattern.compile("<div[^<>]+><a[^<>]+>([\\w\\s]+)</a></div>");
|
||||
public static final Pattern PAGES_PATTERN = Pattern.compile("<tr><td[^<>]*>Length:</td><td[^<>]*>([\\d,]+) pages</td></tr>");
|
||||
public static final Pattern PREVIEW_PAGES_PATTERN = Pattern.compile("<td[^>]+><a[^>]+>([\\d,]+)</a></td><td[^>]+>(?:<a[^>]+>)?>(?:</a>)?</td>");
|
||||
private static final Pattern NORMAL_PREVIEW_PATTERN = Pattern.compile("<div[^<>]*class=\"gdtm\"[^<>]*><div[^<>]*width:(\\d+)[^<>]*height:(\\d+)[^<>]*\\((.+?)\\)[^<>]*-(\\d+)px[^<>]*><a[^<>]*href=\"(.+?)\"[^<>]*>");
|
||||
private static final Pattern LARGE_PREVIEW_PATTERN = Pattern.compile("<div class=\"gdtl\".+?<a href=\"(.+?)\"><img.+?src=\"(.+?)\"");
|
||||
|
||||
static {
|
||||
WEB_COMMENT_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
}
|
||||
@@ -87,8 +126,7 @@ public class GalleryDetailParser {
|
||||
Matcher m;
|
||||
|
||||
// Error info
|
||||
p = Pattern.compile("<div class=\"d\">\n<p>([^<]+)</p>");
|
||||
m = p.matcher(body);
|
||||
m = ERROR_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
throw new EhException(m.group(1));
|
||||
}
|
||||
@@ -96,36 +134,7 @@ public class GalleryDetailParser {
|
||||
GalleryDetail galleryDetail = new GalleryDetail();
|
||||
|
||||
if ((request & REQUEST_DETAIL) != 0) {
|
||||
p = Pattern.compile("var gid = (\\d+)" // 1 gid
|
||||
+ ".+?"
|
||||
+ "var token = \"([a-z0-9A]+)\"" // 2 token
|
||||
+ ".+?"
|
||||
+ "<div id=\"gd1\"><img src=\"([^\"]+)\"[^<>]+></div>" // 3 thumb
|
||||
+ "</div>"
|
||||
+ "<div id=\"gd2\">"
|
||||
+ "<h1 id=\"gn\">([^<>]+)</h1>" // 4 title
|
||||
+ "<h1 id=\"gj\">([^<>]*)</h1>" // 5 title_jpn might be empty string
|
||||
+ "</div>"
|
||||
+ ".+?"
|
||||
+ "<a[^<>]*onclick=\"return popUp\\('([^']+)'[^)]+\\)\">Torrent Download \\( (\\d+) \\)</a>" // 6 torrentUrl, 7 torrentCount
|
||||
+ ".+?"
|
||||
+ "<div id=\"gdc\"><a[^<>]+><[^<>]*alt=\"([\\w\\-]+)\"[^<>]*></a></div>" // 8 category
|
||||
+ "<div id=\"gdn\"><a[^<>]+>([^<>]+)</a>" // 9 uploader
|
||||
+ ".+?"
|
||||
+ "<tr><td[^<>]*>Posted:</td><td[^<>]*>([\\w\\-\\s:]+)</td></tr>" // 10 posted
|
||||
+ "<tr><td[^<>]*>Parent:</td><td[^<>]*>(?:<a[^<>]*>)?([^<>]+)(?:</a>)?</td></tr>" // 11 parent
|
||||
+ "<tr><td[^<>]*>Visible:</td><td[^<>]*>([^<>]+)</td></tr>" // 12 visible
|
||||
+ "<tr><td[^<>]*>Language:</td><td[^<>]*>([^<>]+)(?:<span[^<>]*>[^<>]*</span>)?</td></tr>" // 13 language
|
||||
+ "<tr><td[^<>]*>File Size:</td><td[^<>]*>([^<>]+)(?:<span[^<>]*>([^<>]+)</span>)?</td></tr>" // 14 File size, 15 resize
|
||||
+ "<tr><td[^<>]*>Length:</td><td[^<>]*>([\\d,]+) pages</td></tr>" // 16 pageCount
|
||||
+ "<tr><td[^<>]*>Favorited:</td><[^<>]*>([^<>]+)</td></tr>" // 17 Favorite times "([\d,]+) times" or "Once" or "Never"
|
||||
+ ".+?"
|
||||
+ "<td id=\"grt3\"><span id=\"rating_count\">([\\d,]+)</span></td>" // 18 ratedTimes
|
||||
+ "</tr>"
|
||||
+ "<tr><td[^<>]*>([^<>]+)</td>" // 19 rating "Average: x.xx" or "Not Yet Rated"
|
||||
+ ".+?"
|
||||
+ "<a id=\"favoritelink\"[^<>]*>(.+?)</a>", Pattern.DOTALL); // 20 isFavored "Favorite Gallery" for favorite
|
||||
m = p.matcher(body);
|
||||
m = DETAIL_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
galleryDetail.gid = ParserUtils.parseInt(m.group(1));
|
||||
galleryDetail.token = ParserUtils.trim(m.group(2));
|
||||
@@ -184,8 +193,7 @@ public class GalleryDetailParser {
|
||||
|
||||
if ((request & REQUEST_TAG) != 0) {
|
||||
galleryDetail.tags = new LinkedList<>();
|
||||
p = Pattern.compile("<tr><td[^<>]+>([\\w\\s]+):</td><td>(?:<div[^<>]+><a[^<>]+>[\\w\\s]+</a></div>)+</td></tr>");
|
||||
m = p.matcher(body);
|
||||
m = TAG_PATTERN.matcher(body);
|
||||
while (m.find()) {
|
||||
TagGroup tagGroup = new TagGroup();
|
||||
tagGroup.groupName = ParserUtils.trim(m.group(1));
|
||||
@@ -209,8 +217,7 @@ public class GalleryDetailParser {
|
||||
|
||||
// Get comment
|
||||
if ((request & REQUEST_COMMENT) != 0) {
|
||||
p = Pattern.compile("<div class=\"c3\">Posted on ([^<>]+) by: <a[^<>]+>([^<>]+)</a>.+?<div class=\"c6\"[^>]*>(.+?)</div><div class=\"c[78]\"");
|
||||
m = p.matcher(body);
|
||||
m = COMMENT_PATTERN.matcher(body);
|
||||
galleryDetail.comments = new LinkedList<>();
|
||||
while (m.find()) {
|
||||
String webDateString = ParserUtils.trim(m.group(1));
|
||||
@@ -229,16 +236,14 @@ public class GalleryDetailParser {
|
||||
}
|
||||
|
||||
private static void parseTagGroup(TagGroup tagGroup, String body) {
|
||||
Pattern p = Pattern.compile("<div[^<>]+><a[^<>]+>([\\w\\s]+)</a></div>");
|
||||
Matcher m = p.matcher(body);
|
||||
Matcher m = TAG_GROUP_PATTERN.matcher(body);
|
||||
while (m.find()) {
|
||||
tagGroup.addTag(ParserUtils.trim(m.group(1)));
|
||||
}
|
||||
}
|
||||
|
||||
public static int parsePreviewPages(String body) throws ParseException {
|
||||
Pattern p = Pattern.compile("<td[^>]+><a[^>]+>([\\d,]+)</a></td><td[^>]+>(?:<a[^>]+>)?>(?:</a>)?</td>");
|
||||
Matcher m = p.matcher(body);
|
||||
Matcher m = PREVIEW_PAGES_PATTERN.matcher(body);
|
||||
int previewPages = -1;
|
||||
if (m.find()) {
|
||||
previewPages = ParserUtils.parseInt(m.group(1));
|
||||
@@ -252,8 +257,7 @@ public class GalleryDetailParser {
|
||||
}
|
||||
|
||||
public static int parsePages(String body) throws ParseException {
|
||||
Pattern p = Pattern.compile("<tr><td[^<>]*>Length:</td><td[^<>]*>([\\d,]+) pages</td></tr>");
|
||||
Matcher m = p.matcher(body);
|
||||
Matcher m = PAGES_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
return ParserUtils.parseInt(m.group(1));
|
||||
} else {
|
||||
@@ -285,8 +289,7 @@ public class GalleryDetailParser {
|
||||
}
|
||||
|
||||
private static NormalPreviewSet parseNormalPreview(String body) {
|
||||
Pattern p = Pattern.compile("<div[^<>]*class=\"gdtm\"[^<>]*><div[^<>]*width:(\\d+)[^<>]*height:(\\d+)[^<>]*\\((.+?)\\)[^<>]*-(\\d+)px[^<>]*><a[^<>]*href=\"(.+?)\"[^<>]*>");
|
||||
Matcher m = p.matcher(body);
|
||||
Matcher m = NORMAL_PREVIEW_PATTERN.matcher(body);
|
||||
NormalPreviewSet normalPreviewSet = new NormalPreviewSet();
|
||||
while (m.find()) {
|
||||
normalPreviewSet.addItem(m.group(3), Integer.parseInt(m.group(4)), 0,
|
||||
@@ -297,8 +300,7 @@ public class GalleryDetailParser {
|
||||
}
|
||||
|
||||
private static LargePreviewSet parseLargePreview(String body) {
|
||||
Pattern p = Pattern.compile("<div class=\"gdtl\".+?<a href=\"(.+?)\"><img.+?src=\"(.+?)\"");
|
||||
Matcher m = p.matcher(body);
|
||||
Matcher m = LARGE_PREVIEW_PATTERN.matcher(body);
|
||||
LargePreviewSet largePreviewSet = new LargePreviewSet();
|
||||
while (m.find()) {
|
||||
largePreviewSet.addItem(m.group(2), m.group(1));
|
||||
|
||||
@@ -21,13 +21,14 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class GalleryDetailUrlParser {
|
||||
|
||||
public static final Pattern URL_PATTERN = Pattern.compile("/(\\d+)/(\\w+)");
|
||||
|
||||
public static Result parser(String url) {
|
||||
if (url == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile("/(\\d+)/(\\w+)");
|
||||
Matcher m = p.matcher(url);
|
||||
Matcher m = URL_PATTERN.matcher(url);
|
||||
if (m.find()) {
|
||||
Result result = new Result();
|
||||
result.gid = Integer.parseInt(m.group(1));
|
||||
|
||||
@@ -32,6 +32,30 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class GalleryListParser {
|
||||
|
||||
static final Pattern PAGES_PATTERN = Pattern.compile("<a[^<>]+>([\\d]+)</a></td><td[^<>]+>(?:<a[^<>]+>)?&");
|
||||
static final Pattern PATTERN = Pattern.compile(
|
||||
"<td class=\"itdc\">(?:<a.+?>)?<img.+?alt=\"(.+?)\".+?/>(?:</a>)?</td>" // category
|
||||
+ "<td.+?>(.+?)</td>" // posted
|
||||
+ "<td.+?><div.+?><div.+?height:(\\d+)px; width:(\\d+)px\">"
|
||||
+ "(?:<img.+?src=\"(.+?)\".+?alt=\"(.+?)\" style.+?/>"
|
||||
+ "|init~([^<>\"~]+~[^<>\"~]+)~([^<>]+))" // thumb and title
|
||||
+ "</div>"
|
||||
+ ".+?"
|
||||
+ "<div class=\"it5\"><a href=\"([^<>\"]+)\"[^<>]+>(.+?)</a></div>" // url and title
|
||||
+ ".+?"
|
||||
+ "<div class=\"ir it4r\" style=\"([^<>\"]+)\">" // rating
|
||||
+ ".+?"
|
||||
+ "<td class=\"itu\"><div><a.+?>(.+?)</a>"); // uploader
|
||||
static final Pattern LOFI_PATTERN = Pattern.compile(
|
||||
"<td class=\"ii\"><a href=\"(.+?)\">" // detail url
|
||||
+ "<img src=\"(.+?)\".+?/>" // thumb url
|
||||
+ ".+?<a class=\"b\" href=\".+?\">(.+?)</a>" // title
|
||||
+ ".+?<td class=\"ik ip\">Posted:</td><td class=\"ip\">(.+?)</td>" // Posted and uploader
|
||||
+ "</tr><tr><td class=\"ik\">Category:</td><td>(.+?)</td>" // Category
|
||||
+ "</tr><tr><td class=\"ik\">Tags:</td><td>(.+?)</td>" // Tags
|
||||
+ "</tr><tr><td class=\"ik\">Rating:</td><td class=\"ir\">(.+?)</td>"); // rating
|
||||
static final Pattern RATING_PATTERN = Pattern.compile("\\d+px");
|
||||
|
||||
public static class Result {
|
||||
|
||||
public static final int CURRENT_PAGE_IS_LAST = -1;
|
||||
@@ -62,12 +86,10 @@ public class GalleryListParser {
|
||||
|
||||
private static Result parse(String body) throws EhException {
|
||||
Result result = new Result();
|
||||
Pattern p;
|
||||
Matcher m;
|
||||
|
||||
// pages
|
||||
p = Pattern.compile("<a[^<>]+>([\\d]+)</a></td><td[^<>]+>(?:<a[^<>]+>)?&");
|
||||
m = p.matcher(body);
|
||||
m = PAGES_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
result.pages = ParserUtils.parseInt(m.group(1));
|
||||
} else if (body.contains("No hits found</p>")) {
|
||||
@@ -81,19 +103,7 @@ public class GalleryListParser {
|
||||
List<GalleryInfo> list = new ArrayList<>(25);
|
||||
result.galleryInfos = list;
|
||||
|
||||
p = Pattern.compile("<td class=\"itdc\">(?:<a.+?>)?<img.+?alt=\"(.+?)\".+?/>(?:</a>)?</td>" // category
|
||||
+ "<td.+?>(.+?)</td>" // posted
|
||||
+ "<td.+?><div.+?><div.+?height:(\\d+)px; width:(\\d+)px\">"
|
||||
+ "(?:<img.+?src=\"(.+?)\".+?alt=\"(.+?)\" style.+?/>"
|
||||
+ "|init~([^<>\"~]+~[^<>\"~]+)~([^<>]+))" // thumb and title
|
||||
+ "</div>"
|
||||
+ ".+?"
|
||||
+ "<div class=\"it5\"><a href=\"([^<>\"]+)\"[^<>]+>(.+?)</a></div>" // url and title
|
||||
+ ".+?"
|
||||
+ "<div class=\"ir it4r\" style=\"([^<>\"]+)\">" // rating
|
||||
+ ".+?"
|
||||
+ "<td class=\"itu\"><div><a.+?>(.+?)</a>"); // uploader
|
||||
m = p.matcher(body);
|
||||
m = PATTERN.matcher(body);
|
||||
while (m.find()) {
|
||||
GalleryInfo gi = new GalleryInfo();
|
||||
|
||||
@@ -110,9 +120,7 @@ public class GalleryListParser {
|
||||
gi.title = ParserUtils.trim(m.group(6));
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern
|
||||
.compile("/(\\d+)/(\\w+)");
|
||||
Matcher matcher = pattern.matcher(m.group(9));
|
||||
Matcher matcher = GalleryDetailUrlParser.URL_PATTERN.matcher(m.group(9));
|
||||
if (matcher.find()) {
|
||||
gi.gid = ParserUtils.parseInt(matcher.group(1));
|
||||
gi.token = ParserUtils.trim(matcher.group(2));
|
||||
@@ -120,7 +128,7 @@ public class GalleryListParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
gi.rating = NumberUtils.parseFloatSafely(getRate(m.group(11)), Float.NaN);
|
||||
gi.rating = NumberUtils.parseFloatSafely(getRating(m.group(11)), Float.NaN);
|
||||
gi.uploader = ParserUtils.trim(m.group(12));
|
||||
gi.generateSLang();
|
||||
|
||||
@@ -137,27 +145,22 @@ public class GalleryListParser {
|
||||
|
||||
private static Result parseLofi(String body) throws Exception {
|
||||
Result result = new Result();
|
||||
Pattern p;
|
||||
Matcher m;
|
||||
|
||||
List<GalleryInfo> list = new ArrayList<>(25);
|
||||
result.galleryInfos = list;
|
||||
p = Pattern.compile("<td class=\"ii\"><a href=\"(.+?)\">" // detail url
|
||||
+ "<img src=\"(.+?)\".+?/>" // thumb url
|
||||
+ ".+?<a class=\"b\" href=\".+?\">(.+?)</a>" // title
|
||||
+ ".+?<td class=\"ik ip\">Posted:</td><td class=\"ip\">(.+?)</td>" // Posted and uploader
|
||||
+ "</tr><tr><td class=\"ik\">Category:</td><td>(.+?)</td>" // Category
|
||||
+ "</tr><tr><td class=\"ik\">Tags:</td><td>(.+?)</td>" // Tags
|
||||
+ "</tr><tr><td class=\"ik\">Rating:</td><td class=\"ir\">(.+?)</td>"); // rating
|
||||
m = p.matcher(body);
|
||||
DetailUrlParser dup = new DetailUrlParser();
|
||||
m = LOFI_PATTERN.matcher(body);
|
||||
String[] pau = new String[2];
|
||||
while (m.find()) {
|
||||
LofiGalleryInfo lgi = new LofiGalleryInfo();
|
||||
|
||||
dup.parser(m.group(1));
|
||||
lgi.gid = dup.gid;
|
||||
lgi.token = dup.token;
|
||||
GalleryDetailUrlParser.Result result1 = GalleryDetailUrlParser.parser(m.group(1));
|
||||
if (result1 != null) {
|
||||
lgi.gid = result1.gid;
|
||||
lgi.token = result1.token;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
lgi.thumb = ParserUtils.trim(m.group(2));
|
||||
lgi.title = ParserUtils.trim(m.group(3));
|
||||
@@ -203,9 +206,8 @@ public class GalleryListParser {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String getRate(String rawRate) {
|
||||
Pattern p = Pattern.compile("\\d+px");
|
||||
Matcher m = p.matcher(rawRate);
|
||||
private static String getRating(String rawRate) {
|
||||
Matcher m = RATING_PATTERN.matcher(rawRate);
|
||||
int num1;
|
||||
int num2;
|
||||
int rate = 5;
|
||||
|
||||
@@ -21,19 +21,19 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class SignInParser {
|
||||
|
||||
static final Pattern NAME_PATTERN = Pattern.compile("<p>You are now logged in as: (.+?)<br />");
|
||||
static final Pattern ERROR_PATTERN = Pattern.compile(
|
||||
"(?:<h4>The error returned was:</h4>\\s*<p>(.+?)</p>)"
|
||||
+ "|(?:<span class=\"postcolor\">(.+?)</span>)");
|
||||
|
||||
public static String parse(String body) throws Exception {
|
||||
Pattern p;
|
||||
Matcher m;
|
||||
|
||||
p = Pattern.compile("<p>You are now logged in as: (.+?)<br />");
|
||||
m = p.matcher(body);
|
||||
m = NAME_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
return m.group(1);
|
||||
} else {
|
||||
p = Pattern.compile("(?:<h4>The error returned was:</h4>\\s*<p>(.+?)</p>)"
|
||||
+ "|(?:<span class=\"postcolor\">(.+?)</span>)");
|
||||
m = p.matcher(body);
|
||||
|
||||
m = ERROR_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
throw new EhException(m.group(1) == null ? m.group(2) : m.group(1));
|
||||
} else {
|
||||
|
||||
@@ -444,6 +444,10 @@ public class ImageHandler {
|
||||
|
||||
public abstract void onSave(long receivedSize);
|
||||
|
||||
public boolean isCancelled() {
|
||||
return mCancel;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
mCancel = true;
|
||||
if (mRequest != null) {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.hippo.ehviewer.gallery;
|
||||
|
||||
import com.hippo.ehviewer.client.EhConfig;
|
||||
import com.hippo.ehviewer.client.GalleryDetailParser;
|
||||
import com.hippo.ehviewer.client.ParseException;
|
||||
import com.hippo.ehviewer.client.ParserUtils;
|
||||
|
||||
@@ -27,6 +28,9 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class SpiderParser {
|
||||
|
||||
private static final Pattern NORMAL_PREVIEW_PATTERN = Pattern.compile("<div class=\"gdtm\"[^>]+><div[^>]+><a href=\"[^\"]+/(\\w+)/(\\d+)-(\\d+)\">");
|
||||
private static final Pattern LARGE_PREVIEW_PATTERN = Pattern.compile("<div class=\"gdtl\"[^>]+><a href=\"[^\"]+/(\\w+)/(\\d+)-(\\d+)\">");
|
||||
|
||||
public static class Result {
|
||||
public int pages;
|
||||
public int previewPages;
|
||||
@@ -44,16 +48,14 @@ public class SpiderParser {
|
||||
Matcher m;
|
||||
|
||||
if (request == REQUEST_ALL) {
|
||||
p = Pattern.compile("<tr><td[^<>]*>Length:</td><td[^<>]*>([\\d,]+) pages</td></tr>");
|
||||
m = p.matcher(body);
|
||||
m = GalleryDetailParser.PAGES_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
result.pages = ParserUtils.parseInt(m.group(1));
|
||||
} else {
|
||||
throw new ParseException("Parse pages error", body);
|
||||
}
|
||||
|
||||
p = Pattern.compile("<td[^>]+><a[^>]+>([\\d,]+)</a></td><td[^>]+>(?:<a[^>]+>)?>(?:</a>)?</td>");
|
||||
m = p.matcher(body);
|
||||
m = GalleryDetailParser.PREVIEW_PAGES_PATTERN.matcher(body);
|
||||
if (m.find()) {
|
||||
result.previewPages = ParserUtils.parseInt(m.group(1));
|
||||
} else {
|
||||
@@ -64,11 +66,11 @@ public class SpiderParser {
|
||||
List<String> list;
|
||||
if (body.contains("<div class=\"gdtm\"")) {
|
||||
result.previewSize = EhConfig.PREVIEW_SIZE_NORMAL;
|
||||
p = Pattern.compile("<div class=\"gdtm\"[^>]+><div[^>]+><a href=\"[^\"]+/(\\w+)/(\\d+)-(\\d+)\">");
|
||||
p = NORMAL_PREVIEW_PATTERN;
|
||||
list = new ArrayList<>(40);
|
||||
} else {
|
||||
result.previewSize = EhConfig.PREVIEW_SIZE_LARGE;
|
||||
p = Pattern.compile("<div class=\"gdtl\"[^>]+><a href=\"[^\"]+/(\\w+)/(\\d+)-(\\d+)\">");
|
||||
p = LARGE_PREVIEW_PATTERN;
|
||||
list = new ArrayList<>(20);
|
||||
}
|
||||
result.tokens = list;
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.hippo.yorozuya.Say;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.LinkedList;
|
||||
@@ -766,7 +767,13 @@ public class SpiderQueen implements Runnable {
|
||||
mSpiderListener.onSpiderSucceed(index);
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
if (mCurrentSaveHelper != null && mCurrentSaveHelper.isCancelled()) {
|
||||
mPageStates.lazySet(index, PAGE_STATE_FAILED);
|
||||
mSpiderListener.onSpiderFailed(index, new IOException("Cancelled"));
|
||||
return;
|
||||
} else {
|
||||
exception = e;
|
||||
}
|
||||
} finally {
|
||||
mCurrentSaveHelper = null;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener, M
|
||||
private static final float[] RIGHT_AREA = {2.0f / 3.0f, 0.0f, 1.0f, 1f};
|
||||
private static final float[] CENTER_AREA = {1.0f / 3.0f, 2.0f / 5.0f, 2.0f / 3.0f, 3.0f / 5.0f};
|
||||
|
||||
private static final Interpolator SMOOTH_SCALER_INTERPOLATOR = new OvershootInterpolator();
|
||||
private static final Interpolator SMOOTH_SCALER_INTERPOLATOR = new OvershootInterpolator(0.7f);
|
||||
|
||||
private boolean mEnableRequestLayout = true;
|
||||
|
||||
@@ -181,6 +181,7 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener, M
|
||||
if (mMode != mode) {
|
||||
Mode oldMode = mMode;
|
||||
mMode = mode;
|
||||
resetPageMoving();
|
||||
superRequestLayout();
|
||||
|
||||
mListener.onSetMode(mode);
|
||||
@@ -191,6 +192,13 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener, M
|
||||
}
|
||||
}
|
||||
|
||||
private void resetPageMoving() {
|
||||
mSmoothScroller.forceStop();
|
||||
mSmoothScaler.forceStop();
|
||||
mLayoutOffset = 0;
|
||||
mHaveChangePage = false;
|
||||
}
|
||||
|
||||
public void setAdapter(Adapter adapter) {
|
||||
mAdapter = adapter;
|
||||
}
|
||||
@@ -222,18 +230,60 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener, M
|
||||
}
|
||||
}
|
||||
|
||||
public void scrollToPrevious() {
|
||||
scrollToPage(mFirstShownIndex - 1);
|
||||
}
|
||||
|
||||
public void scrollToNext() {
|
||||
scrollToPage(mFirstShownIndex + 1);
|
||||
}
|
||||
|
||||
public void scrollToLeft() {
|
||||
if (mMode == Mode.RIGHT_TO_LEFT) {
|
||||
scrollToNext();
|
||||
} else {
|
||||
scrollToPrevious();
|
||||
}
|
||||
}
|
||||
|
||||
public void scrollToRight() {
|
||||
if (mMode == Mode.RIGHT_TO_LEFT) {
|
||||
scrollToPrevious();
|
||||
} else {
|
||||
scrollToNext();
|
||||
}
|
||||
}
|
||||
|
||||
public void scrollToPage(int page) {
|
||||
scrollToPageInternal(page, false);
|
||||
}
|
||||
|
||||
private void scrollToPageInternal(int page, boolean internal) {
|
||||
mSmoothScroller.forceStop();
|
||||
mSmoothScaler.forceStop();
|
||||
|
||||
if (page < 0 || page >= mAdapter.getPages()) {
|
||||
if (mMode == Mode.NONE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (page < 0) {
|
||||
if (mMode == Mode.LEFT_TO_RIGHT) {
|
||||
mEdgeView.onPull(getWidth() * 2, EdgeView.LEFT);
|
||||
} else if (mMode == Mode.RIGHT_TO_LEFT) {
|
||||
mEdgeView.onPull(getWidth() * 2, EdgeView.RIGHT);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (page >= mAdapter.getPages()) {
|
||||
if (mMode == Mode.LEFT_TO_RIGHT) {
|
||||
mEdgeView.onPull(getWidth() * 2, EdgeView.RIGHT);
|
||||
} else if (mMode == Mode.RIGHT_TO_LEFT) {
|
||||
mEdgeView.onPull(getWidth() * 2, EdgeView.LEFT);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
mSmoothScroller.forceStop();
|
||||
mSmoothScaler.forceStop();
|
||||
|
||||
mFirstShownIndex = page;
|
||||
mLayoutOffset = 0;
|
||||
requestFill();
|
||||
@@ -598,8 +648,7 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener, M
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changeSize, int left, int top, int right, int bottom) {
|
||||
mSmoothScroller.forceStop();
|
||||
mSmoothScaler.forceStop();
|
||||
|
||||
mEdgeView.layout(left, top, right, bottom);
|
||||
|
||||
/* TODO set all seen UNKNOWN_SEEN in order to setScaleAndOffset
|
||||
@@ -611,9 +660,6 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener, M
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
mLayoutOffset = 0;
|
||||
mHaveChangePage = false;
|
||||
fill();
|
||||
|
||||
if (changeSize) {
|
||||
|
||||
Reference in new issue
Block a user