Skip to content

Commit

Permalink
支持 mikan 按索引搜索, close #215, close #103
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Apr 28, 2024
1 parent 3618bf7 commit 5087be5
Show file tree
Hide file tree
Showing 12 changed files with 21,033 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ internal class DefaultEpisodeMediaFetchSession(
MediaFetchRequest(
subjectId = subject.id.toString(),
episodeId = episode.id.toString(),
subjectNameCN = subject.nameCn,
subjectNames = setOfNotNull(
subject.nameCn,
subject.name,
),
episodeSort = EpisodeSort(episode.sort.toString()),
episodeEp = episode.ep?.let { EpisodeSort(it) },
episodeName = episode.nameCNOrName(),
episodeEp = episode.ep?.let { EpisodeSort(it) },
)
}

Expand Down
2 changes: 1 addition & 1 deletion data-sources/acg.rip/src/AcgRipMediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class AcgRipMediaSource(
}
val document: Document = Jsoup.parse(resp.bodyAsChannel().toInputStream(), "UTF-8", "https://acg.rip/.xml")
parseDocument(document)
.filter { query.matches(it) }
.filter { query.matches(it, allowEpMatch = false) }
.run {
Paged(size, isNotEmpty(), this)
}
Expand Down
1 change: 1 addition & 0 deletions data-sources/api/src/source/MediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class MediaFetchRequest(
*/
val subjectId: String? = null,
val episodeId: String? = null,
val subjectNameCN: String? = null,
/**
* Translated and original names of the subject.
*
Expand Down
41 changes: 21 additions & 20 deletions data-sources/api/src/source/TopicMediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@ import me.him188.ani.datasources.api.topic.TopicCategory
abstract class TopicMediaSource : MediaSource {
override val location: MediaSourceLocation get() = MediaSourceLocation.ONLINE

private fun Topic.toOnlineMedia(): DefaultMedia {
val details = details
return DefaultMedia(
mediaId = "$mediaSourceId.${topicId}",
mediaSourceId = mediaSourceId,
originalUrl = originalLink,
download = downloadLink,
originalTitle = rawTitle,
publishedTime = publishedTimeMillis ?: 0,
episodes = details?.episodeRange?.sorts?.toList() ?: emptyList(),
properties = MediaProperties(
subtitleLanguageIds = details?.subtitleLanguages?.map { it.id } ?: emptyList(),
resolution = details?.resolution?.toString() ?: Resolution.R1080P.toString(),
alliance = alliance,
size = size,
),
)
}

// For backward compatibility
protected abstract suspend fun startSearch(query: DownloadSearchQuery): SizedSource<Topic>

Expand All @@ -51,7 +32,7 @@ abstract class TopicMediaSource : MediaSource {
episodeName = query.episodeName,
)
).map {
MediaMatch(it.toOnlineMedia(), MatchKind.FUZZY)
MediaMatch(it.toOnlineMedia(mediaSourceId), MatchKind.FUZZY)
}
}.merge()
}
Expand All @@ -67,3 +48,23 @@ data class DownloadSearchQuery(
val episodeEp: EpisodeSort? = null,
val episodeName: String? = null,
)


fun Topic.toOnlineMedia(mediaSourceId: String): DefaultMedia {
val details = details
return DefaultMedia(
mediaId = "$mediaSourceId.${topicId}",
mediaSourceId = mediaSourceId,
originalUrl = originalLink,
download = downloadLink,
originalTitle = rawTitle,
publishedTime = publishedTimeMillis ?: 0,
episodes = details?.episodeRange?.sorts?.toList() ?: emptyList(),
properties = MediaProperties(
subtitleLanguageIds = details?.subtitleLanguages?.map { it.id } ?: emptyList(),
resolution = details?.resolution?.toString() ?: Resolution.R1080P.toString(),
alliance = alliance,
size = size,
),
)
}
12 changes: 5 additions & 7 deletions data-sources/api/src/topic/Topic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ class TopicDetails(
val subtitleLanguages: List<SubtitleLanguage>,
)

fun DownloadSearchQuery.matches(topic: Topic): Boolean {
val details = topic.details ?: return true
episodeSort?.let { expected ->
val ep = details.episodeRange
if (ep != null && expected !in ep) return false
}
return true
fun DownloadSearchQuery.matches(topic: Topic, allowEpMatch: Boolean): Boolean {
return TopicCriteria(
episodeSort = episodeSort,
episodeEp = episodeEp,
).matches(topic, allowEpMatch)
}

enum class TopicCategory {
Expand Down
46 changes: 46 additions & 0 deletions data-sources/api/src/topic/TopicCriteria.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.him188.ani.datasources.api.topic

import me.him188.ani.datasources.api.EpisodeSort
import me.him188.ani.datasources.api.source.MediaFetchRequest

class TopicCriteria(
val episodeSort: EpisodeSort?,
val episodeEp: EpisodeSort?,
) {
companion object {
val ANY = TopicCriteria(null, null)
}
}

fun TopicCriteria.matches(topic: Topic, allowEpMatch: Boolean): Boolean {
val details = topic.details ?: return true
if (!isEpisodeSortMatch(details)) {
if (!allowEpMatch) return false
if (!isEpisodeEpMatch(details)) return false
}
return true
}

private fun TopicCriteria.isEpisodeSortMatch(details: TopicDetails): Boolean {
episodeSort?.let { expected ->
val ep = details.episodeRange
if (ep != null && expected !in ep) return false
}
return true
}

private fun TopicCriteria.isEpisodeEpMatch(details: TopicDetails): Boolean {
episodeEp?.let { expected ->
val ep = details.episodeRange
if (ep != null && expected !in ep) return false
}
return true
}


fun MediaFetchRequest.toTopicCriteria(): TopicCriteria {
return TopicCriteria(
episodeSort = episodeSort,
episodeEp = episodeEp,
)
}
2 changes: 1 addition & 1 deletion data-sources/dmhy/src/impl/DmhyPagedSourceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class DmhyPagedSourceImpl(
originalLink = topic.link,
)
}.filter {
query.matches(it)
query.matches(it, allowEpMatch = false)
}
if (results.none()) {
noMorePages()
Expand Down
Loading

1 comment on commit 5087be5

@NeKoOuO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

憋了個大的

Please sign in to comment.