Skip to content

Commit 755f24b

Browse files
authored
Merge pull request #6946 from Bnyro/master
refactor: re-use StreamInfoItem#toStreamItem from StreamsExtractor
2 parents d987522 + 92dba35 commit 755f24b

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ fun VideoStream.toPipedStream(): PipedStream = PipedStream(
3636
contentLength = itagItem?.contentLength ?: 0L
3737
)
3838

39+
fun StreamInfoItem.toStreamItem(
40+
uploaderAvatarUrl: String? = null
41+
): StreamItem = StreamItem(
42+
type = StreamItem.TYPE_STREAM,
43+
url = url.replace(YOUTUBE_FRONTEND_URL, ""),
44+
title = name,
45+
uploaded = uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0,
46+
uploadedDate = textualUploadDate ?: uploadDate?.offsetDateTime()?.toLocalDateTime()?.toLocalDate()
47+
?.toString(),
48+
uploaderName = uploaderName,
49+
uploaderUrl = uploaderUrl.replace(YOUTUBE_FRONTEND_URL, ""),
50+
uploaderAvatar = uploaderAvatarUrl ?: uploaderAvatars.maxByOrNull { it.height }?.url,
51+
thumbnail = thumbnails.maxByOrNull { it.height }?.url,
52+
duration = duration,
53+
views = viewCount,
54+
uploaderVerified = isUploaderVerified,
55+
shortDescription = shortDescription,
56+
isShort = isShortFormContent
57+
)
58+
3959
object StreamsExtractor {
4060
suspend fun extractStreams(videoId: String): Streams {
4161
if (!PlayerHelper.disablePipedProxy || !PlayerHelper.localStreamExtraction) {
@@ -74,24 +94,7 @@ object StreamsExtractor {
7494
uploadTimestamp = resp.uploadDate.offsetDateTime().toInstant().toKotlinInstant(),
7595
uploaded = resp.uploadDate.offsetDateTime().toEpochSecond() * 1000,
7696
thumbnailUrl = resp.thumbnails.maxBy { it.height }.url,
77-
relatedStreams = resp.relatedItems.filterIsInstance<StreamInfoItem>().map {
78-
StreamItem(
79-
it.url.replace(YOUTUBE_FRONTEND_URL, ""),
80-
StreamItem.TYPE_STREAM,
81-
it.name,
82-
it.thumbnails.maxBy { image -> image.height }.url,
83-
it.uploaderName,
84-
it.uploaderUrl.replace(YOUTUBE_FRONTEND_URL, ""),
85-
it.uploaderAvatars.maxBy { image -> image.height }.url,
86-
it.textualUploadDate,
87-
it.duration,
88-
it.viewCount,
89-
it.isUploaderVerified,
90-
it.uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0L,
91-
it.shortDescription,
92-
it.isShortFormContent,
93-
)
94-
},
97+
relatedStreams = resp.relatedItems.filterIsInstance<StreamInfoItem>().map(StreamInfoItem::toStreamItem),
9598
chapters = resp.streamSegments.map {
9699
ChapterSegment(
97100
title = it.title,

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.libretube.repo
33
import android.util.Log
44
import com.github.libretube.api.SubscriptionHelper
55
import com.github.libretube.api.obj.StreamItem
6+
import com.github.libretube.api.toStreamItem
67
import com.github.libretube.constants.PreferenceKeys
78
import com.github.libretube.db.DatabaseHolder
89
import com.github.libretube.db.obj.SubscriptionsFeedItem
@@ -72,7 +73,10 @@ class LocalFeedRepository : FeedRepository {
7273
}
7374
}
7475

75-
private suspend fun getRelatedStreams(channelId: String, minimumDateMillis: Long): List<StreamItem> {
76+
private suspend fun getRelatedStreams(
77+
channelId: String,
78+
minimumDateMillis: Long
79+
): List<StreamItem> {
7680
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
7781
val feedInfo = FeedInfo.getInfo(channelUrl)
7882

@@ -95,23 +99,8 @@ class LocalFeedRepository : FeedRepository {
9599
}.flatten().filterIsInstance<StreamInfoItem>()
96100

97101
return related.map { item ->
98-
StreamItem(
99-
type = StreamItem.TYPE_STREAM,
100-
url = item.url.replace(YOUTUBE_FRONTEND_URL, ""),
101-
title = item.name,
102-
uploaded = item.uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0,
103-
uploadedDate = item.uploadDate?.offsetDateTime()?.toLocalDateTime()?.toLocalDate()
104-
?.toString(),
105-
uploaderName = item.uploaderName,
106-
uploaderUrl = item.uploaderUrl.replace(YOUTUBE_FRONTEND_URL, ""),
107-
uploaderAvatar = channelInfo.avatars.maxByOrNull { it.height }?.url,
108-
thumbnail = item.thumbnails.maxByOrNull { it.height }?.url,
109-
duration = item.duration,
110-
views = item.viewCount,
111-
uploaderVerified = item.isUploaderVerified,
112-
shortDescription = item.shortDescription,
113-
isShort = item.isShortFormContent
114-
)
102+
// avatar is not always included in these info items, thus must be taken from channel info response
103+
item.toStreamItem(channelInfo.avatars.maxByOrNull { it.height }?.url)
115104
}.filter { it.uploaded > minimumDateMillis }
116105
}
117106

0 commit comments

Comments
 (0)