@@ -4,6 +4,7 @@ import android.content.Context
44import android.net.Uri
55import android.util.Log
66import androidx.core.net.toUri
7+ import androidx.documentfile.provider.DocumentFile
78import com.github.libretube.R
89import com.github.libretube.api.JsonHelper
910import com.github.libretube.api.PlaylistsHelper
@@ -36,6 +37,9 @@ object ImportHelper {
3637 private const val VIDEO_ID_LENGTH = 11
3738 private const val YOUTUBE_IMG_URL = " https://img.youtube.com"
3839
40+ // format: playlistName-videos.csv, where "videos" could also be i18ned to a different language
41+ private val csvPlaylistNameRegex = Regex (""" (.*)-(\w+)\.csv""" )
42+
3943 /* *
4044 * Import subscriptions by a file uri
4145 */
@@ -192,7 +196,8 @@ object ImportHelper {
192196
193197 val playlistName = lines[1 ].split(" ," ).reversed().getOrNull(2 )
194198 // the playlist name can be undefined in some cases, e.g. watch later lists
195- playlist.name = playlistName ? : TextUtils .getFileSafeTimeStampNow()
199+ playlist.name = playlistName ? : extractYTPlaylistName(context, uri)
200+ ? : TextUtils .getFileSafeTimeStampNow()
196201
197202 // start directly at the beginning if header playlist info such as name is missing
198203 val startIndex = if (playlistName == null ) {
@@ -212,16 +217,11 @@ object ImportHelper {
212217 ?.takeIf { it.isNotBlank() }
213218
214219 if (videoId != null ) {
215- playlist.videos + = videoId.trim()
220+ playlist.videos + = videoId.trim().takeLast( VIDEO_ID_LENGTH )
216221 }
217222 }
218223 importPlaylists.add(playlist)
219224 }
220-
221- // convert the YouTube URLs to videoIds
222- importPlaylists.forEach { importPlaylist ->
223- importPlaylist.videos = importPlaylist.videos.map { it.takeLast(VIDEO_ID_LENGTH ) }
224- }
225225 }
226226
227227 ImportFormat .URLSORIDS -> {
@@ -365,4 +365,11 @@ object ImportHelper {
365365 context.toastFromMainDispatcher(R .string.success)
366366 }
367367 }
368+
369+ private fun extractYTPlaylistName (context : Context , uri : Uri ): String? {
370+ val fileName = DocumentFile .fromSingleUri(context, uri)?.name
371+
372+ return csvPlaylistNameRegex.find(fileName.orEmpty())?.groupValues?.getOrNull(1 )
373+ ? : fileName?.removeSuffix(" .csv" )
374+ }
368375}
0 commit comments