Skip to content

Commit

Permalink
修复未追番筛选不到资源的问题, close #372, close #365
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Jun 3, 2024
1 parent 5b5efaa commit 25cfdea
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
10 changes: 9 additions & 1 deletion app/shared/data/common/data/subject/SubjectManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ interface SubjectManager {
*/
suspend fun getEpisode(episodeId: Int): Episode

suspend fun getEpisodeCollections(subjectId: Int): List<UserEpisodeCollection>

fun episodeCollectionFlow(subjectId: Int, episodeId: Int, contentPolicy: ContentPolicy): Flow<UserEpisodeCollection>

suspend fun setSubjectCollectionType(subjectId: Int, type: UnifiedCollectionType)
Expand Down Expand Up @@ -196,7 +198,6 @@ class SubjectManagerImpl(
episodeSort = episode.episode.sort.toString(),
watchStatus = episode.type.toCollectionType(),
isOnAir = episode.episode.isOnAir(),
airDate = PackedDate.parseFromDate(episode.episode.airdate),
cacheStatus = cacheStatus,
)
}
Expand Down Expand Up @@ -226,6 +227,13 @@ class SubjectManagerImpl(
}
}

override suspend fun getEpisodeCollections(subjectId: Int): List<UserEpisodeCollection> {
return findCachedSubjectCollection(subjectId)?._episodes
?: runUntilSuccess {
episodeRepository.getSubjectEpisodeCollection(subjectId, EpType.MainStory)
}.toList()
}

override fun episodeCollectionFlow(
subjectId: Int,
episodeId: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
Expand All @@ -26,13 +27,13 @@ import me.him188.ani.app.data.repositories.SubjectRepository
import me.him188.ani.app.data.subject.PackedDate
import me.him188.ani.app.data.subject.SubjectManager
import me.him188.ani.app.data.subject.minus
import me.him188.ani.app.tools.caching.ContentPolicy
import me.him188.ani.app.ui.foundation.BackgroundScope
import me.him188.ani.app.ui.foundation.HasBackgroundScope
import me.him188.ani.app.ui.foundation.launchInBackground
import me.him188.ani.datasources.api.EpisodeSort
import me.him188.ani.datasources.api.Media
import me.him188.ani.datasources.api.source.MediaFetchRequest
import me.him188.ani.datasources.bangumi.processing.isOnAir
import me.him188.ani.datasources.bangumi.processing.nameCNOrName
import me.him188.ani.datasources.core.fetch.MediaFetchSession
import me.him188.ani.datasources.core.fetch.MediaFetcher
Expand Down Expand Up @@ -204,17 +205,22 @@ internal class DefaultEpisodeMediaFetchSession(
)
}

val subjectProgress = subjectManager.subjectProgressFlow(subjectId, ContentPolicy.CACHE_ONLY)
val subjectProgress = flow {
emit(subjectManager.getEpisodeCollections(subjectId).map { it.episode })
}

