Convert ArchiveParser to kotlin
This commit is contained in:
1 parent
4f7978070f
commit
52fa8f64cc
3 files changed
+74
-177
No files matched your search
@@ -669,7 +669,7 @@ object EhEngine {
|
|||||||
code = response.code
|
code = response.code
|
||||||
headers = response.headers
|
headers = response.headers
|
||||||
body = response.body.string()
|
body = response.body.string()
|
||||||
result = ArchiveParser.parse(body)
|
result = ArchiveParser.parse(body)!!
|
||||||
}
|
}
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
ExceptionUtils.throwIfFatal(e)
|
ExceptionUtils.throwIfFatal(e)
|
||||||
|
|||||||
@@ -13,199 +13,96 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
package com.hippo.ehviewer.client.parser
|
||||||
|
|
||||||
package com.hippo.ehviewer.client.parser;
|
import com.hippo.ehviewer.R
|
||||||
|
import com.hippo.ehviewer.client.exception.NoHAtHClientException
|
||||||
|
import org.jsoup.Jsoup
|
||||||
|
import java.util.function.IntFunction
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
object ArchiveParser {
|
||||||
|
private val PATTERN_ARCHIVE_URL =
|
||||||
|
Pattern.compile("<strong>(.*)</strong>.*<a href=\"([^\"]*)\">Click Here To Start Downloading</a>")
|
||||||
|
private val PATTERN_HATH_FORM =
|
||||||
|
Pattern.compile("<form id=\"hathdl_form\" action=\"[^\"]*?or=([^=\"]*?)\" method=\"post\">")
|
||||||
|
private val PATTERN_HATH_ARCHIVE =
|
||||||
|
Pattern.compile("<p><a href=\"[^\"]*\" onclick=\"return do_hathdl\\('([0-9]+|org)'\\)\">([^<]+)</a></p>\\s*<p>([\\w. ]+)</p>\\s*<p>([\\w. ]+)</p>")
|
||||||
|
private val PATTERN_NEED_HATH_CLIENT =
|
||||||
|
Pattern.compile("You must have a H@H client assigned to your account to use this feature\\.")
|
||||||
|
|
||||||
import com.hippo.ehviewer.R;
|
fun parse(body: String?): Result? {
|
||||||
import com.hippo.ehviewer.client.exception.NoHAtHClientException;
|
body ?: return null
|
||||||
|
var m = PATTERN_HATH_FORM.matcher(body)
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.IntFunction;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class ArchiveParser {
|
|
||||||
|
|
||||||
private static final Pattern PATTERN_ARCHIVE_URL = Pattern.compile("<strong>(.*)</strong>.*<a href=\"([^\"]*)\">Click Here To Start Downloading</a>");
|
|
||||||
private static final Pattern PATTERN_HATH_FORM = Pattern.compile("<form id=\"hathdl_form\" action=\"[^\"]*?or=([^=\"]*?)\" method=\"post\">");
|
|
||||||
private static final Pattern PATTERN_HATH_ARCHIVE = Pattern.compile("<p><a href=\"[^\"]*\" onclick=\"return do_hathdl\\('([0-9]+|org)'\\)\">([^<]+)</a></p>\\s*<p>([\\w. ]+)</p>\\s*<p>([\\w. ]+)</p>");
|
|
||||||
private static final Pattern PATTERN_NEED_HATH_CLIENT = Pattern.compile("You must have a H@H client assigned to your account to use this feature\\.");
|
|
||||||
|
|
||||||
public static Result parse(String body) {
|
|
||||||
Matcher m = PATTERN_HATH_FORM.matcher(body);
|
|
||||||
if (!m.find()) {
|
if (!m.find()) {
|
||||||
return new Result(null, null);
|
return null
|
||||||
}
|
}
|
||||||
String paramOr = m.group(1);
|
val paramOr = m.group(1)
|
||||||
|
val archiveList = ArrayList<Archive>()
|
||||||
var archiveList = new ArrayList<Archive>();
|
val d = Jsoup.parse(body)
|
||||||
var d = Jsoup.parse(body);
|
val es = d.select("#db>div>div")
|
||||||
var es = d.select("#db>div>div");
|
for (e in es) {
|
||||||
for (var e : es) {
|
|
||||||
try {
|
try {
|
||||||
var btn = e.selectFirst("form>div>input");
|
val btn = e.selectFirst("form>div>input")
|
||||||
if (!btn.hasAttr("disabled")) {
|
if (!btn!!.hasAttr("disabled")) {
|
||||||
var res = e.selectFirst("form>input").attr("value");
|
val res = e.selectFirst("form>input")!!.attr("value")
|
||||||
var name = btn.attr("value");
|
val name = btn.attr("value")
|
||||||
var size = e.selectFirst("p>strong").text();
|
val size = e.selectFirst("p>strong")!!.text()
|
||||||
var cost = e.selectFirst("div>strong").text().replace(",", "");
|
val cost = e.selectFirst("div>strong")!!.text().replace(",", "")
|
||||||
var item = new Archive(res, name, size, cost, false);
|
val item = Archive(res, name, size, cost, false)
|
||||||
archiveList.add(item);
|
archiveList.add(item)
|
||||||
}
|
}
|
||||||
} catch (NullPointerException ex) {
|
} catch (ex: NullPointerException) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
m = PATTERN_HATH_ARCHIVE.matcher(body)
|
||||||
m = PATTERN_HATH_ARCHIVE.matcher(body);
|
|
||||||
while (m.find()) {
|
while (m.find()) {
|
||||||
String res = ParserUtils.trim(m.group(1));
|
val res = ParserUtils.trim(m.group(1))
|
||||||
String name = ParserUtils.trim(m.group(2));
|
val name = ParserUtils.trim(m.group(2))
|
||||||
String size = ParserUtils.trim(m.group(3));
|
val size = ParserUtils.trim(m.group(3))
|
||||||
String cost = ParserUtils.trim(m.group(4));
|
val cost = ParserUtils.trim(m.group(4))
|
||||||
var item = new Archive(res, name, size, cost, true);
|
val item = Archive(res, name, size, cost, true)
|
||||||
archiveList.add(item);
|
archiveList.add(item)
|
||||||
}
|
}
|
||||||
|
return Result(paramOr, archiveList)
|
||||||
return new Result(paramOr, archiveList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String parseArchiveUrl(String body) throws NoHAtHClientException {
|
@Throws(NoHAtHClientException::class)
|
||||||
Matcher m = PATTERN_NEED_HATH_CLIENT.matcher(body);
|
fun parseArchiveUrl(body: String?): String? {
|
||||||
|
body ?: return null
|
||||||
|
val m = PATTERN_NEED_HATH_CLIENT.matcher(body)
|
||||||
if (m.find()) {
|
if (m.find()) {
|
||||||
throw new NoHAtHClientException("No H@H client");
|
throw NoHAtHClientException("No H@H client")
|
||||||
}
|
|
||||||
|
|
||||||
var d = Jsoup.parse(body);
|
|
||||||
var a = d.selectFirst("#continue>a[href]");
|
|
||||||
if (a != null) {
|
|
||||||
return a.attr("href") + "?start=1";
|
|
||||||
}
|
}
|
||||||
|
val d = Jsoup.parse(body)
|
||||||
|
val a = d.selectFirst("#continue>a[href]")
|
||||||
|
return if (a != null) {
|
||||||
|
a.attr("href") + "?start=1"
|
||||||
|
} else null
|
||||||
|
|
||||||
// TODO: Check more errors
|
// TODO: Check more errors
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class Archive {
|
class Archive(
|
||||||
private final String res;
|
val res: String,
|
||||||
private final String name;
|
val name: String,
|
||||||
private final String size;
|
val size: String,
|
||||||
private final String cost;
|
val cost: String,
|
||||||
private final boolean isHAtH;
|
val isHAtH: Boolean
|
||||||
|
) {
|
||||||
public Archive(String res, String name, String size, String cost, boolean isHAtH) {
|
fun format(getString: IntFunction<String?>): String {
|
||||||
this.res = res;
|
return if (isHAtH) {
|
||||||
this.name = name;
|
val costStr = if (cost == "Free") getString.apply(R.string.archive_free) else cost
|
||||||
this.size = size;
|
"[H@H] $name [$size] [$costStr]"
|
||||||
this.cost = cost;
|
|
||||||
this.isHAtH = isHAtH;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String format(IntFunction<String> getString) {
|
|
||||||
if (isHAtH) {
|
|
||||||
var costStr = cost.equals("Free") ? getString.apply(R.string.archive_free) : cost;
|
|
||||||
return String.format("[H@H] %s [%s] [%s]", name, size, costStr);
|
|
||||||
} else {
|
} else {
|
||||||
var nameStr = getString.apply(res.equals("org") ? R.string.archive_original : R.string.archive_resample);
|
val nameStr =
|
||||||
var costStr = cost.equals("Free!") ? getString.apply(R.string.archive_free) : cost;
|
getString.apply(if (res == "org") R.string.archive_original else R.string.archive_resample)
|
||||||
return String.format("%s [%s] [%s]", nameStr, size, costStr);
|
val costStr = if (cost == "Free!") getString.apply(R.string.archive_free) else cost
|
||||||
|
"$nameStr [$size] [$costStr]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String res() {
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String name() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String size() {
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String cost() {
|
|
||||||
return cost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isHAtH() {
|
|
||||||
return isHAtH;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (obj == this) return true;
|
|
||||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
|
||||||
var that = (Archive) obj;
|
|
||||||
return Objects.equals(this.res, that.res) &&
|
|
||||||
Objects.equals(this.name, that.name) &&
|
|
||||||
Objects.equals(this.size, that.size) &&
|
|
||||||
Objects.equals(this.cost, that.cost) &&
|
|
||||||
this.isHAtH == that.isHAtH;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(res, name, size, cost, isHAtH);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Archive[" +
|
|
||||||
"res=" + res + ", " +
|
|
||||||
"name=" + name + ", " +
|
|
||||||
"size=" + size + ", " +
|
|
||||||
"cost=" + cost + ", " +
|
|
||||||
"isHAtH=" + isHAtH + ']';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class Result {
|
class Result(val paramOr: String?, val archiveList: List<Archive>)
|
||||||
private final String paramOr;
|
}
|
||||||
private final List<Archive> archiveList;
|
|
||||||
|
|
||||||
public Result(String paramOr, List<Archive> archiveList) {
|
|
||||||
this.paramOr = paramOr;
|
|
||||||
this.archiveList = archiveList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String paramOr() {
|
|
||||||
return paramOr;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Archive> archiveList() {
|
|
||||||
return archiveList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (obj == this) return true;
|
|
||||||
if (obj == null || obj.getClass() != this.getClass()) return false;
|
|
||||||
var that = (Result) obj;
|
|
||||||
return Objects.equals(this.paramOr, that.paramOr) &&
|
|
||||||
Objects.equals(this.archiveList, that.archiveList);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(paramOr, archiveList);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Result[" +
|
|
||||||
"paramOr=" + paramOr + ", " +
|
|
||||||
"archiveList=" + archiveList + ']';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1470,7 +1470,7 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
|
|||||||
val context = context
|
val context = context
|
||||||
val activity = mainActivity
|
val activity = mainActivity
|
||||||
if (null != context && null != activity && null != mArchiveList && position < mArchiveList!!.size) {
|
if (null != context && null != activity && null != mArchiveList && position < mArchiveList!!.size) {
|
||||||
val res = mArchiveList!![position].res()
|
val res = mArchiveList!![position].res
|
||||||
val isHAtH = mArchiveList!![position].isHAtH
|
val isHAtH = mArchiveList!![position].isHAtH
|
||||||
val request = EhRequest()
|
val request = EhRequest()
|
||||||
request.setMethod(EhClient.METHOD_DOWNLOAD_ARCHIVE)
|
request.setMethod(EhClient.METHOD_DOWNLOAD_ARCHIVE)
|
||||||
@@ -1504,9 +1504,9 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
|
|||||||
override fun onSuccess(result: ArchiveParser.Result) {
|
override fun onSuccess(result: ArchiveParser.Result) {
|
||||||
if (mRequest != null) {
|
if (mRequest != null) {
|
||||||
mRequest = null
|
mRequest = null
|
||||||
mArchiveFormParamOr = result.paramOr()
|
mArchiveFormParamOr = result.paramOr
|
||||||
mArchiveList = result.archiveList()
|
mArchiveList = result.archiveList
|
||||||
bind(result.archiveList())
|
bind(result.archiveList)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user