[Shinigami] Fix search with tags + Refactor

master
dragonx943 1 year ago
parent 6418422157
commit fecb1db3be

@ -44,7 +44,11 @@ internal class Shinigami(context: MangaLoaderContext) :
override suspend fun getFilterOptions(): MangaListFilterOptions {
return MangaListFilterOptions(
availableTags = fetchTags(),
availableStates = EnumSet.of(MangaState.ONGOING, MangaState.FINISHED, MangaState.PAUSED),
availableStates = EnumSet.of(
MangaState.ONGOING,
MangaState.FINISHED,
MangaState.PAUSED
),
availableContentTypes = EnumSet.of(
ContentType.MANGA,
ContentType.MANHWA,
@ -58,6 +62,7 @@ internal class Shinigami(context: MangaLoaderContext) :
isMultipleTagsSupported = true,
isTagsExclusionSupported = true,
isSearchSupported = true,
isSearchWithFiltersSupported = true,
)
override suspend fun getListPage(page: Int, order: SortOrder, filter: MangaListFilter): List<Manga> {
@ -114,20 +119,21 @@ internal class Shinigami(context: MangaLoaderContext) :
}
if (filter.tags.isNotEmpty()) {
append("&genres_include=")
filter.tags.joinTo(this, ",") { it.key }
append("&genres_include_mode=and")
append("&genre_include=")
filter.tags.joinTo(this, separator = ",") { it.key }
append("&genre_include_mode=and")
}
if (filter.tagsExclude.isNotEmpty()) {
append("&genres_exclude=")
filter.tagsExclude.joinTo(this, ",") { it.key }
append("&genres_exclude_mode=and")
append("&genre_exclude=")
filter.tagsExclude.joinTo(this, separator = ",") { it.key }
append("&genre_exclude_mode=and")
}
if (!filter.query.isNullOrEmpty()) {
append("&q=")
val encodedQuery = filter.query.splitByWhitespace().joinToString(separator = "+") { part ->
val encodedQuery = filter.query.splitByWhitespace()
.joinToString(separator = "+") { part ->
part.urlEncoded()
}
append(encodedQuery)
@ -136,6 +142,7 @@ internal class Shinigami(context: MangaLoaderContext) :
val json = webClient.httpGet(url).parseJson()
val data = json.getJSONArray("data")
return data.mapJSON { jo ->
val id = jo.getString("manga_id")
Manga(
@ -145,11 +152,16 @@ internal class Shinigami(context: MangaLoaderContext) :
title = jo.getString("title"),
altTitles = setOf(jo.optString("alternative_title") ?: ""),
coverUrl = jo.getString("cover_image_url"),
largeCoverUrl = jo.optString("cover_portrait_url").takeIf { it.isNotEmpty() },
authors = jo.optJSONObject("taxonomy")?.optJSONArray("Author")?.mapJSONToSet { x ->
largeCoverUrl = jo.optString("cover_portrait_url")
.takeIf { it.isNotEmpty() },
authors = jo.optJSONObject("taxonomy")
?.optJSONArray("Author")
?.mapJSONToSet { x ->
x.getString("name")
}.orEmpty(),
tags = jo.optJSONObject("taxonomy")?.optJSONArray("Genre")?.mapJSONToSet { x ->
tags = jo.optJSONObject("taxonomy")
?.optJSONArray("Genre")
?.mapJSONToSet { x ->
MangaTag(
key = x.getString("slug"),
title = x.getString("name"),
@ -174,15 +186,20 @@ internal class Shinigami(context: MangaLoaderContext) :
val json = webClient.httpGet("https://$apiSuffix" + manga.url).parseJson()
val jo = json.getJSONObject("data")
val id = jo.getString("manga_id")
return manga.copy(
tags = jo.optJSONObject("taxonomy")?.optJSONArray("Genre")?.mapJSONToSet { x ->
tags = jo.optJSONObject("taxonomy")
?.optJSONArray("Genre")
?.mapJSONToSet { x ->
MangaTag(
key = x.getString("slug"),
title = x.getString("name"),
source = source,
)
}.orEmpty(),
authors = jo.optJSONObject("taxonomy")?.optJSONArray("Author")?.mapJSONToSet { x ->
authors = jo.optJSONObject("taxonomy")
?.optJSONArray("Author")
?.mapJSONToSet { x ->
x.getString("name")
}.orEmpty(),
state = when (jo.getInt("status")) {
@ -192,7 +209,8 @@ internal class Shinigami(context: MangaLoaderContext) :
else -> null
},
description = jo.optString("description"),
largeCoverUrl = jo.optString("cover_portrait_url").takeIf { it.isNotEmpty() },
largeCoverUrl = jo.optString("cover_portrait_url")
.takeIf { it.isNotEmpty() },
chapters = getChapters(id)
)
}
@ -205,7 +223,8 @@ internal class Shinigami(context: MangaLoaderContext) :
return data.mapJSON { jo ->
val chapterId = jo.getString("chapter_id")
val number = jo.getInt("chapter_number")
val title = jo.optString("chapter_title").takeIf { it.isNotEmpty() }
val title = jo.optString("chapter_title")
.takeIf { it.isNotEmpty() }
?: "Chapter $number"
MangaChapter(
@ -232,6 +251,7 @@ internal class Shinigami(context: MangaLoaderContext) :
return List(datas.length()) { i ->
val imgExt = datas.getString(i)
val imageUrl = "https://$cdnSuffix" + basePath + imgExt
MangaPage(
id = generateUid(imageUrl),
url = imageUrl,
@ -243,6 +263,7 @@ internal class Shinigami(context: MangaLoaderContext) :
private suspend fun fetchTags(): Set<MangaTag> {
val json = webClient.httpGet("https://$apiSuffix/genre/list").parseJson()
return json.getJSONArray("data").mapJSONToSet { x ->
MangaTag(
key = x.getString("slug"),
@ -254,9 +275,11 @@ internal class Shinigami(context: MangaLoaderContext) :
private fun String.parseDate(): Long {
return try {
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US).apply {
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
.apply {
timeZone = TimeZone.getTimeZone("UTC")
}.parse(this)?.time ?: 0L
}
.parse(this)?.time ?: 0L
} catch (e: Exception) {
0L
}

Loading…
Cancel
Save