Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from KotatsuApp:master #51

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ internal class ExHentaiParser(
joiner.append(lc.toLanguagePath())
joiner.append("\"$")
}
return joiner.complete().takeUnless { it.isEmpty() }
return joiner.complete().nullIfEmpty()
}

private fun Collection<ContentType>.toFCats(): Int = fold(0) { acc, ct ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import org.koitharu.kotatsu.parsers.MangaParser
import org.koitharu.kotatsu.parsers.MangaSourceParser
import org.koitharu.kotatsu.parsers.config.ConfigKey
import org.koitharu.kotatsu.parsers.model.*
import org.koitharu.kotatsu.parsers.util.domain
import org.koitharu.kotatsu.parsers.util.generateUid
import org.koitharu.kotatsu.parsers.util.getDomain
import org.koitharu.kotatsu.parsers.util.*
import org.koitharu.kotatsu.parsers.util.json.asTypedList
import org.koitharu.kotatsu.parsers.util.json.mapJSON
import org.koitharu.kotatsu.parsers.util.json.mapJSONIndexed
import org.koitharu.kotatsu.parsers.util.json.mapJSONNotNull
import org.koitharu.kotatsu.parsers.util.toAbsoluteUrl
import java.util.*

@Broken
Expand Down Expand Up @@ -92,7 +89,7 @@ internal class AnibelParser(context: MangaLoaderContext) : MangaParser(context,
title = title.getString("be"),
coverUrl = jo.getString("poster").removePrefix("/cdn")
.toAbsoluteUrl(getDomain("cdn")) + "?width=200&height=280",
altTitle = title.optJSONArray("alt")?.optString(0)?.takeUnless(String::isEmpty),
altTitle = title.optJSONArray("alt")?.optString(0)?.nullIfEmpty(),
author = null,
isNsfw = false,
rating = jo.getDouble("rating").toFloat() / 10f,
Expand Down Expand Up @@ -144,7 +141,7 @@ internal class AnibelParser(context: MangaLoaderContext) : MangaParser(context,
).getJSONObject("chapters").getJSONArray("docs")
return manga.copy(
title = title.getString("be"),
altTitle = title.optJSONArray("alt")?.optString(0)?.takeUnless(String::isEmpty),
altTitle = title.optJSONArray("alt")?.optString(0)?.nullIfEmpty(),
coverUrl = "$poster?width=200&height=280",
largeCoverUrl = poster,
description = details.getJSONObject("description").getString("be"),
Expand Down Expand Up @@ -239,7 +236,7 @@ internal class AnibelParser(context: MangaLoaderContext) : MangaParser(context,
title = title.getString("be"),
coverUrl = jo.getString("poster").removePrefix("/cdn")
.toAbsoluteUrl(getDomain("cdn")) + "?width=200&height=280",
altTitle = title.getString("en").takeUnless(String::isEmpty),
altTitle = title.getString("en").nullIfEmpty(),
author = null,
isNsfw = false,
rating = RATING_UNKNOWN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ internal class AsuraScansParser(context: MangaLoaderContext) :
val data = doc.selectOrThrow("script").mapNotNull { x ->
x.data().substringBetween("self.__next_f.push(", ")", "")
.trim()
.takeUnless { it.isEmpty() }
.nullIfEmpty()
}.flatMap { it.jsonStrings() }
.joinToString("")
.split('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal class ComicExtra(context: MangaLoaderContext) : PagedMangaParser(contex
append(page.toString())
}
}

else -> {
when (order) {
SortOrder.POPULARITY -> append("popular-comics")
Expand Down Expand Up @@ -151,9 +152,9 @@ internal class ComicExtra(context: MangaLoaderContext) : PagedMangaParser(contex
override suspend fun getPages(chapter: MangaChapter): List<MangaPage> {
val fullUrl = chapter.url.toAbsoluteUrl(domain) + "/full"
val doc = webClient.httpGet(fullUrl).parseHtml()

return doc.select("div.chapter-container img").mapNotNull { img ->
val url = img.attr("src")?.takeUnless { it.isBlank() }?.toAbsoluteUrl(domain)
val url = img.attrAsAbsoluteUrlOrNull("src")
url?.let {
MangaPage(
id = generateUid(url),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import org.koitharu.kotatsu.parsers.model.MangaListFilterCapabilities
import org.koitharu.kotatsu.parsers.model.MangaParserSource
import org.koitharu.kotatsu.parsers.model.MangaTag
import org.koitharu.kotatsu.parsers.site.mangareader.MangaReaderParser
import org.koitharu.kotatsu.parsers.util.attrAsAbsoluteUrl
import org.koitharu.kotatsu.parsers.util.domain
import org.koitharu.kotatsu.parsers.util.parseHtml
import org.koitharu.kotatsu.parsers.util.toTitleCase
import org.koitharu.kotatsu.parsers.util.*

@Broken
@MangaSourceParser("FREAKCOMIC", "FreakComic", "en")
Expand Down Expand Up @@ -41,7 +38,7 @@ internal class FreakComic(context: MangaLoaderContext) :
key = el.attrAsAbsoluteUrl("href")
.toHttpUrlOrNull()
?.queryParameter("genre")
?.takeUnless { it.isEmpty() }
?.nullIfEmpty()
?: continue,
source = source,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ internal class TruyenGG(context: MangaLoaderContext) : PagedMangaParser(context,

return manga.copy(
altTitle = doc.selectFirst("h2.other-name")?.text(),
author = doc.select("p:contains(Tác Giả) + p").joinToString { it.text() }.takeUnless { it.isEmpty() },
author = doc.select("p:contains(Tác Giả) + p").joinToString { it.text() }.nullIfEmpty(),
tags = doc.select("a.clblue").mapToSet {
MangaTag(
key = it.attr("href").substringAfterLast('-').substringBeforeLast('.'),
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/koitharu/kotatsu/parsers/util/Jsoup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ public fun Element.selectLastOrThrow(cssQuery: String): Element = parseNotNull(s
"Cannot find \"$cssQuery\""
}

public fun Element.textOrNull(): String? = text().takeUnless { it.isEmpty() }
public fun Element.textOrNull(): String? = text().nullIfEmpty()

public fun Element.ownTextOrNull(): String? = ownText().takeUnless { it.isEmpty() }
public fun Element.ownTextOrNull(): String? = ownText().nullIfEmpty()

public fun Element.selectFirstParent(query: String): Element? {
val selector = QueryParser.parse(query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public suspend fun Call.await(): Response = suspendCancellableCoroutine { contin
}

public val Response.mimeType: String?
get() = header("content-type")?.takeUnless { it.isEmpty() }
get() = header("content-type")?.nullIfEmpty()

public val Response.contentDisposition: String?
get() = header("Content-Disposition")
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/org/koitharu/kotatsu/parsers/util/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public fun String.nl2br(): String = replace("\n", "<br>")

public fun String.splitByWhitespace(): List<String> = trim().split(REGEX_WHITESPACE)

public fun <T : CharSequence> T.nullIfEmpty(): T? = takeUnless { it.isEmpty() }

public fun ByteArray.byte2HexFormatted(): String {
val str = StringBuilder(size * 2)
for (i in indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.collection.ArraySet
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.koitharu.kotatsu.parsers.util.nullIfEmpty
import java.util.*
import kotlin.contracts.contract

Expand Down Expand Up @@ -67,9 +68,7 @@ public fun <T> JSONArray.mapJSONIndexed(block: (Int, JSONObject) -> T): List<T>

public fun JSONObject.getStringOrNull(name: String): String? = opt(name)?.takeUnless {
it === JSONObject.NULL
}?.toString()?.takeUnless {
it.isEmpty()
}
}?.toString()?.nullIfEmpty()

public fun JSONObject.getBooleanOrDefault(name: String, defaultValue: Boolean): Boolean {
return when (val rawValue = opt(name)) {
Expand Down
Loading