Drop PreviewSet

This commit is contained in:
FooIbar authored and tarsin committed 2023-03-07 22:11:30 +08:00
1 parent f46d72a9eb
commit b815c924ff
11 files changed
+59 -333

No files matched your search

@@ -58,7 +58,7 @@ object EhClient {
params[0] as String?
)
METHOD_GET_PREVIEW_SET -> EhEngine.getPreviewSet(
METHOD_GET_PREVIEW_LIST -> EhEngine.getPreviewList(
params[0] as String?
)
@@ -165,7 +165,7 @@ object EhClient {
const val METHOD_SIGN_IN = 0
const val METHOD_GET_GALLERY_LIST = 1
const val METHOD_GET_GALLERY_DETAIL = 3
const val METHOD_GET_PREVIEW_SET = 4
const val METHOD_GET_PREVIEW_LIST = 4
const val METHOD_GET_RATE_GALLERY = 5
const val METHOD_GET_COMMENT_GALLERY = 6
const val METHOD_GET_GALLERY_TOKEN = 7
@@ -26,7 +26,7 @@ import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.data.GalleryCommentList
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.PreviewSet
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.client.exception.ParseException
import com.hippo.ehviewer.client.parser.ArchiveParser
@@ -336,7 +336,7 @@ object EhEngine {
}
@Throws(Throwable::class)
suspend fun getPreviewSet(url: String?): Pair<PreviewSet, Int> {
suspend fun getPreviewList(url: String?): Pair<List<GalleryPreview>, Int> {
val referer = EhUrl.referer
Log.d(TAG, url!!)
val request = EhRequestBuilder(url, referer).build()
@@ -351,7 +351,7 @@ object EhEngine {
headers = response.headers
body = response.body.string()
return Pair.create(
GalleryDetailParser.parsePreviewSet(body!!),
GalleryDetailParser.parsePreviewList(body!!),
GalleryDetailParser.parsePreviewPages(body!!)
)
}
@@ -21,60 +21,26 @@ import kotlinx.parcelize.Parcelize
@Parcelize
class GalleryDetail(
@JvmField
val galleryInfo: GalleryInfo = BaseGalleryInfo(),
@JvmField
var apiUid: Long = -1L,
@JvmField
var apiKey: String? = null,
@JvmField
var torrentCount: Int = 0,
@JvmField
var torrentUrl: String? = null,
@JvmField
var archiveUrl: String? = null,
@JvmField
var parent: String? = null,
@JvmField
var newerVersions: ArrayList<GalleryInfo> = ArrayList(),
@JvmField
var visible: String? = null,
@JvmField
var language: String? = null,
@JvmField
var size: String? = null,
@JvmField
var favoriteCount: Int = 0,
@JvmField
var isFavorited: Boolean = false,
@JvmField
var ratingCount: Int = 0,
@JvmField
var tags: Array<GalleryTagGroup>? = null,
@JvmField
var comments: GalleryCommentList? = null,
@JvmField
var previewPages: Int = 0,
@IgnoredOnParcel
@JvmField
var previewSet: PreviewSet? = null,
var previewList: List<GalleryPreview> = emptyList(),
) : AbstractGalleryInfo by galleryInfo, GalleryInfo {
override fun generateSLang() {
val index = LANGUAGES.indexOf(language)
@@ -21,23 +21,16 @@ import kotlinx.parcelize.Parcelize
@Parcelize
class GalleryPreview(
@JvmField
var imageUrl: String? = null,
@JvmField
var pageUrl: String? = null,
@JvmField
var position: Int = 0,
@JvmField
var offsetX: Int = Int.MIN_VALUE,
@JvmField
var offsetY: Int = Int.MIN_VALUE,
@JvmField
var clipWidth: Int = Int.MIN_VALUE,
@JvmField
var clipHeight: Int = Int.MIN_VALUE,
private val imageUrl: String,
val pageUrl: String,
val position: Int,
private val offsetX: Int = Int.MIN_VALUE,
private val offsetY: Int = Int.MIN_VALUE,
private val clipWidth: Int = Int.MIN_VALUE,
private val clipHeight: Int = Int.MIN_VALUE,
) : Parcelable {
fun load(view: LoadImageView) {
view.setClip(offsetX, offsetY, clipWidth, clipHeight)
view.load(imageUrl!!)
view.load(imageUrl)
}
}
@@ -1,59 +0,0 @@
/*
* 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.data
import com.hippo.widget.LoadImageView
import com.hippo.yorozuya.collect.IntList
class LargePreviewSet(
private val mPositionList: IntList = IntList(),
private val mImageUrlList: ArrayList<String> = ArrayList(),
private val mPageUrlList: ArrayList<String> = ArrayList()
) : PreviewSet() {
fun addItem(index: Int, imageUrl: String, pageUrl: String) {
mPositionList.add(index)
mImageUrlList.add(imageUrl)
mPageUrlList.add(pageUrl)
}
override fun size(): Int {
return mImageUrlList.size
}
override fun getPosition(index: Int): Int {
return mPositionList[index]
}
override fun getPageUrlAt(index: Int): String {
return mPageUrlList[index]
}
override fun getGalleryPreview(gid: Long, index: Int): GalleryPreview {
val galleryPreview = GalleryPreview()
galleryPreview.position = mPositionList[index]
galleryPreview.imageUrl = mImageUrlList[index]
galleryPreview.pageUrl = mPageUrlList[index]
return galleryPreview
}
override fun load(view: LoadImageView, gid: Long, index: Int) {
view.resetClip()
view.load(
mImageUrlList[index]
)
}
}
@@ -1,75 +0,0 @@
/*
* 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.data
import com.hippo.widget.LoadImageView
import com.hippo.yorozuya.collect.IntList
class NormalPreviewSet(
private var mPositionList: IntList = IntList(),
private var mImageUrlList: ArrayList<String> = ArrayList(),
private var mOffsetXList: IntList = IntList(),
private var mOffsetYList: IntList = IntList(),
private var mClipWidthList: IntList = IntList(),
private var mClipHeightList: IntList = IntList(),
private var mPageUrlList: ArrayList<String> = ArrayList()
) : PreviewSet() {
fun addItem(
position: Int, imageUrl: String, xOffset: Int, yOffset: Int, width: Int,
height: Int, pageUrl: String
) {
mPositionList.add(position)
mImageUrlList.add(imageUrl)
mOffsetXList.add(xOffset)
mOffsetYList.add(yOffset)
mClipWidthList.add(width)
mClipHeightList.add(height)
mPageUrlList.add(pageUrl)
}
override fun size(): Int {
return mPositionList.size
}
override fun getPosition(index: Int): Int {
return mPositionList[index]
}
override fun getPageUrlAt(index: Int): String {
return mPageUrlList[index]
}
override fun getGalleryPreview(gid: Long, index: Int): GalleryPreview {
val galleryPreview = GalleryPreview()
galleryPreview.position = mPositionList[index]
galleryPreview.imageUrl = mImageUrlList[index]
galleryPreview.pageUrl = mPageUrlList[index]
galleryPreview.offsetX = mOffsetXList[index]
galleryPreview.offsetY = mOffsetYList[index]
galleryPreview.clipWidth = mClipWidthList[index]
galleryPreview.clipHeight = mClipHeightList[index]
return galleryPreview
}
override fun load(view: LoadImageView, gid: Long, index: Int) {
view.setClip(
mOffsetXList[index], mOffsetYList[index],
mClipWidthList[index], mClipHeightList[index]
)
view.load(mImageUrlList[index])
}
}
@@ -1,26 +0,0 @@
/*
* 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.data
import com.hippo.widget.LoadImageView
abstract class PreviewSet {
abstract fun size(): Int
abstract fun getPosition(index: Int): Int
abstract fun getPageUrlAt(index: Int): String
abstract fun getGalleryPreview(gid: Long, index: Int): GalleryPreview
abstract fun load(view: LoadImageView, gid: Long, index: Int)
}
@@ -24,10 +24,8 @@ import com.hippo.ehviewer.client.data.BaseGalleryInfo
import com.hippo.ehviewer.client.data.GalleryComment
import com.hippo.ehviewer.client.data.GalleryCommentList
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.client.data.GalleryTagGroup
import com.hippo.ehviewer.client.data.LargePreviewSet
import com.hippo.ehviewer.client.data.NormalPreviewSet
import com.hippo.ehviewer.client.data.PreviewSet
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.client.exception.OffensiveException
import com.hippo.ehviewer.client.exception.ParseException
@@ -97,8 +95,8 @@ object GalleryDetailParser {
parseDetail(galleryDetail, document, body)
galleryDetail.tags = parseTagGroups(document)
galleryDetail.comments = parseComments(document)
galleryDetail.previewPages = parsePreviewPages(document, body)
galleryDetail.previewSet = parsePreviewSet(document, body)
galleryDetail.previewPages = parsePreviewPages(body)
galleryDetail.previewList = parsePreviewList(body)
// Generate simpleLanguage for local favorites
galleryDetail.generateSLang()
@@ -447,26 +445,9 @@ object GalleryDetailParser {
}
}
/**
* Parse preview pages with html parser
*/
@Throws(ParseException::class)
fun parsePreviewPages(document: Document, body: String?): Int {
return try {
val ptt = document.getElementsByClass("ptt").first()!!
val elements = ptt.child(0).child(0).children()
elements[elements.size - 2].text().toInt()
} catch (e: Throwable) {
ExceptionUtils.throwIfFatal(e)
e.printStackTrace()
throw ParseException("Can't parse preview pages", body!!)
}
}
/**
* Parse preview pages with regular expressions
*/
@JvmStatic
@Throws(ParseException::class)
fun parsePreviewPages(body: String): Int {
return PATTERN_PREVIEW_PAGES.find(body)?.groupValues?.get(1)?.toIntOrNull()
@@ -476,7 +457,6 @@ object GalleryDetailParser {
/**
* Parse pages with regular expressions
*/
@JvmStatic
@Throws(ParseException::class)
fun parsePages(body: String): Int {
return PATTERN_PAGES.find(body)?.groupValues?.get(1)?.toIntOrNull()
@@ -484,21 +464,11 @@ object GalleryDetailParser {
}
@Throws(ParseException::class)
fun parsePreviewSet(d: Document, body: String): PreviewSet {
fun parsePreviewList(body: String): List<GalleryPreview> {
return try {
parseLargePreviewSet(d, body)
parseLargePreview(body)
} catch (e: ParseException) {
parseNormalPreviewSet(body)
}
}
@JvmStatic
@Throws(ParseException::class)
fun parsePreviewSet(body: String): PreviewSet {
return try {
parseLargePreviewSet(body)
} catch (e: ParseException) {
parseNormalPreviewSet(body)
parseNormalPreview(body)
}
}
@@ -506,67 +476,37 @@ object GalleryDetailParser {
* Parse large previews with regular expressions
*/
@Throws(ParseException::class)
private fun parseLargePreviewSet(d: Document, body: String): LargePreviewSet {
return try {
val largePreviewSet = LargePreviewSet()
val gdt = d.getElementById("gdt")!!
val gdtls = gdt.getElementsByClass("gdtl")
val n = gdtls.size
if (n == 0) {
throw ParseException("Can't parse large preview", body)
}
for (i in 0 until n) {
var element = gdtls[i].child(0)
val pageUrl = element.attr("href")
element = element.child(0)
val imageUrl = element.attr("src")
val index = element.attr("alt").toInt() - 1
largePreviewSet.addItem(index, imageUrl, pageUrl)
}
largePreviewSet
} catch (e: Throwable) {
ExceptionUtils.throwIfFatal(e)
throw ParseException("Can't parse large preview", body)
}
}
/**
* Parse large previews with regular expressions
*/
@Throws(ParseException::class)
private fun parseLargePreviewSet(body: String): LargePreviewSet {
val largePreviewSet = LargePreviewSet()
PATTERN_LARGE_PREVIEW.findAll(body).forEach {
val index = (it.groupValues[2].toIntOrNull() ?: return@forEach) - 1
private fun parseLargePreview(body: String): List<GalleryPreview> {
val list = PATTERN_LARGE_PREVIEW.findAll(body).mapNotNull {
val index = (it.groupValues[2].toIntOrNull() ?: return@mapNotNull null) - 1
val imageUrl = it.groupValues[3].trim()
val pageUrl = it.groupValues[1].trim()
largePreviewSet.addItem(index, imageUrl, pageUrl)
}
if (largePreviewSet.size() == 0) {
GalleryPreview(imageUrl, pageUrl, index)
}.toList()
if (list.isEmpty()) {
throw ParseException("Can't parse large preview", body)
}
return largePreviewSet
return list
}
/**
* Parse normal previews with regular expressions
*/
@Throws(ParseException::class)
private fun parseNormalPreviewSet(body: String): NormalPreviewSet {
val normalPreviewSet = NormalPreviewSet()
PATTERN_NORMAL_PREVIEW.findAll(body).forEach {
val position = (it.groupValues[6].toIntOrNull() ?: return@forEach) - 1
private fun parseNormalPreview(body: String): List<GalleryPreview> {
val list = PATTERN_NORMAL_PREVIEW.findAll(body).mapNotNull {
val position = (it.groupValues[6].toIntOrNull() ?: return@mapNotNull null) - 1
val imageUrl = it.groupValues[3].trim()
val xOffset = it.groupValues[4].toIntOrNull() ?: 0
val yOffset = 0
val width = it.groupValues[1].toIntOrNull() ?: return@forEach
val height = it.groupValues[2].toIntOrNull() ?: return@forEach
val width = it.groupValues[1].toIntOrNull() ?: return@mapNotNull null
val height = it.groupValues[2].toIntOrNull() ?: return@mapNotNull null
val pageUrl = it.groupValues[5].trim()
normalPreviewSet.addItem(position, imageUrl, xOffset, yOffset, width, height, pageUrl)
}
if (normalPreviewSet.size() == 0) {
GalleryPreview(imageUrl, pageUrl, position, xOffset, yOffset, width, height)
}.toList()
if (list.isEmpty()) {
throw ParseException("Can't parse normal preview", body)
}
return normalPreviewSet
return list
}
}
@@ -34,7 +34,7 @@ import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.exception.ParseException
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePages
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePreviewPages
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePreviewSet
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePreviewList
import com.hippo.ehviewer.client.parser.GalleryMultiPageViewerPTokenParser
import com.hippo.ehviewer.client.parser.GalleryPageUrlParser
import com.hippo.image.Image
@@ -339,22 +339,19 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
@Throws(ParseException::class)
private fun readPreviews(body: String, index: Int, spiderInfo: SpiderInfo) {
spiderInfo.previewPages = parsePreviewPages(body)
val previewSet = parsePreviewSet(body)
if (previewSet.size() > 0) {
val previewList = parsePreviewList(body)
if (previewList.isNotEmpty()) {
if (index == 0) {
spiderInfo.previewPerPage = previewSet.size()
spiderInfo.previewPerPage = previewList.size
} else {
spiderInfo.previewPerPage = previewSet.getPosition(0) / index
spiderInfo.previewPerPage = previewList[0].position / index
}
}
var i = 0
val n = previewSet.size()
while (i < n) {
val result = GalleryPageUrlParser.parse(previewSet.getPageUrlAt(i))
previewList.forEach {
val result = GalleryPageUrlParser.parse(it.pageUrl)
if (result != null) {
spiderInfo.pTokenMap[result.page] = result.pToken
}
i++
}
}
@@ -709,8 +709,8 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
_binding ?: return
binding.content.previews.run {
gridLayout.removeAllViews()
val previewSet = gd.previewSet
if (gd.previewPages <= 0 || previewSet == null || previewSet.size() == 0) {
val previewList = gd.previewList
if (gd.previewPages <= 0 || previewList.isEmpty()) {
previewText.setText(R.string.no_previews)
return
} else if (gd.previewPages == 1) {
@@ -722,17 +722,17 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
gridLayout.setColumnSize(columnWidth)
gridLayout.setStrategy(SimpleGridAutoSpanLayout.STRATEGY_SUITABLE_SIZE)
viewLifecycleOwner.lifecycleScope.launchIO {
for (i in 0 until previewSet.size()) {
previewList.forEach {
val view = inflater.inflate(R.layout.item_gallery_preview, gridLayout, false)
val image = view.findViewById<LoadImageView>(R.id.image)
withUIContext {
gridLayout.addView(view)
image.setTag(R.id.index, i)
image.setTag(R.id.index, it.position)
image.setOnClickListener(this@GalleryDetailScene)
val text = view.findViewById<TextView>(R.id.text)
text.text = (previewSet.getPosition(i) + 1).toString()
text.text = (it.position + 1).toString()
}
previewSet.load(image, gd.gid, i)
it.load(image)
}
}
}
@@ -40,7 +40,6 @@ import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.client.data.PreviewSet
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.databinding.SceneGalleryPreviewsBinding
import com.hippo.util.getParcelableCompat
@@ -185,22 +184,13 @@ class GalleryPreviewsScene : BaseToolbarScene() {
return true
}
private fun onGetPreviewSetSuccess(result: Pair<PreviewSet, Int>, taskId: Int) {
private fun onGetPreviewListSuccess(result: Pair<List<GalleryPreview>, Int>, taskId: Int) {
if (null != mHelper && mHelper!!.isCurrentTask(taskId) && null != mGalleryInfo) {
val previewSet = result.first
val size = previewSet.size()
val list = ArrayList<GalleryPreview>(size)
for (i in 0 until size) {
list.add(previewSet.getGalleryPreview(mGalleryInfo!!.gid, i))
}
mHelper!!.onGetPageData(
taskId, result.second, 0, null, null,
list as List<GalleryPreview?>?
)
mHelper!!.onGetPageData(taskId, result.second, 0, null, null, result.first)
}
}
private fun onGetPreviewSetFailure(e: Exception, taskId: Int) {
private fun onGetPreviewListFailure(e: Exception, taskId: Int) {
if (mHelper != null && mHelper!!.isCurrentTask(taskId)) {
mHelper!!.onGetException(taskId, e)
}
@@ -216,18 +206,18 @@ class GalleryPreviewsScene : BaseToolbarScene() {
}
}
private inner class GetPreviewSetListener(
private inner class GetPreviewListListener(
context: Context,
private val mTaskId: Int
) : EhCallback<GalleryPreviewsScene, Pair<PreviewSet, Int>>(context) {
override fun onSuccess(result: Pair<PreviewSet, Int>) {
) : EhCallback<GalleryPreviewsScene, Pair<List<GalleryPreview>, Int>>(context) {
override fun onSuccess(result: Pair<List<GalleryPreview>, Int>) {
val scene = this@GalleryPreviewsScene
scene.onGetPreviewSetSuccess(result, mTaskId)
scene.onGetPreviewListSuccess(result, mTaskId)
}
override fun onFailure(e: Exception) {
val scene = this@GalleryPreviewsScene
scene.onGetPreviewSetFailure(e, mTaskId)
scene.onGetPreviewListFailure(e, mTaskId)
}
override fun onCancel() {}
@@ -279,9 +269,9 @@ class GalleryPreviewsScene : BaseToolbarScene() {
val url =
EhUrl.getGalleryDetailUrl(mGalleryInfo!!.gid, mGalleryInfo!!.token, page, false)
val request = EhRequest()
request.setMethod(EhClient.METHOD_GET_PREVIEW_SET)
request.setMethod(EhClient.METHOD_GET_PREVIEW_LIST)
request.setCallback(
GetPreviewSetListener(
GetPreviewListListener(
context,
taskId
)