mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Simplify toDownloadItems() method
This commit is contained in:
parent
cfce1fa9f9
commit
1175d18d77
@ -3,6 +3,7 @@ package com.github.libretube.api.obj
|
||||
import com.github.libretube.db.obj.DownloadItem
|
||||
import com.github.libretube.enums.FileType
|
||||
import com.github.libretube.helpers.ProxyHelper
|
||||
import com.github.libretube.parcelable.DownloadData
|
||||
import java.nio.file.Paths
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.serialization.Serializable
|
||||
@ -35,40 +36,33 @@ data class Streams(
|
||||
val uploaderSubscriberCount: Long = 0,
|
||||
val previewFrames: List<PreviewFrames> = emptyList()
|
||||
) {
|
||||
fun toDownloadItems(
|
||||
videoId: String,
|
||||
fileName: String,
|
||||
videoFormat: String?,
|
||||
videoQuality: String?,
|
||||
audioFormat: String?,
|
||||
audioQuality: String?,
|
||||
subtitleCode: String?
|
||||
): List<DownloadItem> {
|
||||
fun toDownloadItems(downloadData: DownloadData): List<DownloadItem> {
|
||||
val (id, name, videoFormat, videoQuality, audioFormat, audioQuality, subCode) = downloadData
|
||||
val items = mutableListOf<DownloadItem>()
|
||||
|
||||
if (!videoQuality.isNullOrEmpty() && !videoFormat.isNullOrEmpty()) {
|
||||
val stream = videoStreams.find {
|
||||
it.quality == videoQuality && it.format == videoFormat
|
||||
}
|
||||
stream?.toDownloadItem(FileType.VIDEO, videoId, fileName)?.let { items.add(it) }
|
||||
stream?.toDownloadItem(FileType.VIDEO, id, name)?.let { items.add(it) }
|
||||
}
|
||||
|
||||
if (!audioQuality.isNullOrEmpty() && !audioFormat.isNullOrEmpty()) {
|
||||
val stream = audioStreams.find {
|
||||
it.quality == audioQuality && it.format == audioFormat
|
||||
}
|
||||
stream?.toDownloadItem(FileType.AUDIO, videoId, fileName)?.let { items.add(it) }
|
||||
stream?.toDownloadItem(FileType.AUDIO, id, name)?.let { items.add(it) }
|
||||
}
|
||||
|
||||
if (!subtitleCode.isNullOrEmpty()) {
|
||||
if (!subCode.isNullOrEmpty()) {
|
||||
items.add(
|
||||
DownloadItem(
|
||||
type = FileType.SUBTITLE,
|
||||
videoId = videoId,
|
||||
fileName = "${fileName}_$subtitleCode.srt",
|
||||
videoId = id,
|
||||
fileName = "${name}_$subCode.srt",
|
||||
path = Paths.get(""),
|
||||
url = subtitles.find {
|
||||
it.code == subtitleCode
|
||||
it.code == subCode
|
||||
}?.url?.let { ProxyHelper.unwrapIfEnabled(it) }
|
||||
)
|
||||
)
|
||||
|
@ -6,7 +6,7 @@ import kotlinx.parcelize.Parcelize
|
||||
@Parcelize
|
||||
data class DownloadData(
|
||||
val videoId: String,
|
||||
val fileName: String?,
|
||||
val fileName: String,
|
||||
val videoFormat: String?,
|
||||
val videoQuality: String?,
|
||||
val audioFormat: String?,
|
||||
|
@ -92,10 +92,10 @@ class DownloadService : LifecycleService() {
|
||||
ACTION_DOWNLOAD_PAUSE -> pause(intent.getIntExtra("id", -1))
|
||||
}
|
||||
|
||||
val (videoId, name, videoFormat, videoQuality, audioFormat, audioQuality, subtitleCode) =
|
||||
intent?.parcelableExtra<DownloadData>(IntentData.downloadData)
|
||||
?: return START_NOT_STICKY
|
||||
val fileName = name ?: videoId
|
||||
val downloadData = intent?.parcelableExtra<DownloadData>(IntentData.downloadData)
|
||||
?: return START_NOT_STICKY
|
||||
val (videoId, name) = downloadData
|
||||
val fileName = name.ifEmpty { videoId }
|
||||
|
||||
lifecycleScope.launch(coroutineContext) {
|
||||
try {
|
||||
@ -120,15 +120,7 @@ class DownloadService : LifecycleService() {
|
||||
thumbnailTargetPath
|
||||
)
|
||||
|
||||
val downloadItems = streams.toDownloadItems(
|
||||
videoId,
|
||||
fileName,
|
||||
videoFormat,
|
||||
videoQuality,
|
||||
audioFormat,
|
||||
audioQuality,
|
||||
subtitleCode
|
||||
)
|
||||
val downloadItems = streams.toDownloadItems(downloadData.copy(fileName = fileName))
|
||||
downloadItems.forEach { start(it) }
|
||||
} catch (e: Exception) {
|
||||
return@launch
|
||||
|
@ -174,7 +174,7 @@ class DownloadDialog(
|
||||
|
||||
val downloadData = DownloadData(
|
||||
videoId = videoId,
|
||||
fileName = binding.fileName.text?.toString(),
|
||||
fileName = binding.fileName.text?.toString().orEmpty(),
|
||||
videoFormat = videoStream?.format,
|
||||
videoQuality = videoStream?.quality,
|
||||
audioFormat = audioStream?.format,
|
||||
|
Loading…
x
Reference in New Issue
Block a user