[*chan] Fix title parsing

Koitharu 3 years ago
parent 400a90464e
commit a2f4bb35cd
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -56,12 +56,13 @@ internal abstract class ChanParser(
val a = row.selectFirst("div.manga_row1")?.selectFirst("h2")?.selectFirst("a") val a = row.selectFirst("div.manga_row1")?.selectFirst("h2")?.selectFirst("a")
?: return@mapNotNull null ?: return@mapNotNull null
val href = a.attrAsRelativeUrl("href") val href = a.attrAsRelativeUrl("href")
val title = a.text().parseTitle()
Manga( Manga(
id = generateUid(href), id = generateUid(href),
url = href, url = href,
publicUrl = href.toAbsoluteUrl(a.host ?: domain), publicUrl = href.toAbsoluteUrl(a.host ?: domain),
altTitle = a.attr("title"), altTitle = title.second,
title = a.text().substringAfterLast('(').substringBeforeLast(')'), title = title.first,
author = row.getElementsByAttributeValueStarting( author = row.getElementsByAttributeValueStarting(
"href", "href",
"/mangaka", "/mangaka",
@ -169,12 +170,13 @@ internal abstract class ChanParser(
val info = div.selectFirst(".related_info") ?: return@mapNotNull null val info = div.selectFirst(".related_info") ?: return@mapNotNull null
val a = info.selectFirst("a") ?: return@mapNotNull null val a = info.selectFirst("a") ?: return@mapNotNull null
val href = a.attrAsRelativeUrl("href") val href = a.attrAsRelativeUrl("href")
val title = a.text().parseTitle()
Manga( Manga(
id = generateUid(href), id = generateUid(href),
url = href, url = href,
publicUrl = href.toAbsoluteUrl(a.host ?: domain), publicUrl = href.toAbsoluteUrl(a.host ?: domain),
altTitle = a.attr("title"), altTitle = title.second,
title = a.text().substringAfterLast('(').substringBeforeLast(')'), title = title.first,
author = info.getElementsByAttributeValueStarting( author = info.getElementsByAttributeValueStarting(
"href", "href",
"/mangaka", "/mangaka",
@ -206,4 +208,20 @@ internal abstract class ChanParser(
} }
private fun String.toTagName() = replace('_', ' ').toTitleCase() private fun String.toTagName() = replace('_', ' ').toTitleCase()
private fun String.parseTitle(): Pair<String, String?> {
var depth = 0
for (i in indices.reversed()) {
val c = this[i]
if (c == '(') {
depth--
if (depth == 0 && (i + 2) < lastIndex && i > 0) {
return substring(i + 1, lastIndex).trim() to substring(0, i).trim()
}
} else if (c == ')') {
depth++
}
}
return this to null
}
} }

Loading…
Cancel
Save