mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Playlists import from YouTube
This commit is contained in:
parent
197228f6d5
commit
5cf055f41b
@ -1,7 +1,6 @@
|
|||||||
package com.github.libretube.api
|
package com.github.libretube.api
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.obj.Playlist
|
import com.github.libretube.api.obj.Playlist
|
||||||
@ -207,7 +206,7 @@ object PlaylistsHelper {
|
|||||||
addToPlaylist(
|
addToPlaylist(
|
||||||
playlistId,
|
playlistId,
|
||||||
*playlist.videos.map {
|
*playlist.videos.map {
|
||||||
Uri.parse(it).getQueryParameter("v")!!
|
it.substring(it.length - 11, it.length)
|
||||||
}.toTypedArray()
|
}.toTypedArray()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package com.github.libretube.obj
|
package com.github.libretube.obj
|
||||||
|
|
||||||
data class ImportPlaylist(
|
data class ImportPlaylist(
|
||||||
val name: String? = null,
|
var name: String? = null,
|
||||||
val type: String? = null,
|
val type: String? = null,
|
||||||
val visibility: String? = null,
|
val visibility: String? = null,
|
||||||
val videos: List<String> = listOf()
|
var videos: List<String> = listOf()
|
||||||
)
|
)
|
||||||
|
@ -11,6 +11,7 @@ import com.github.libretube.api.RetrofitInstance
|
|||||||
import com.github.libretube.api.SubscriptionHelper
|
import com.github.libretube.api.SubscriptionHelper
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.extensions.toastFromMainThread
|
import com.github.libretube.extensions.toastFromMainThread
|
||||||
|
import com.github.libretube.obj.ImportPlaylist
|
||||||
import com.github.libretube.obj.ImportPlaylistFile
|
import com.github.libretube.obj.ImportPlaylistFile
|
||||||
import com.github.libretube.obj.NewPipeSubscription
|
import com.github.libretube.obj.NewPipeSubscription
|
||||||
import com.github.libretube.obj.NewPipeSubscriptions
|
import com.github.libretube.obj.NewPipeSubscriptions
|
||||||
@ -112,13 +113,37 @@ class ImportHelper(
|
|||||||
fun importPlaylists(uri: Uri?) {
|
fun importPlaylists(uri: Uri?) {
|
||||||
if (uri == null) return
|
if (uri == null) return
|
||||||
|
|
||||||
val playlistFile = ObjectMapper().readValue(uri.readText(), ImportPlaylistFile::class.java)
|
val importPlaylists = mutableListOf<ImportPlaylist>()
|
||||||
|
|
||||||
|
when (val fileType = activity.contentResolver.getType(uri)) {
|
||||||
|
"text/csv", "text/comma-separated-values" -> {
|
||||||
|
val playlist = ImportPlaylist()
|
||||||
|
activity.contentResolver.openInputStream(uri)?.use {
|
||||||
|
val lines = it.bufferedReader().readLines()
|
||||||
|
playlist.name = lines[1].split(",").reversed()[2]
|
||||||
|
val splitIndex = lines.indexOfFirst { line -> line.startsWith("Video ID") }
|
||||||
|
lines.subList(splitIndex + 1, lines.size).forEach { line ->
|
||||||
|
line.split(",").firstOrNull()?.let { videoId ->
|
||||||
|
if (videoId.isNotBlank()) playlist.videos = playlist.videos + videoId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
importPlaylists.add(playlist)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"application/json", "application/*", "application/octet-stream" -> {
|
||||||
|
val playlistFile = ObjectMapper().readValue(uri.readText(), ImportPlaylistFile::class.java)
|
||||||
|
importPlaylists.addAll(playlistFile.playlists.orEmpty())
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
activity.applicationContext.toastFromMainThread("Unsupported file type $fileType")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
playlistFile.playlists?.let {
|
PlaylistsHelper.importPlaylists(activity, importPlaylists)
|
||||||
PlaylistsHelper.importPlaylists(activity, it)
|
activity.applicationContext.toastFromMainThread(R.string.success)
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
e.localizedMessage?.let {
|
e.localizedMessage?.let {
|
||||||
@ -126,8 +151,6 @@ class ImportHelper(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
activity.toastFromMainThread(R.string.success)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user