Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: re-use StreamInfoItem#toStreamItem from StreamsExtractor #6946

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions app/src/main/java/com/github/libretube/api/StreamsExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ fun VideoStream.toPipedStream(): PipedStream = PipedStream(
contentLength = itagItem?.contentLength ?: 0L
)

fun StreamInfoItem.toStreamItem(
uploaderAvatarUrl: String? = null
): StreamItem = StreamItem(
type = StreamItem.TYPE_STREAM,
url = url.replace(YOUTUBE_FRONTEND_URL, ""),
title = name,
uploaded = uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0,
uploadedDate = textualUploadDate ?: uploadDate?.offsetDateTime()?.toLocalDateTime()?.toLocalDate()
?.toString(),
uploaderName = uploaderName,
uploaderUrl = uploaderUrl.replace(YOUTUBE_FRONTEND_URL, ""),
uploaderAvatar = uploaderAvatarUrl ?: uploaderAvatars.maxByOrNull { it.height }?.url,
thumbnail = thumbnails.maxByOrNull { it.height }?.url,
duration = duration,
views = viewCount,
uploaderVerified = isUploaderVerified,
shortDescription = shortDescription,
isShort = isShortFormContent
)

object StreamsExtractor {
suspend fun extractStreams(videoId: String): Streams {
if (!PlayerHelper.disablePipedProxy || !PlayerHelper.localStreamExtraction) {
Expand Down Expand Up @@ -74,24 +94,7 @@ object StreamsExtractor {
uploadTimestamp = resp.uploadDate.offsetDateTime().toInstant().toKotlinInstant(),
uploaded = resp.uploadDate.offsetDateTime().toEpochSecond() * 1000,
thumbnailUrl = resp.thumbnails.maxBy { it.height }.url,
relatedStreams = resp.relatedItems.filterIsInstance<StreamInfoItem>().map {
StreamItem(
it.url.replace(YOUTUBE_FRONTEND_URL, ""),
StreamItem.TYPE_STREAM,
it.name,
it.thumbnails.maxBy { image -> image.height }.url,
it.uploaderName,
it.uploaderUrl.replace(YOUTUBE_FRONTEND_URL, ""),
it.uploaderAvatars.maxBy { image -> image.height }.url,
it.textualUploadDate,
it.duration,
it.viewCount,
it.isUploaderVerified,
it.uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0L,
it.shortDescription,
it.isShortFormContent,
)
},
relatedStreams = resp.relatedItems.filterIsInstance<StreamInfoItem>().map(StreamInfoItem::toStreamItem),
chapters = resp.streamSegments.map {
ChapterSegment(
title = it.title,
Expand Down
25 changes: 7 additions & 18 deletions app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.libretube.repo
import android.util.Log
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.toStreamItem
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.SubscriptionsFeedItem
Expand Down Expand Up @@ -65,7 +66,10 @@ class LocalFeedRepository : FeedRepository {
}
}

private suspend fun getRelatedStreams(channelId: String, minimumDateMillis: Long): List<StreamItem> {
private suspend fun getRelatedStreams(
channelId: String,
minimumDateMillis: Long
): List<StreamItem> {
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
val feedInfo = FeedInfo.getInfo(channelUrl)

Expand All @@ -88,23 +92,8 @@ class LocalFeedRepository : FeedRepository {
}.flatten().filterIsInstance<StreamInfoItem>()

return related.map { item ->
StreamItem(
type = StreamItem.TYPE_STREAM,
url = item.url.replace(YOUTUBE_FRONTEND_URL, ""),
title = item.name,
uploaded = item.uploadDate?.offsetDateTime()?.toEpochSecond()?.times(1000) ?: 0,
uploadedDate = item.uploadDate?.offsetDateTime()?.toLocalDateTime()?.toLocalDate()
?.toString(),
uploaderName = item.uploaderName,
uploaderUrl = item.uploaderUrl.replace(YOUTUBE_FRONTEND_URL, ""),
uploaderAvatar = channelInfo.avatars.maxByOrNull { it.height }?.url,
thumbnail = item.thumbnails.maxByOrNull { it.height }?.url,
duration = item.duration,
views = item.viewCount,
uploaderVerified = item.isUploaderVerified,
shortDescription = item.shortDescription,
isShort = item.isShortFormContent
)
// avatar is not always included in these info items, thus must be taken from channel info response
item.toStreamItem(channelInfo.avatars.maxByOrNull { it.height }?.url)
}.filter { it.uploaded > minimumDateMillis }
}

Expand Down
Loading