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