Fix the importing of playlists

This commit is contained in:
Bnyro 2023-01-31 16:23:34 +01:00
parent 9607edef22
commit f274dd205b
2 changed files with 21 additions and 23 deletions

View File

@ -19,12 +19,12 @@ import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.obj.ImportPlaylist import com.github.libretube.obj.ImportPlaylist
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ProxyHelper import com.github.libretube.util.ProxyHelper
import java.io.IOException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
object PlaylistsHelper { object PlaylistsHelper {
private val pipedPlaylistRegex = private val pipedPlaylistRegex =
@ -68,30 +68,25 @@ object PlaylistsHelper {
} }
} }
suspend fun createPlaylist( suspend fun createPlaylist(playlistName: String, appContext: Context?): String? {
playlistName: String,
appContext: Context
): String? {
if (!loggedIn) { if (!loggedIn) {
val playlist = LocalPlaylist(name = playlistName, thumbnailUrl = "") val playlist = LocalPlaylist(name = playlistName, thumbnailUrl = "")
DatabaseHolder.Database.localPlaylistsDao().createPlaylist(playlist) DatabaseHolder.Database.localPlaylistsDao().createPlaylist(playlist)
return DatabaseHolder.Database.localPlaylistsDao().getAll() return DatabaseHolder.Database.localPlaylistsDao().getAll()
.last().playlist.id.toString() .last().playlist.id.toString()
} else { } else {
val response = try { return try {
RetrofitInstance.authApi.createPlaylist(token, Playlists(name = playlistName)) RetrofitInstance.authApi.createPlaylist(token, Playlists(name = playlistName))
} catch (e: IOException) { } catch (e: IOException) {
appContext.toastFromMainThread(R.string.unknown_error) appContext?.toastFromMainThread(R.string.unknown_error)
return null return null
} catch (e: HttpException) { } catch (e: HttpException) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
appContext.toastFromMainThread(R.string.server_error) appContext?.toastFromMainThread(R.string.server_error)
return null return null
}.playlistId.also {
appContext?.toastFromMainThread(R.string.playlistCreated)
} }
if (response.playlistId != null) {
appContext.toastFromMainThread(R.string.playlistCreated)
}
return response.playlistId
} }
} }
@ -159,26 +154,23 @@ object PlaylistsHelper {
} }
} }
suspend fun importPlaylists(appContext: Context, playlists: List<ImportPlaylist>) { suspend fun importPlaylists(playlists: List<ImportPlaylist>) {
for (playlist in playlists) { for (playlist in playlists) {
val playlistId = createPlaylist(playlist.name!!, appContext) ?: continue val playlistId = createPlaylist(playlist.name!!, null) ?: continue
// if logged in, add the playlists by their ID via an api call // if logged in, add the playlists by their ID via an api call
val success = if (loggedIn) { if (loggedIn) {
addToPlaylist(playlistId, *playlist.videos.map { StreamItem(url = it) }.toTypedArray()) addToPlaylist(playlistId, *playlist.videos.map {
StreamItem(url = it)
}.toTypedArray())
} else { } else {
// if not logged in, all video information needs to become fetched manually // if not logged in, all video information needs to become fetched manually
try { runCatching {
val streamItems = playlist.videos.map { val streamItems = playlist.videos.map {
RetrofitInstance.api.getStreams(it).toStreamItem(it) RetrofitInstance.api.getStreams(it).toStreamItem(it)
} }
addToPlaylist(playlistId, *streamItems.toTypedArray()) addToPlaylist(playlistId, *streamItems.toTypedArray())
} catch (e: Exception) {
false
} }
} }
appContext.toastFromMainThread(
if (success) R.string.importsuccess else R.string.server_error
)
} }
} }

View File

@ -19,6 +19,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
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 okio.use import okio.use
@ -109,6 +110,7 @@ class ImportHelper(
/** /**
* Import Playlists * Import Playlists
*/ */
@OptIn(ExperimentalSerializationApi::class)
fun importPlaylists(uri: Uri?) { fun importPlaylists(uri: Uri?) {
if (uri == null) return if (uri == null) return
@ -141,9 +143,13 @@ class ImportHelper(
} }
} }
// convert the YouTube URLs to videoIds
importPlaylists.forEach { playlist ->
playlist.videos = playlist.videos.map { it.takeLast(11) }
}
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
try { try {
PlaylistsHelper.importPlaylists(activity, importPlaylists) PlaylistsHelper.importPlaylists(importPlaylists)
activity.applicationContext.toastFromMainThread(R.string.success) activity.applicationContext.toastFromMainThread(R.string.success)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())