From 5bd8f1a3c7be0db44044832bef51cbc849df90b6 Mon Sep 17 00:00:00 2001 From: rebelonion <87634197+rebelonion@users.noreply.github.com> Date: Thu, 18 Jan 2024 01:55:47 -0600 Subject: [PATCH] regex fix --- .../dantotsu/media/anime/AnimeNameAdapter.kt | 21 +++++++++---------- .../ani/dantotsu/parsers/AniyomiAdapter.kt | 7 ++++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt b/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt index 1a46e6c3a1..b59864b749 100644 --- a/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt +++ b/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt @@ -30,7 +30,15 @@ class AnimeNameAdapter { if (episodeMatcher.group(2) != null) { episodeMatcher.group(2)?.toFloat() } else { - episodeMatcher.group(4)?.toFloat() + val failedEpisodeNumberPattern: Pattern = + Pattern.compile(failedEpisodeNumberRegex, Pattern.CASE_INSENSITIVE) + val failedEpisodeNumberMatcher: Matcher = + failedEpisodeNumberPattern.matcher(text) + if (failedEpisodeNumberMatcher.find()) { + failedEpisodeNumberMatcher.group(1)?.toFloat() + } else { + null + } } } else { null @@ -39,18 +47,9 @@ class AnimeNameAdapter { fun removeEpisodeNumber(text: String): String { val regexPattern = Regex(episodeRegex, RegexOption.IGNORE_CASE) - val removedNumber = text.replace(regexPattern, "").ifEmpty { + return text.replace(regexPattern, "").ifEmpty { text } - return if (removedNumber.equals(text, true)) { - val failedEpisodeNumberPattern: Regex = - Regex(failedEpisodeNumberRegex, RegexOption.IGNORE_CASE) - failedEpisodeNumberPattern.replace(removedNumber) { mr -> - mr.value.replaceFirst(mr.groupValues[1], "") - }.ifEmpty { removedNumber } - } else { - removedNumber - } } fun removeEpisodeNumberCompletely(text: String): String { diff --git a/app/src/main/java/ani/dantotsu/parsers/AniyomiAdapter.kt b/app/src/main/java/ani/dantotsu/parsers/AniyomiAdapter.kt index 2af5257cce..7883a530b7 100644 --- a/app/src/main/java/ani/dantotsu/parsers/AniyomiAdapter.kt +++ b/app/src/main/java/ani/dantotsu/parsers/AniyomiAdapter.kt @@ -93,8 +93,8 @@ class DynamicAnimeParser(extension: AnimeExtension.Installed) : AnimeParser() { val sortedEpisodes = if (res[0].episode_number == -1f) { // Find the number in the string and sort by that number val sortedByStringNumber = res.sortedBy { - val matchResult = "\\d+".toRegex().find(it.name) - val number = matchResult?.value?.toFloat() ?: Float.MAX_VALUE + val matchResult = AnimeNameAdapter.findEpisodeNumber(it.name) + val number = matchResult ?: Float.MAX_VALUE it.episode_number = number // Store the found number in episode_number number } @@ -113,7 +113,8 @@ class DynamicAnimeParser(extension: AnimeExtension.Installed) : AnimeParser() { // Group by season, sort within each season, and then renumber while keeping episode number 0 as is val seasonGroups = res.groupBy { AnimeNameAdapter.findSeasonNumber(it.name) ?: 0 } - seasonGroups.keys.sorted().flatMap { season -> + seasonGroups.keys.sortedBy { it.toInt() } + .flatMap { season -> seasonGroups[season]?.sortedBy { it.episode_number }?.map { episode -> if (episode.episode_number != 0f) { // Skip renumbering for episode number 0 val potentialNumber =