diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index b33df68cc..ae734e2ee 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -14,6 +14,7 @@ -keep class androidx.viewpager.widget.ViewPager$LayoutParams { int position; } -keep class com.hippo.ehviewer.client.parser.Torrent { *; } +-keep class com.hippo.ehviewer.client.parser.GalleryListResult { *; } -keep class com.hippo.ehviewer.client.parser.Limits { *; } -keep class com.hippo.ehviewer.client.data.BaseGalleryInfo { *; } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt index 88a37a765..1eee9ec63 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt @@ -1,18 +1,3 @@ -/* - * Copyright 2016 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.parser import com.hippo.ehviewer.R @@ -35,7 +20,7 @@ object FavoritesParser { class Result( val catArray: Array, val countArray: IntArray, - galleryListResult: GalleryListParser.Result, + galleryListResult: GalleryListResult, ) { val prev = galleryListResult.prev val next = galleryListResult.next diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt index b347b60b4..ee2d437f5 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt @@ -1,108 +1,25 @@ -/* - * Copyright 2016 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.parser import com.hippo.ehviewer.EhDB import com.hippo.ehviewer.client.data.BaseGalleryInfo import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED -import com.hippo.ehviewer.client.exception.ParseException -import com.hippo.ehviewer.util.ExceptionUtils -import com.hippo.ehviewer.util.toIntOrDefault -import org.jsoup.Jsoup -import org.jsoup.nodes.Document -import java.util.regex.Pattern object GalleryListParser { - private val TAG = GalleryListParser::class.java.simpleName - private const val NO_UNFILTERED_TEXT = - "No unfiltered results in this page range. You either requested an invalid page or used too aggressive filters." - private val PATTERN_NEXT_PAGE = Pattern.compile("p=(\\d+)") - private val PATTERN_PREV = Pattern.compile("prev=(\\d+(-\\d+)?)") - private val PATTERN_NEXT = Pattern.compile("next=(\\d+(-\\d+)?)") - - suspend fun parse(body: String): Result { - val d = Jsoup.parse(body) - d.outputSettings().prettyPrint(false) - return parse(d, body) - } - - suspend fun parse(d: Document, body: String): Result { - val result = Result() - try { - val prev = d.getElementById("uprev") - val next = d.getElementById("unext") - assert(prev != null) - assert(next != null) - val matcherPrev = PATTERN_PREV.matcher(prev!!.attr("href")) - val matcherNext = PATTERN_NEXT.matcher(next!!.attr("href")) - if (matcherPrev.find()) result.prev = matcherPrev.group(1) - if (matcherNext.find()) result.next = matcherNext.group(1) - } catch (e: Throwable) { - ExceptionUtils.throwIfFatal(e) - result.noWatchedTags = body.contains("

You do not have any watched tags") - if (body.contains("No hits found

")) { - return result + suspend fun parse(body: String) = parseGalleryInfoList(body).apply { + galleryInfoList.onEach { + if (it.favoriteSlot == NOT_FAVORITED && EhDB.containLocalFavorites(it.gid)) { + it.favoriteSlot = LOCAL_FAVORITED } + it.generateSLang() } - try { // For toplists - val ptt = d.getElementsByClass("ptt").first() - if (ptt != null) { - val es = ptt.child(0).child(0).children() - result.pages = es[es.size - 2].text().trim { it <= ' ' }.toInt() - var e = es[es.size - 1] - if (e != null) { - e = e.children().first() - if (e != null) { - val href = e.attr("href") - val matcher = PATTERN_NEXT_PAGE.matcher(href) - if (matcher.find()) { - result.nextPage = matcher.group(1)!!.toIntOrDefault(0) - } - } - } - } - } catch (e: Throwable) { - e.printStackTrace() - } - try { - result.galleryInfoList.addAll( - parseGalleryInfoList(body).onEach { - if (it.favoriteSlot == NOT_FAVORITED && EhDB.containLocalFavorites(it.gid)) { - it.favoriteSlot = LOCAL_FAVORITED - } - it.generateSLang() - }, - ) - } catch (e: Throwable) { - ExceptionUtils.throwIfFatal(e) - e.printStackTrace() - throw ParseException("Can't parse gallery list", body) - } - return result - } - - class Result { - var pages = 0 - var nextPage = 0 - var prev: String? = null - var next: String? = null - var noWatchedTags = false - val galleryInfoList = mutableListOf() } } -private external fun parseGalleryInfoList(e: String): ArrayList +class GalleryListResult( + val prev: String?, + val next: String?, + val galleryInfoList: ArrayList, +) + +private external fun parseGalleryInfoList(e: String): GalleryListResult diff --git a/app/src/main/rust/src/parser/list.rs b/app/src/main/rust/src/parser/list.rs index 4441f781d..482da675f 100644 --- a/app/src/main/rust/src/parser/list.rs +++ b/app/src/main/rust/src/parser/list.rs @@ -8,6 +8,7 @@ use jnix_macros::IntoJava; use parse_jni_string; use quick_xml::escape::unescape; use std::borrow::Cow; +use std::ops::Index; use tl::{Node, Parser}; use {get_element_by_id, get_vdom_first_element_by_class_name}; use {get_first_element_by_class_name, query_childs_first_match_attr}; @@ -39,6 +40,15 @@ pub struct BaseGalleryInfo { favoriteNote: Option, } +#[derive(Default, IntoJava)] +#[allow(non_snake_case)] +#[jnix(package = "com.hippo.ehviewer.client.parser")] +pub struct GalleryListResult { + prev: Option, + next: Option, + galleryInfoList: Vec, +} + fn to_category_i32(category: &str) -> i32 { match category { "misc" => 0x1, @@ -196,7 +206,29 @@ pub fn parseGalleryInfoList(env: JNIEnv, _class: JClass, input: JString) -> jobj let info: Vec = iter .filter_map(|x| parse_gallery_info(x.get(parser)?, parser)) .collect(); - Some(info) + let prev = dom.get_element_by_id("uprev").and_then(|e| { + let str = get_node_handle_attr(&e, parser, "href")?; + Some( + regex!("prev=(\\d+(-\\d+)?)") + .captures(str)? + .index(1) + .to_string(), + ) + }); + let next = dom.get_element_by_id("unext").and_then(|e| { + let str = get_node_handle_attr(&e, parser, "href")?; + Some( + regex!("next=(\\d+(-\\d+)?)") + .captures(str)? + .index(1) + .to_string(), + ) + }); + Some(GalleryListResult { + prev, + next, + galleryInfoList: info, + }) }) .unwrap() .into_java(&env)