Skip to content

Commit

Permalink
feat: Add YouTube Music sanitizer (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs authored Nov 19, 2022
1 parent 15ca04e commit a3d0894
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,6 +77,7 @@ class ContainerInitializer : DistinctInitializer<Unit> {
TwitterSanitizer(),
WebtrekkSanitizer(),
YahooSearchSanitizer(),
YoutubeMusicSanitizer(),
YoutubeRedirectSanitizer(),
YoutubeShortUrlSanitizer(),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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")
}
}
1 change: 1 addition & 0 deletions core-domain/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
<string name="sanitizer_yahoo_search_name">Yahoo Search</string>
<string name="sanitizer_youtube_redirect_name">YouTube Redirect</string>
<string name="sanitizer_youtube_short_url_name" translatable="false">Youtu.be</string>
<string name="sanitizer_youtube_music_name" translatable="false">YouTube Music</string>
</resources>
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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"
}
}
},
)

0 comments on commit a3d0894

Please sign in to comment.