DefaultMediaSelector(
mediaSelectorContextNotCached = subjectProgress.map { eps ->
val allEpisodesFinished = eps.fastAll { it.isOnAir == false }
val allEpisodesFinished = eps.fastAll { it.isOnAir() == false }

val finishedLongTimeAgo = allEpisodesFinished || run {
val now = PackedDate.now()
val maxAirDate = eps
.filter { it.airDate.isValid }
.maxOfOrNull { it.airDate }
.map {
PackedDate.parseFromDate(it.airdate)
}
.filter { it.isValid }
.maxOrNull()

maxAirDate != null && now - maxAirDate >= 14.days
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,71 +24,62 @@ private val testEpisodes = listOf(
episodeSort = "00",
watchStatus = UnifiedCollectionType.DONE,
isOnAir = false,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.Caching(0.3f, 300.megaBytes),
),
EpisodeProgressItem(
episodeId = 1,
episodeSort = "01",
watchStatus = UnifiedCollectionType.DONE,
isOnAir = false,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.NotCached,
),
EpisodeProgressItem(
episodeId = 2,
episodeSort = "02",
watchStatus = UnifiedCollectionType.DONE,
isOnAir = false,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.Cached(300.megaBytes),
),
EpisodeProgressItem(
episodeId = 3,
episodeSort = "03",
watchStatus = UnifiedCollectionType.WISH,
isOnAir = false,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.Cached(300.megaBytes),
),
EpisodeProgressItem(
episodeId = 4,
episodeSort = "04",
watchStatus = UnifiedCollectionType.WISH,
isOnAir = false,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.Caching(0.7f, 300.megaBytes),
),
EpisodeProgressItem(
episodeId = 5,
episodeSort = "05",
watchStatus = UnifiedCollectionType.WISH,
isOnAir = false,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.NotCached,
),
EpisodeProgressItem(
episodeId = 6,
episodeSort = "06",
watchStatus = UnifiedCollectionType.WISH,
isOnAir = true,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.NotCached,
),
EpisodeProgressItem(
episodeId = 7,
episodeSort = "07",
watchStatus = UnifiedCollectionType.WISH,
isOnAir = true,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.Cached(300.megaBytes),
),
EpisodeProgressItem(
episodeId = 8,
episodeSort = "08",
watchStatus = UnifiedCollectionType.WISH,
isOnAir = true,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.Caching(0.3f, 300.megaBytes),
),
)
Expand Down Expand Up @@ -170,6 +161,5 @@ private fun item(id: Int) = EpisodeProgressItem(
episodeSort = id.toString(),
watchStatus = UnifiedCollectionType.WISH,
isOnAir = true,
airDate = airDate,
cacheStatus = EpisodeCacheStatus.Caching(0.3f, 300.megaBytes),
)
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,15 @@ class EpisodeProgressItem(
val episodeSort: String,
val watchStatus: UnifiedCollectionType,
val isOnAir: Boolean?,
val airDate: PackedDate,
val cacheStatus: EpisodeCacheStatus?,
) {
var isLoading by mutableStateOf(false)
}

class EpisodeInfo(
val airDate: PackedDate,
)


@Composable
fun EpisodeProgressRow(
Expand Down
39 changes: 27 additions & 12 deletions data-sources/nyafun/src/NyafunMediaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.ktor.http.isSuccess
import io.ktor.utils.io.jvm.javaio.toInputStream
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flow
Expand Down Expand Up @@ -134,12 +133,23 @@ class NyafunMediaSource(config: MediaSourceConfig) : MediaSource {
alliance = ID,
size = FileSize.Unspecified,
),
episodeRange = EpisodeRange.single(sort),
episodeRange = EpisodeRange.single(
if (isPossiblyMovie(ep.name) && sort is EpisodeSort.Special) {
EpisodeSort(1) // 电影总是 01
} else {
sort
}
),
location = MediaSourceLocation.Online,
kind = MediaSourceKind.WEB,
), MatchKind.FUZZY
)
}

private fun isPossiblyMovie(title: String): Boolean {
val t = title
return ("" in t || "" in t) && ("2160P" in t || "1440P" in t || "2K" in t || "4K" in t || "1080P" in t || "720P" in t)
}
}

class Factory : MediaSourceFactory {
Expand Down Expand Up @@ -183,26 +193,31 @@ class NyafunMediaSource(config: MediaSourceConfig) : MediaSource {

bangumiList.asFlow()
.flatMapMerge { bangumi ->
flow {
val result = flow {
emit(getDocument(bangumi.url))
}.map {
parseEpisodeList(it)
}.retry(3) { e ->
logger.warn(e) { "Failed to get episodes using name '$name'" }
true
}.firstOrNull()?.map { ep ->
createMediaMatch(bangumi, ep)
}.orEmpty().also {
logger.info { "$ID fetched ${it.size} episodes for '$name': ${it.joinToString { it.media.episodeRange.toString() }}" }
}.asFlow()
}
.filter {
it.definitelyMatches(query)
}.firstOrNull()
.orEmpty()
.asSequence()
.map { ep ->
createMediaMatch(bangumi, ep)
}
.filter {
it.definitelyMatches(query) ||
isPossiblyMovie(it.media.originalTitle)
}
.toList()

logger.info { "$ID fetched ${result.size} episodes for '$name': ${result.joinToString { it.media.episodeRange.toString() }}" }
result.asFlow()
}
}
}


private suspend inline fun getDocument(
url: String,
block: HttpRequestBuilder.() -> Unit = {}
Expand Down

0 comments on commit 25cfdea

Please sign in to comment.