Skip to content

Commit 60564bb

Browse files
committed
fix(Feed): extract shorts uploadDate from feedInfo
Shorts extracted from the Shorts tab do not have an uploadDate. They are currently saved with their uploadDate set to -1. This results in them to appear at the end of the feed. To fix this, we can use the generic FeedInfo that contains the uploadDate. This also fixes an issue, where old shorts were fetched on every refresh. Ref: libre-tube#7111
1 parent 4b74f3c commit 60564bb

2 files changed

Lines changed: 35 additions & 24 deletions

File tree

app/src/main/java/com/github/libretube/api/StreamsExtractor.kt

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,33 @@ fun AudioStream.toPipedStream() = PipedStream(
6262
)
6363

6464
fun StreamInfoItem.toStreamItem(
65-
uploaderAvatarUrl: String? = null
66-
) = StreamItem(
67-
type = StreamItem.TYPE_STREAM,
68-
url = url.toID(),
69-
title = name,
70-
uploaded = uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: -1,
71-
uploadedDate = textualUploadDate ?: uploadDate?.offsetDateTime()?.toLocalDateTime()
72-
?.toLocalDate()
73-
?.toString(),
74-
uploaderName = uploaderName,
75-
uploaderUrl = uploaderUrl.toID(),
76-
uploaderAvatar = uploaderAvatarUrl ?: uploaderAvatars.maxByOrNull { it.height }?.url,
77-
thumbnail = thumbnails.maxByOrNull { it.height }?.url,
78-
duration = duration,
79-
views = viewCount,
80-
uploaderVerified = isUploaderVerified,
81-
shortDescription = shortDescription,
82-
isShort = isShortFormContent
83-
)
65+
uploaderAvatarUrl: String? = null,
66+
feedInfo: StreamInfoItem? = null,
67+
): StreamItem {
68+
val uploadDate = uploadDate ?: feedInfo?.uploadDate
69+
val textualUploadDate = textualUploadDate ?: feedInfo?.textualUploadDate
70+
71+
return StreamItem(
72+
type = StreamItem.TYPE_STREAM,
73+
url = url.toID(),
74+
title = name,
75+
uploaded = uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000)
76+
?: -1,
77+
uploadedDate = textualUploadDate ?: uploadDate
78+
?.offsetDateTime()?.toLocalDateTime()
79+
?.toLocalDate()
80+
?.toString(),
81+
uploaderName = uploaderName,
82+
uploaderUrl = uploaderUrl.toID(),
83+
uploaderAvatar = uploaderAvatarUrl ?: uploaderAvatars.maxByOrNull { it.height }?.url,
84+
thumbnail = thumbnails.maxByOrNull { it.height }?.url,
85+
duration = duration,
86+
views = viewCount,
87+
uploaderVerified = isUploaderVerified,
88+
shortDescription = shortDescription,
89+
isShort = isShortFormContent
90+
)
91+
}
8492

8593
object StreamsExtractor {
8694
suspend fun extractStreams(videoId: String): Streams = withContext(Dispatchers.IO) {

app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class LocalFeedRepository : FeedRepository {
120120
): List<StreamItem> {
121121
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
122122
val feedInfo = FeedInfo.getInfo(channelUrl)
123+
val feedInfoItems = feedInfo.relatedItems.associateBy { it.url }
123124

124125
val mostRecentChannelVideo = feedInfo.relatedItems.maxBy {
125126
it.uploadDate?.offsetDateTime()?.toInstant()?.toEpochMilli() ?: 0
@@ -146,13 +147,15 @@ class LocalFeedRepository : FeedRepository {
146147
}.getOrElse { emptyList() }
147148
}.flatten().filterIsInstance<StreamInfoItem>()
148149

150+
val channelAvatar = channelInfo.avatars.maxByOrNull { it.height }?.url
149151
return related.map { item ->
150152
// avatar is not always included in these info items, thus must be taken from channel info response
151-
item.toStreamItem(channelInfo.avatars.maxByOrNull { it.height }?.url)
152-
}.filter {
153-
// shorts don't have upload dates apparently
154-
it.isShort || it.uploaded > minimumDateMillis
155-
}
153+
item.toStreamItem(
154+
channelAvatar,
155+
// shorts fetched via the shorts tab don't have upload dates so we fall back to the feedInfo
156+
feedInfoItems[item.url]
157+
)
158+
}.filter { it.uploaded > minimumDateMillis }
156159
}
157160

158161
companion object {

0 commit comments

Comments
 (0)