diff --git a/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt b/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt index 4e6ab85e..d070a25b 100644 --- a/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt +++ b/app/src/main/kotlin/com/svenjacobs/app/leon/startup/ContainerInitializer.kt @@ -42,6 +42,7 @@ import com.svenjacobs.app.leon.core.domain.sanitizer.spotify.SpotifySanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.twitter.TwitterSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.webtrekk.WebtrekkSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.yahoo.YahooSearchSanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.youtube.YoutubeMusicSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.youtube.YoutubeRedirectSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.youtube.YoutubeShortUrlSanitizer import com.svenjacobs.app.leon.sanitizer.SanitizerRepositoryImpl @@ -76,6 +77,7 @@ class ContainerInitializer : DistinctInitializer { TwitterSanitizer(), WebtrekkSanitizer(), YahooSearchSanitizer(), + YoutubeMusicSanitizer(), YoutubeRedirectSanitizer(), YoutubeShortUrlSanitizer(), ), diff --git a/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeMusicSanitizer.kt b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeMusicSanitizer.kt new file mode 100644 index 00000000..eba2cefd --- /dev/null +++ b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeMusicSanitizer.kt @@ -0,0 +1,44 @@ +/* + * Léon - The URL Cleaner + * Copyright (C) 2022 Sven Jacobs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.svenjacobs.app.leon.core.domain.sanitizer.youtube + +import android.content.Context +import com.svenjacobs.app.leon.core.domain.R +import com.svenjacobs.app.leon.core.domain.sanitizer.Sanitizer +import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerId + +class YoutubeMusicSanitizer : Sanitizer { + + override val id = SanitizerId("youtube_music") + + override fun getMetadata(context: Context) = Sanitizer.Metadata( + name = context.getString(R.string.sanitizer_youtube_music_name), + ) + + override fun matchesDomain(input: String) = DOMAIN_REGEX.containsMatchIn(input) + + override fun invoke(input: String): String { + val firstGroup = DOMAIN_REGEX.find(input)?.groups?.get(1) ?: return input + return input.replaceRange(firstGroup.range, "") + } + + private companion object { + private val DOMAIN_REGEX = Regex("(?:https?://)?(music\\.)youtube\\.com") + } +} diff --git a/core-domain/src/main/res/values/strings.xml b/core-domain/src/main/res/values/strings.xml index 3d8e4b71..91fa40be 100644 --- a/core-domain/src/main/res/values/strings.xml +++ b/core-domain/src/main/res/values/strings.xml @@ -41,4 +41,5 @@ Yahoo Search YouTube Redirect Youtu.be + YouTube Music diff --git a/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeMusicSanitizerTest.kt b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeMusicSanitizerTest.kt new file mode 100644 index 00000000..1f6c83c9 --- /dev/null +++ b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeMusicSanitizerTest.kt @@ -0,0 +1,50 @@ +/* + * Léon - The URL Cleaner + * Copyright (C) 2022 Sven Jacobs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.svenjacobs.app.leon.core.domain.sanitizer.youtube + +import io.kotest.core.spec.style.WordSpec +import io.kotest.matchers.shouldBe + +class YoutubeMusicSanitizerTest : WordSpec( + { + val sanitizer = YoutubeMusicSanitizer() + + "matchesDomain" should { + + "match music.youtube.com domain" { + sanitizer.matchesDomain("https://music.youtube.com/") shouldBe true + } + + "not match regular youtube.com domain" { + sanitizer.matchesDomain("https://youtube.com/") shouldBe false + } + } + + "invoke" should { + + "convert music.youtube.com domain to youtube.com" { + sanitizer( + "https://music.youtube.com/playlist?list=RDCLAK5uy_mPolD_J22gS1SKxufARW" + + "cTZd1UrAH_0ZI", + ) shouldBe "https://youtube.com/playlist?list=RDCLAK5uy_mPolD_J22gS1SKxufARWcTZd1" + + "UrAH_0ZI" + } + } + }, +)