Skip to content

Commit

Permalink
regex fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Jan 18, 2024
1 parent 39fc508 commit 5bd8f1a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
21 changes: 10 additions & 11 deletions app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/ani/dantotsu/parsers/AniyomiAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 =
Expand Down

0 comments on commit 5bd8f1a

Please sign in to comment.