mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 16:30:31 +05:30
Merge pull request #4740 from Bnyro/master
fix: importing playlists without a header containing its name
This commit is contained in:
commit
5b59c81577
@ -20,10 +20,10 @@ import com.github.libretube.obj.NewPipeSubscriptions
|
|||||||
import com.github.libretube.obj.PipedImportPlaylist
|
import com.github.libretube.obj.PipedImportPlaylist
|
||||||
import com.github.libretube.obj.PipedImportPlaylistFile
|
import com.github.libretube.obj.PipedImportPlaylistFile
|
||||||
import java.util.stream.Collectors
|
import java.util.stream.Collectors
|
||||||
import kotlin.streams.toList
|
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.json.decodeFromStream
|
import kotlinx.serialization.json.decodeFromStream
|
||||||
import kotlinx.serialization.json.encodeToStream
|
import kotlinx.serialization.json.encodeToStream
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
object ImportHelper {
|
object ImportHelper {
|
||||||
/**
|
/**
|
||||||
@ -168,20 +168,36 @@ object ImportHelper {
|
|||||||
|
|
||||||
ImportFormat.YOUTUBECSV -> {
|
ImportFormat.YOUTUBECSV -> {
|
||||||
val playlist = PipedImportPlaylist()
|
val playlist = PipedImportPlaylist()
|
||||||
activity.contentResolver.openInputStream(uri)?.use {
|
activity.contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||||
val lines = it.bufferedReader().use { reader ->
|
val lines = inputStream.bufferedReader().use { reader ->
|
||||||
reader.lines().collect(Collectors.toList())
|
reader.lines().collect(Collectors.toList())
|
||||||
}
|
}
|
||||||
playlist.name = lines[1].split(",").reversed()[2]
|
// invalid playlist file, hence returning
|
||||||
var splitIndex = lines.indexOfFirst { line -> line.isBlank() }
|
if (lines.size < 2) return
|
||||||
// seek until playlist items table
|
|
||||||
while (lines.getOrNull(splitIndex + 1).orEmpty().isBlank()) {
|
val playlistName = lines[1].split(",").reversed().getOrNull(2)
|
||||||
splitIndex++
|
// the playlist name can be undefined in some cases, e.g. watch later lists
|
||||||
|
playlist.name = playlistName ?: Date().toString()
|
||||||
|
|
||||||
|
// start directly at the beginning if header playlist info such as name is missing
|
||||||
|
val startIndex = if (playlistName == null) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
// seek to the first blank line
|
||||||
|
var splitIndex = lines.indexOfFirst { line -> line.isBlank() }
|
||||||
|
while (lines.getOrElse(splitIndex) { return }.isBlank()) splitIndex++
|
||||||
|
// skip the line containing the names of the columns
|
||||||
|
splitIndex + 2
|
||||||
}
|
}
|
||||||
lines.subList(splitIndex + 2, lines.size).forEach { line ->
|
for (line in lines.subList(startIndex, lines.size)) {
|
||||||
line.split(",").firstOrNull()?.let { videoId ->
|
if (line.isBlank()) continue
|
||||||
if (videoId.isNotBlank()) playlist.videos += videoId.trim()
|
|
||||||
}
|
line.split(",")
|
||||||
|
.firstOrNull()
|
||||||
|
?.takeIf { it.isNotBlank() }
|
||||||
|
?.let { videoId ->
|
||||||
|
playlist.videos += videoId.trim()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
importPlaylists.add(playlist)
|
importPlaylists.add(playlist)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user