Convert ArchiveParser to kotlin
This commit is contained in:
1 parent
4f7978070f
commit
52fa8f64cc
3 files changed
+73
-176
No files matched your search
@@ -669,7 +669,7 @@ object EhEngine {
|
||||
code = response.code
|
||||
headers = response.headers
|
||||
body = response.body.string()
|
||||
result = ArchiveParser.parse(body)
|
||||
result = ArchiveParser.parse(body)!!
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
|
||||
@@ -13,199 +13,96 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* 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;
|
||||
import com.hippo.ehviewer.client.exception.NoHAtHClientException;
|
||||
|
||||
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);
|
||||
fun parse(body: String?): Result? {
|
||||
body ?: return null
|
||||
var m = PATTERN_HATH_FORM.matcher(body)
|
||||
if (!m.find()) {
|
||||
return new Result(null, null);
|
||||
return null
|
||||
}
|
||||
String paramOr = m.group(1);
|
||||
|
||||
var archiveList = new ArrayList<Archive>();
|
||||
var d = Jsoup.parse(body);
|
||||
var es = d.select("#db>div>div");
|
||||
for (var e : es) {
|
||||
val paramOr = m.group(1)
|
||||
val archiveList = ArrayList<Archive>()
|
||||
val d = Jsoup.parse(body)
|
||||
val es = d.select("#db>div>div")
|
||||
for (e in es) {
|
||||
try {
|
||||
var btn = e.selectFirst("form>div>input");
|
||||
if (!btn.hasAttr("disabled")) {
|
||||
var res = e.selectFirst("form>input").attr("value");
|
||||
var name = btn.attr("value");
|
||||
var size = e.selectFirst("p>strong").text();
|
||||
var cost = e.selectFirst("div>strong").text().replace(",", "");
|
||||
var item = new Archive(res, name, size, cost, false);
|
||||
archiveList.add(item);
|
||||
val btn = e.selectFirst("form>div>input")
|
||||
if (!btn!!.hasAttr("disabled")) {
|
||||
val res = e.selectFirst("form>input")!!.attr("value")
|
||||
val name = btn.attr("value")
|
||||
val size = e.selectFirst("p>strong")!!.text()
|
||||
val cost = e.selectFirst("div>strong")!!.text().replace(",", "")
|
||||
val item = Archive(res, name, size, cost, false)
|
||||
archiveList.add(item)
|
||||
}
|
||||
} catch (NullPointerException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (ex: NullPointerException) {
|
||||
ex.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
m = PATTERN_HATH_ARCHIVE.matcher(body);
|
||||
m = PATTERN_HATH_ARCHIVE.matcher(body)
|
||||
while (m.find()) {
|
||||
String res = ParserUtils.trim(m.group(1));
|
||||
String name = ParserUtils.trim(m.group(2));
|
||||
String size = ParserUtils.trim(m.group(3));
|
||||
String cost = ParserUtils.trim(m.group(4));
|
||||
var item = new Archive(res, name, size, cost, true);
|
||||
archiveList.add(item);
|
||||
val res = ParserUtils.trim(m.group(1))
|
||||
val name = ParserUtils.trim(m.group(2))
|
||||
val size = ParserUtils.trim(m.group(3))
|
||||
val cost = ParserUtils.trim(m.group(4))
|
||||
val item = Archive(res, name, size, cost, true)
|
||||
archiveList.add(item)
|
||||
}
|
||||
|
||||
return new Result(paramOr, archiveList);
|
||||
return Result(paramOr, archiveList)
|
||||
}
|
||||
|
||||
public static String parseArchiveUrl(String body) throws NoHAtHClientException {
|
||||
Matcher m = PATTERN_NEED_HATH_CLIENT.matcher(body);
|
||||
@Throws(NoHAtHClientException::class)
|
||||
fun parseArchiveUrl(body: String?): String? {
|
||||
body ?: return null
|
||||
val m = PATTERN_NEED_HATH_CLIENT.matcher(body)
|
||||
if (m.find()) {
|
||||
throw new 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";
|
||||
throw NoHAtHClientException("No H@H client")
|
||||
}
|
||||
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
|
||||
return null;
|
||||
}
|
||||
|
||||
public static final class Archive {
|
||||
private final String res;
|
||||
private final String name;
|
||||
private final String size;
|
||||
private final String cost;
|
||||
private final boolean isHAtH;
|
||||
|
||||
public Archive(String res, String name, String size, String cost, boolean isHAtH) {
|
||||
this.res = res;
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
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);
|
||||
class Archive(
|
||||
val res: String,
|
||||
val name: String,
|
||||
val size: String,
|
||||
val cost: String,
|
||||
val isHAtH: Boolean
|
||||
) {
|
||||
fun format(getString: IntFunction<String?>): String {
|
||||
return if (isHAtH) {
|
||||
val costStr = if (cost == "Free") getString.apply(R.string.archive_free) else cost
|
||||
"[H@H] $name [$size] [$costStr]"
|
||||
} else {
|
||||
var nameStr = getString.apply(res.equals("org") ? R.string.archive_original : R.string.archive_resample);
|
||||
var costStr = cost.equals("Free!") ? getString.apply(R.string.archive_free) : cost;
|
||||
return String.format("%s [%s] [%s]", nameStr, size, costStr);
|
||||
val nameStr =
|
||||
getString.apply(if (res == "org") R.string.archive_original else R.string.archive_resample)
|
||||
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 {
|
||||
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 + ']';
|
||||
}
|
||||
|
||||
}
|
||||
class Result(val paramOr: String?, val archiveList: List<Archive>)
|
||||
}
|
||||
@@ -1470,7 +1470,7 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
|
||||
val context = context
|
||||
val activity = mainActivity
|
||||
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 request = EhRequest()
|
||||
request.setMethod(EhClient.METHOD_DOWNLOAD_ARCHIVE)
|
||||
@@ -1504,9 +1504,9 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
|
||||
override fun onSuccess(result: ArchiveParser.Result) {
|
||||
if (mRequest != null) {
|
||||
mRequest = null
|
||||
mArchiveFormParamOr = result.paramOr()
|
||||
mArchiveList = result.archiveList()
|
||||
bind(result.archiveList())
|
||||
mArchiveFormParamOr = result.paramOr
|
||||
mArchiveList = result.archiveList
|
||||
bind(result.archiveList)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user