Skip to content

Commit 2c7657d

Browse files
authored
Merge pull request libre-tube#7287 from Bnyro/master
fix: skip already downloaded videos when downloading playlists
2 parents 1d0ce3b + 50c2e18 commit 2c7657d

1 file changed

Lines changed: 35 additions & 28 deletions

File tree

app/src/main/java/com/github/libretube/services/PlaylistDownloadEnqueueService.kt

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.github.libretube.api.PlaylistsHelper
1515
import com.github.libretube.api.obj.PipedStream
1616
import com.github.libretube.api.obj.StreamItem
1717
import com.github.libretube.constants.IntentData
18+
import com.github.libretube.db.DatabaseHolder
1819
import com.github.libretube.enums.NotificationId
1920
import com.github.libretube.enums.PlaylistType
2021
import com.github.libretube.extensions.getWhileDigit
@@ -136,35 +137,41 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
136137
nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification())
137138

138139
for (stream in streams) {
139-
val videoInfo = runCatching {
140-
MediaServiceRepository.instance.getStreams(stream.url!!.toID())
141-
}.getOrNull() ?: continue
142-
143-
val videoStream = getStream(videoInfo.videoStreams, maxVideoQuality)
144-
val audioStream = getStream(videoInfo.audioStreams, maxAudioQuality)
145-
146-
// remove all UNIX reserved characters from the title in order to generate
147-
// a valid filename
148-
var fileName = videoInfo.title
149-
TextUtils.RESERVED_CHARS.forEach {
150-
fileName = fileName.replace(it, '_')
151-
}
152-
153-
val downloadData = DownloadData(
154-
videoId = stream.url!!.toID(),
155-
fileName = fileName,
156-
videoFormat = videoStream?.format,
157-
videoQuality = videoStream?.quality,
158-
audioFormat = audioStream?.format,
159-
audioQuality = audioStream?.quality,
160-
audioLanguage = audioLanguage.takeIf {
161-
videoInfo.audioStreams.any { it.audioTrackLocale == audioLanguage }
162-
},
163-
subtitleCode = captionLanguage.takeIf {
164-
videoInfo.subtitles.any { it.code == captionLanguage }
140+
val videoId = stream.url!!.toID()
141+
142+
// only download videos that have not been downloaded before
143+
if (!DatabaseHolder.Database.downloadDao().exists(videoId)) {
144+
val videoInfo = runCatching {
145+
MediaServiceRepository.instance.getStreams(videoId)
146+
}.getOrNull() ?: continue
147+
148+
val videoStream = getStream(videoInfo.videoStreams, maxVideoQuality)
149+
val audioStream = getStream(videoInfo.audioStreams, maxAudioQuality)
150+
151+
// remove all UNIX reserved characters from the title in order to generate
152+
// a valid filename
153+
var fileName = videoInfo.title
154+
TextUtils.RESERVED_CHARS.forEach {
155+
fileName = fileName.replace(it, '_')
165156
}
166-
)
167-
DownloadHelper.startDownloadService(this, downloadData)
157+
158+
val downloadData = DownloadData(
159+
videoId = videoId,
160+
fileName = fileName,
161+
videoFormat = videoStream?.format,
162+
videoQuality = videoStream?.quality,
163+
audioFormat = audioStream?.format,
164+
audioQuality = audioStream?.quality,
165+
audioLanguage = audioLanguage.takeIf {
166+
videoInfo.audioStreams.any { it.audioTrackLocale == audioLanguage }
167+
},
168+
subtitleCode = captionLanguage.takeIf {
169+
videoInfo.subtitles.any { it.code == captionLanguage }
170+
}
171+
)
172+
DownloadHelper.startDownloadService(this, downloadData)
173+
}
174+
// TODO: inform the user if an already downloaded video has been skipped
168175

169176
amountOfVideosDone++
170177
nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification())

0 commit comments

Comments
 (0)