From b371f35502c5f2777d317bce9ac551259813cdf3 Mon Sep 17 00:00:00 2001 From: Sven Jacobs Date: Sat, 8 Jul 2023 09:09:10 +0200 Subject: [PATCH] feat: Add YouTube sanitizer --- .../app/leon/startup/ContainerInitializer.kt | 2 + .../sanitizer/youtube/YoutubeSanitizer.kt | 43 +++++++++++++++++ core-domain/src/main/res/values/strings.xml | 1 + .../sanitizer/youtube/YoutubeSanitizerTest.kt | 48 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizer.kt create mode 100644 core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizerTest.kt 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 9f8babdb..bb0b2cd0 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 @@ -53,6 +53,7 @@ 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.YoutubeSanitizer import com.svenjacobs.app.leon.core.domain.sanitizer.youtube.YoutubeShortUrlSanitizer import com.svenjacobs.app.leon.sanitizer.SanitizerRepositoryImpl import kotlinx.collections.immutable.persistentListOf @@ -97,6 +98,7 @@ class ContainerInitializer : DistinctInitializer { YahooSearchSanitizer(), YoutubeMusicSanitizer(), YoutubeRedirectSanitizer(), + YoutubeSanitizer(), YoutubeShortUrlSanitizer(), ), ) diff --git a/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizer.kt b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizer.kt new file mode 100644 index 00000000..9684a3b4 --- /dev/null +++ b/core-domain/src/main/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizer.kt @@ -0,0 +1,43 @@ +/* + * Léon - The URL Cleaner + * Copyright (C) 2023 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.common.domain.matchesDomain +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 YoutubeSanitizer : Sanitizer { + + override val id = SanitizerId("youtube") + + override fun getMetadata(context: Context) = Sanitizer.Metadata( + name = context.getString(R.string.sanitizer_youtube_name), + ) + + override fun matchesDomain(input: String) = + input.matchesDomain("(m\\.)?youtube\\.com", isRegex = true) + + override fun invoke(input: String) = PARAMS_REGEX.replace(input, "") + + private companion object { + private val PARAMS_REGEX = Regex("[?&](?!v=)[^&]+") + } +} diff --git a/core-domain/src/main/res/values/strings.xml b/core-domain/src/main/res/values/strings.xml index 93c01f1e..8b7b45dd 100644 --- a/core-domain/src/main/res/values/strings.xml +++ b/core-domain/src/main/res/values/strings.xml @@ -49,6 +49,7 @@ Webtrekk Yahoo Search YouTube Music + YouTube YouTube Redirect Youtu.be diff --git a/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizerTest.kt b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizerTest.kt new file mode 100644 index 00000000..26ce37a7 --- /dev/null +++ b/core-domain/src/test/kotlin/com/svenjacobs/app/leon/core/domain/sanitizer/youtube/YoutubeSanitizerTest.kt @@ -0,0 +1,48 @@ +/* + * Léon - The URL Cleaner + * Copyright (C) 2023 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 YoutubeSanitizerTest : WordSpec( + { + val sanitizer = YoutubeSanitizer() + + "invoke" should { + + "remove all parameters except v=" { + sanitizer( + "https://m.youtube.com/watch?v=CvFH_6DNRCY&pp=ygUHZGVidXNzeQ%3D%3D", + ) shouldBe "https://m.youtube.com/watch?v=CvFH_6DNRCY" + } + } + + "matchesDomain" should { + + "match youtube.com domain" { + sanitizer.matchesDomain("https://youtube.com/") shouldBe true + } + + "match m.youtube.com domain" { + sanitizer.matchesDomain("https://m.youtube.com/") shouldBe true + } + } + }, +)