Skip to content

Commit 34fb27c

Browse files
authored
Merge pull request #7481 from Bnyro/master
feat: extract playlist name from filename at YouTube CSV imports
2 parents 1d28597 + 2f51439 commit 34fb27c

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ dependencies {
103103
implementation(libs.androidx.navigation.fragment)
104104
implementation(libs.androidx.navigation.ui)
105105
implementation(libs.androidx.preference)
106+
implementation(libs.androidx.documentfile)
106107
implementation(libs.androidx.work.runtime)
107108
implementation(libs.androidx.collection)
108109
implementation(libs.androidx.media)

app/src/main/java/com/github/libretube/helpers/ImportHelper.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.net.Uri
55
import android.util.Log
66
import androidx.core.net.toUri
7+
import androidx.documentfile.provider.DocumentFile
78
import com.github.libretube.R
89
import com.github.libretube.api.JsonHelper
910
import 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
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[versions]
22
appcompat = "1.7.1"
33
core = "1.13.1"
4+
documentfile = "1.1.0"
45
gradle = "8.6.1"
56
junit = "4.13.2"
67
kotlin = "1.9.25"
@@ -40,6 +41,7 @@ androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version
4041
androidx-core = { group = "androidx.core", name = "core", version.ref = "core" }
4142
androidx-core-splashscreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "splashscreen" }
4243
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
44+
androidx-documentfile = { module = "androidx.documentfile:documentfile", version.ref = "documentfile" }
4345
androidx-fragment = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "fragment" }
4446
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
4547
junit = { module = "junit:junit", version.ref = "junit" }

0 commit comments

Comments
 (0)