MangaPark - refactor page parsing (#3902)

* Manga Park - refactor page parsing

* Update requests
This commit is contained in:
Mike 2020-07-26 03:44:35 -04:00 committed by GitHub
parent 40e8892a7b
commit 394b1af7cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 30 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'MangaPark' extName = 'MangaPark'
pkgNameSuffix = 'en.mangapark' pkgNameSuffix = 'en.mangapark'
extClass = '.MangaPark' extClass = '.MangaPark'
extVersionCode = 16 extVersionCode = 17
libVersion = '1.2' libVersion = '1.2'
} }

View File

@ -37,28 +37,26 @@ class MangaPark : ConfigurableSource, ParsedHttpSource() {
override val name = "MangaPark" override val name = "MangaPark"
override val baseUrl = "https://mangapark.net" override val baseUrl = "https://mangapark.net"
private val directorySelector = ".ls1 .item" private val nextPageSelector = ".paging:not(.order) > li:last-child > a"
private val directoryUrl = "/genre"
private val directoryNextPageSelector = ".paging.full > li:last-child > a"
private val dateFormat = SimpleDateFormat("MMM d, yyyy, HH:mm a", Locale.ENGLISH) private val dateFormat = SimpleDateFormat("MMM d, yyyy, HH:mm a", Locale.ENGLISH)
private val dateFormatTimeOnly = SimpleDateFormat("HH:mm a", Locale.ENGLISH) private val dateFormatTimeOnly = SimpleDateFormat("HH:mm a", Locale.ENGLISH)
override fun popularMangaRequest(page: Int) = GET("$baseUrl$directoryUrl/$page?views_a") override fun popularMangaRequest(page: Int) = GET("$baseUrl/search?orderby=views_a&page=$page")
override fun popularMangaSelector() = directorySelector override fun popularMangaSelector() = searchMangaSelector()
override fun popularMangaFromElement(element: Element) = mangaFromElement(element) override fun popularMangaFromElement(element: Element) = mangaFromElement(element)
override fun popularMangaNextPageSelector() = directoryNextPageSelector override fun popularMangaNextPageSelector() = nextPageSelector
override fun latestUpdatesRequest(page: Int) = GET("$baseUrl/latest") override fun latestUpdatesRequest(page: Int) = GET("$baseUrl/latest" + if (page > 1) "/$page" else "")
override fun latestUpdatesSelector() = directorySelector override fun latestUpdatesSelector() = ".ls1 .item"
override fun latestUpdatesFromElement(element: Element) = mangaFromElement(element) override fun latestUpdatesFromElement(element: Element) = mangaFromElement(element)
override fun latestUpdatesNextPageSelector() = directoryNextPageSelector override fun latestUpdatesNextPageSelector() = nextPageSelector
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
val uri = Uri.parse("$baseUrl/search").buildUpon() val uri = Uri.parse("$baseUrl/search").buildUpon()
@ -79,7 +77,7 @@ class MangaPark : ConfigurableSource, ParsedHttpSource() {
override fun searchMangaFromElement(element: Element) = mangaFromElement(element) override fun searchMangaFromElement(element: Element) = mangaFromElement(element)
override fun searchMangaNextPageSelector() = ".paging:not(.order) > li:last-child > a" override fun searchMangaNextPageSelector() = nextPageSelector
private fun mangaFromElement(element: Element) = SManga.create().apply { private fun mangaFromElement(element: Element) = SManga.create().apply {
val coverElement = element.getElementsByClass("cover").first() val coverElement = element.getElementsByClass("cover").first()
@ -174,15 +172,19 @@ class MangaPark : ConfigurableSource, ParsedHttpSource() {
override fun chapterListSelector() = ".volume .chapter li" override fun chapterListSelector() = ".volume .chapter li"
private val chapterNumberRegex = Regex("""\b\d+\.?\d?\b""")
private fun chapterFromElement(element: Element, source: String, lastNum: Float): SChapter { private fun chapterFromElement(element: Element, source: String, lastNum: Float): SChapter {
fun Float.incremented() = this + .00001F fun Float.incremented() = this + .00001F
fun Float?.orIncrementLastNum() = if (this == null || this < lastNum) lastNum.incremented() else this fun Float?.orIncrementLastNum() = if (this == null || this < lastNum) lastNum.incremented() else this
return SChapter.create().apply { return SChapter.create().apply {
url = element.select(".tit > a").first().attr("href").replaceAfterLast("/", "") element.select(".tit > a").first().let {
name = element.select(".tit > a").first().text() url = it.attr("href").removeSuffix("1")
name = it.text()
}
// Get the chapter number or create a unique one if it's not available // Get the chapter number or create a unique one if it's not available
chapter_number = Regex("""\b\d+\.?\d?\b""").findAll(name) chapter_number = chapterNumberRegex.findAll(name)
.toList() .toList()
.map { it.value.toFloatOrNull() } .map { it.value.toFloatOrNull() }
.let { nums -> .let { nums ->
@ -215,7 +217,7 @@ class MangaPark : ConfigurableSource, ParsedHttpSource() {
relativeDate?.let { relativeDate?.let {
// Since the date is not specified, it defaults to 1970! // Since the date is not specified, it defaults to 1970!
val time = dateFormatTimeOnly.parse(lcDate.substringAfter(' ')) val time = dateFormatTimeOnly.parse(lcDate.substringAfter(' ')) ?: return 0
val cal = Calendar.getInstance() val cal = Calendar.getInstance()
cal.time = time cal.time = time
@ -225,7 +227,7 @@ class MangaPark : ConfigurableSource, ParsedHttpSource() {
return it.timeInMillis return it.timeInMillis
} }
return dateFormat.parse(lcDate).time return dateFormat.parse(lcDate)?.time ?: 0
} }
/** /**
@ -262,22 +264,18 @@ class MangaPark : ConfigurableSource, ParsedHttpSource() {
return now.timeInMillis return now.timeInMillis
} }
override fun pageListParse(document: Document): List<Page> { private val objRegex = Regex("""var _load_pages = (\[.*])""")
val doc = document.toString()
val obj = doc.substringAfter("var _load_pages = ").substringBefore(";") override fun pageListParse(response: Response): List<Page> {
val pages = mutableListOf<Page>() val obj = objRegex.find(response.body()!!.string())?.groupValues?.get(1)
val imglist = JSONArray(obj) ?: throw Exception("_load_pages not found - ${response.request().url()}")
for (i in 0 until imglist.length()) { val jsonArray = JSONArray(obj)
val item = imglist.getJSONObject(i) return (0 until jsonArray.length()).map { i -> jsonArray.getJSONObject(i).getString("u") }
var page = item.getString("u") .mapIndexed { i, url -> Page(i, "", if (url.startsWith("//")) "https://$url" else url) }
if (page.startsWith("//")) {
page = "https:$page"
}
pages.add(Page(i, "", page))
}
return pages
} }
override fun pageListParse(document: Document): List<Page> = throw UnsupportedOperationException("Not used")
// Unused, we can get image urls directly from the chapter page // Unused, we can get image urls directly from the chapter page
override fun imageUrlParse(document: Document) = throw UnsupportedOperationException("Not used") override fun imageUrlParse(document: Document) = throw UnsupportedOperationException("Not used")