mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
fix: dearrow for playing video and watch history
This commit is contained in:
parent
3cef1505f6
commit
ccaedfafbc
@ -125,6 +125,6 @@ object SubscriptionHelper {
|
||||
subscriptions.joinToString(",")
|
||||
)
|
||||
}
|
||||
}.deArrow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Streams(
|
||||
val title: String,
|
||||
var title: String,
|
||||
val description: String,
|
||||
|
||||
@Serializable(SafeInstantSerializer::class)
|
||||
@ -24,7 +24,7 @@ data class Streams(
|
||||
val uploader: String,
|
||||
val uploaderUrl: String,
|
||||
val uploaderAvatar: String? = null,
|
||||
val thumbnailUrl: String,
|
||||
var thumbnailUrl: String,
|
||||
val category: String,
|
||||
val license: String = "YouTube licence",
|
||||
val visibility: String = "public",
|
||||
|
@ -85,7 +85,7 @@ class HomeViewModel : ViewModel() {
|
||||
private suspend fun loadFeed(subscriptionsViewModel: SubscriptionsViewModel) {
|
||||
runSafely(
|
||||
onSuccess = { videos -> feed.updateIfChanged(videos) },
|
||||
ioBlock = { tryLoadFeed(subscriptionsViewModel) }
|
||||
ioBlock = { tryLoadFeed(subscriptionsViewModel).deArrow().take(20) }
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -60,9 +60,7 @@ class PlayerViewModel : ViewModel() {
|
||||
if (isOrientationChangeInProgress && streamsInfo != null) return@withContext streamsInfo to null
|
||||
|
||||
streamsInfo = try {
|
||||
RetrofitInstance.api.getStreams(videoId).apply {
|
||||
relatedStreams = relatedStreams.deArrow()
|
||||
}
|
||||
RetrofitInstance.api.getStreams(videoId).deArrow(videoId)
|
||||
} catch (e: IOException) {
|
||||
return@withContext null to context.getString(R.string.unknown_error)
|
||||
} catch (e: HttpException) {
|
||||
|
@ -5,6 +5,7 @@ import com.github.libretube.api.RetrofitInstance
|
||||
import com.github.libretube.api.obj.ContentItem
|
||||
import com.github.libretube.api.obj.DeArrowContent
|
||||
import com.github.libretube.api.obj.StreamItem
|
||||
import com.github.libretube.api.obj.Streams
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.extensions.toID
|
||||
import com.github.libretube.helpers.PreferenceHelper
|
||||
@ -19,26 +20,58 @@ object DeArrowUtil {
|
||||
return newTitle to newThumbnail
|
||||
}
|
||||
|
||||
private suspend fun fetchDeArrowContent(videoIds: List<String>): Map<String, DeArrowContent>? {
|
||||
val videoIdsString = videoIds.mapTo(TreeSet()) { it }.joinToString(",")
|
||||
|
||||
return try {
|
||||
RetrofitInstance.api.getDeArrowContent(videoIdsString)
|
||||
} catch (e: Exception) {
|
||||
Log.e(this::class.java.name, e.toString())
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the new titles and thumbnails generated by DeArrow to the streams item
|
||||
*/
|
||||
suspend fun deArrowStreams(streams: Streams, vidId: String): Streams {
|
||||
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return streams
|
||||
|
||||
val videoIds = listOf(vidId) + streams.relatedStreams.map { it.url!!.toID() }
|
||||
val response = fetchDeArrowContent(videoIds) ?: return streams
|
||||
|
||||
for ((videoId, data) in response.entries) {
|
||||
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)
|
||||
|
||||
if (videoId == vidId) {
|
||||
if (newTitle != null) streams.title = newTitle
|
||||
if (newThumbnail != null) streams.thumbnailUrl = newThumbnail
|
||||
} else {
|
||||
val streamItem = streams.relatedStreams
|
||||
.firstOrNull { it.url?.toID() == videoId } ?: continue
|
||||
|
||||
if (newTitle != null) streamItem.title = newTitle
|
||||
if (newThumbnail != null) streamItem.thumbnail = newThumbnail
|
||||
}
|
||||
}
|
||||
|
||||
return streams
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the new titles and thumbnails generated by DeArrow to the stream items
|
||||
*/
|
||||
suspend fun deArrowStreamItems(streamItems: List<StreamItem>): List<StreamItem> {
|
||||
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return streamItems
|
||||
|
||||
val videoIds = streamItems.mapNotNullTo(TreeSet()) { it.url?.toID() }
|
||||
.joinToString(",")
|
||||
val response = fetchDeArrowContent(streamItems.map{ it.url!!.toID() }) ?: return streamItems
|
||||
|
||||
val response = try {
|
||||
RetrofitInstance.api.getDeArrowContent(videoIds)
|
||||
} catch (e: Exception) {
|
||||
Log.e(this::class.java.name, e.toString())
|
||||
return streamItems
|
||||
}
|
||||
for ((videoId, data) in response.entries) {
|
||||
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)
|
||||
val streamItem = streamItems.firstOrNull { it.url?.toID() == videoId }
|
||||
newTitle?.let { streamItem?.title = newTitle }
|
||||
newThumbnail?.let { streamItem?.thumbnail = newThumbnail }
|
||||
val streamItem = streamItems.firstOrNull { it.url?.toID() == videoId } ?: continue
|
||||
|
||||
if (newTitle != null) streamItem.title = newTitle
|
||||
if (newThumbnail != null) streamItem.thumbnail = newThumbnail
|
||||
}
|
||||
return streamItems
|
||||
}
|
||||
@ -50,22 +83,18 @@ object DeArrowUtil {
|
||||
if (!PreferenceHelper.getBoolean(PreferenceKeys.DEARROW, false)) return contentItems
|
||||
|
||||
val videoIds = contentItems.filter { it.type == "stream" }
|
||||
.mapTo(TreeSet()) { it.url.toID() }
|
||||
.joinToString(",")
|
||||
.map { it.url.toID() }
|
||||
|
||||
if (videoIds.isEmpty()) return contentItems
|
||||
|
||||
val response = try {
|
||||
RetrofitInstance.api.getDeArrowContent(videoIds)
|
||||
} catch (e: Exception) {
|
||||
Log.e(this::class.java.name, e.toString())
|
||||
return contentItems
|
||||
}
|
||||
val response = fetchDeArrowContent(videoIds) ?: return contentItems
|
||||
|
||||
for ((videoId, data) in response.entries) {
|
||||
val (newTitle, newThumbnail) = extractTitleAndThumbnail(data)
|
||||
val contentItem = contentItems.firstOrNull { it.url.toID() == videoId }
|
||||
newTitle?.let { contentItem?.title = newTitle }
|
||||
newThumbnail?.let { contentItem?.thumbnail = newThumbnail }
|
||||
val contentItem = contentItems.firstOrNull { it.url.toID() == videoId } ?: continue
|
||||
|
||||
if (newTitle != null) { contentItem.title = newTitle }
|
||||
if (newThumbnail != null) { contentItem.thumbnail = newThumbnail }
|
||||
}
|
||||
return contentItems
|
||||
}
|
||||
@ -84,3 +113,6 @@ suspend fun List<StreamItem>.deArrow() = DeArrowUtil.deArrowStreamItems(this)
|
||||
*/
|
||||
@JvmName("deArrowContentItems")
|
||||
suspend fun List<ContentItem>.deArrow() = DeArrowUtil.deArrowContentItems(this)
|
||||
|
||||
@JvmName("deArrowStreams")
|
||||
suspend fun Streams.deArrow(videoId: String) = DeArrowUtil.deArrowStreams(this, videoId)
|
Loading…
x
Reference in New Issue
Block a user